【问题标题】:discord.py bot won't respond to commandsdiscord.py bot 不会响应命令
【发布时间】:2020-09-12 13:52:44
【问题描述】:

我遇到了我的机器人无法响应命令的问题。这是我的代码:

import os
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import random

client = discord.Client()

bot = commands.Bot(command_prefix='!')

@client.event #server + member list
async def on_ready():
    guild = discord.utils.get(client.guilds, name=GUILD)
    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})\n'
    )

    members = '\n - '.join([member.name for member in guild.members])
    print(f'Guild Members:\n - {members}')

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

client.run(TOKEN)

我在代码中为机器人提供了其他客户端事件,例如对消息做出反应和回复消息。即使在我注释掉了所有其他 cmets 之后,我上面的代码也没有工作。在运行程序时,我在我的不和谐频道中输入了 !test arg,但只有在没有被注释掉时才从我的机器人那里得到编程的反应。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您一次只能运行一个机器人/客户端。我会使用Bot,因为Bot 类是Client 类的子类,所以它可以做它的父类可以做的所有事情。

    from discord.ext import commands
    import discord.utils
    
    bot = commands.Bot(command_prefix='!')
    
    @bot.event #server + member list
    async def on_ready():
        guild = discord.utils.get(bot.guilds, name=GUILD)
        print(
            f'{client.user} is connected to the following guild:\n'
            f'{guild.name}(id: {guild.id})\n'
        )
    
        members = '\n - '.join([member.name for member in guild.members])
        print(f'Guild Members:\n - {members}')
    
    @bot.command() 
    async def test(ctx, arg):
        await ctx.send(arg)
    
    bot.run(TOKEN)
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 2020-02-22
      • 2023-02-10
      • 2020-12-15
      • 2021-08-28
      • 2021-02-17
      • 2023-03-09
      相关资源
      最近更新 更多