【问题标题】:My discord bot isn't responding to my commands我的 discord 机器人没有响应我的命令
【发布时间】:2022-12-15 11:26:14
【问题描述】:

我有多个 discord 机器人,但突然间,它们都停止响应我的命令。我从网上复制了这段代码,看看是不是我的代码有问题,但是这个好像也不管用。当我输入“$hello”时,没有任何反应。机器人在运行时会上线,但除此之外什么都不做。我仔细检查了我的 python 是否是最新的,该机器人具有服务器角色和权限,在多台服务器上进行了尝试,并确保该机器人在 discord 开发人员门户上具有管理员权限。我不确定还有什么问题。这可能只是巧合,但当问题发生时我安装了一些 discord-ui 包。但是,我确实卸载了它,但机器人仍然无法正常工作。是的,我真正的标志在代码中。

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.send('Hello!')

client.run('MY TOKEN IS HERE')

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    如果您从其他地方复制它,他们可能不会正确地做(证明:herehere)。检查文档后(检查我的猜测是否正确),它是await message.channel.send('things'),而不是await message.send('Hello!')。因此,要修复它,请更改为:

    import discord
    
    client = discord.Client()
    
    @client.event
    async def on_ready():
        print('We have logged in as {0.user}'.format(client))
    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
    
        if message.content.startswith('$hello'):
            await message.channel.send('Hello!')
    
    client.run('MY TOKEN IS HERE')
    

    PS:因为你只显示复制的代码,所以我只回答那个代码

    【讨论】:

    • 另外关于所有没有响应的机器人,检查你的机器人是否有消息意图(和你的代码)。你应该顺便说一句斜线命令。
    【解决方案2】:

    这是因为消息意图。如果禁用,您的机器人将看不到消息内容。当然你也可以只打开它,但建议使用斜杠命令。 discord.py 不支持斜杠命令,所以我建议使用pycord

    这是斜杠命令的简短示例

    import discord
    
    bot = discord.Bot()
    
    @bot.slash_command()
    async def hello(ctx, name: str = None):
        name = name or ctx.author.name
        await ctx.respond(f"Hello {name}!")
    
    bot.run("token")
    

    Pycord documentation

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2021-03-11
      • 1970-01-01
      • 2021-02-01
      • 2021-07-22
      • 2021-05-25
      • 2021-09-04
      • 2021-04-09
      相关资源
      最近更新 更多