discord.py机器人不会响应命令 [英] discord.py bot won't respond to commands

查看:55
本文介绍了discord.py机器人不会响应命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem where my bot won't respond to commands. here's my code:

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)

在代码中,我还有其他针对该机器人的客户端事件,例如对消息做出反应和对消息进行回复.我上面的代码即使在我注释掉所有其他注释后也无法正常工作.在运行程序时,我在不和谐频道中输入了!test arg,但是只有在未注释掉它的情况下,我的机器人才会收到编程的反应.

I have other client events for the bot in the code that have worked, such as reacting to messages and replying to messages. My above code hasn't been working even after I've commented out all of the other comments. while running the program, I've typed !test arg in my discord channel, but have only gotten the programmed reaction from my bot when it wasn't commented out.

推荐答案

一次只能运行一个Bot/Client.我将使用 Bot ,因为 Bot 类是 Client 类的子类,因此它可以执行父级可以做的所有事情.

You can only have one Bot/Client running at a time. I would use Bot because the Bot class is a subclass of the Client class, so it can do everything it's parent can do.

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)

这篇关于discord.py机器人不会响应命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆