【问题标题】:AttributeError: 'Client' object has no attribute 'command' error while running my codeAttributeError:“客户端”对象在运行我的代码时没有属性“命令”错误
【发布时间】:2020-09-30 19:38:39
【问题描述】:

我正在尝试编写一个 discord.py 机器人,它会为一条消息添加一个竖起大拇指,当点击该消息时,它将为用户提供“测试”角色。这是我正在使用的代码:

import discord
from discord.ext import commands

client = discord.Client()

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

messageIDs = [759721500579463219]

@client.event
async def on_raw_reaction_add(payload):
    global messageIDs

    for messageID in messageIDs:
        if messageID == payload.message_id:
            user = payload.member
            role = "Test"
            await user.add_roles(discord.utils.get(user.guild.roles, name = role))

@client.command()
async def addMessage(ctx, messageID):
    global messageIDs
    
    emoji = "????"
    channel = ctx.message.channel

    try:
        msg = await channel.fetch_message(messageID)
    except:
        await ctx.send("Invalid Message ID!")
        return
    await msg.add_reaction(emoji)
    messageIDs.append(messageID)

bot.run("Not gonna reveal this")

但是,当我运行代码时,我得到了这个错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\james\Desktop\Random Codes\CustomBot.py”,第 22 行,在 @client.command() AttributeError:“客户端”对象没有属性“命令”

请有人告诉我我做错了什么,如果可能的话,用所做的更正重写代码,因为有时我发现很难弄清楚人们的答案是什么意思。

提前谢谢你。

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您需要使用Bot 实例来使用命令,您正在使用Client

查看ext.commands 文档了解更多信息

【讨论】:

    【解决方案2】:

    试试这个,

    from discord.ext.commands import Bot
    

    Bot 是 Client 的子类,因此您还可以将所有 Client 功能与 Bot 实例一起使用,使用 discord.ext.commands.Bot 而不是 discord.Client

    from discord.ext.commands import Bot
    
    @bot.command()
    

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 2019-10-15
      • 2020-10-25
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多