【问题标题】:Discord bot discord.py commands aren't workingDiscord bot discord.py 命令不起作用
【发布时间】:2020-08-16 15:32:45
【问题描述】:

我对制作不和谐机器人相当陌生,我正在尝试制作一个基本的测试命令,你说(前缀)测试 abc,机器人也说 abc。我没有收到任何错误,但是当我输入 r!test abc 时,什么也没有发生。我在终端也一无所获。

这是我的代码。

import discord

from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()


GUILD = os.getenv('DISCORD_GUILD') # Same thing but gets the server name
client = discord.Client()
bot = commands.Bot(command_prefix='r!')
TOKEN = 'TOKENGOESHERE'
on = "I'm up and running!"
print("Booting up...")
channel = client.get_channel(703869904264101969)

@client.event # idk what this means but you have to do it

async def on_ready(): # when the bot is ready

    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(f'{client.user} has connected to Discord! They have connected to the following server: ' #client.user is just the bot name
    f'{guild.name}(id: {guild.id})' # guild.name is just the server name


    )

    channel2 = client.get_channel(channelidishere)
    await channel2.send(on)
#everything works up until I get to here, when I run the command, nothing happens, not even some output in the terminal.

@commands.command()
async def test(ctx):
    await ctx.send("test")



client.run(TOKEN)

谢谢大家!

【问题讨论】:

  • 机器人没有响应,这就是问题所在。我尝试了许多其他命令,其中机器人创建了一个频道,或者给某人一个角色,但这些也不起作用。即使是在终端中打印某些内容的基本命令也不起作用。

标签: python command discord.py


【解决方案1】:

关键问题是你没有正确定义你的装饰器。由于您使用的是命令,因此您只需要 bot = commands.Bot(command_prefix='r!') 语句。不需要client = discord.Client()。因为是bot = ...,所以你所有的装饰器都需要以@bot开头。另外,你不会使用客户端,你会使用像channel2 = bot.get_channel(channelidishere)这样的机器人。

移除公会循环并用discord.utils.get 替换它以获得公会 - guild = discord.utils.get(bot.guilds, name=GUILD)。这需要另一个导入 - import discord

试试这个:

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')  # Same thing but gets the server name
bot = commands.Bot(command_prefix='r!')
on = "I'm up and running!"
print("Booting up...")


@bot.event  # idk what this means but you have to do it
async def on_ready():  # when the bot is ready
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} has connected to Discord! They have connected to the following server: '  # client.user is just the bot name
        f'{guild.name}(id: {guild.id})')  # guild.name is just the server name
    channel2 = bot.get_channel(channelidishere)
    await channel2.send(on)


@bot.command()
async def test(ctx):
    await ctx.send("test")


bot.run(TOKEN)

【讨论】:

    猜你喜欢
    • 2021-01-02
    • 2020-09-24
    • 2019-07-23
    • 1970-01-01
    • 2021-01-05
    • 2020-08-07
    • 2021-04-16
    • 2021-05-26
    • 2018-09-17
    相关资源
    最近更新 更多