【问题标题】:AttributeError: 'Member' object has no attribute 'channel'AttributeError:“成员”对象没有属性“通道”
【发布时间】:2021-07-28 10:37:17
【问题描述】:

我想制作一个基于反应行动的不和谐机器人,代码如下:

import discord
    from discord.ext import commands
    import random
    pershealth=50
    enhealth=75
    client= commands.Bot(command_prefix='!')
    @client.event
    async def on_ready():
        await client.change_presence(activity=discord.Game("fighting"))
        print("Works")
    @client.command()
    async def fight(ctx):
        options=["no","yes"]
        choice=random.choice(options)
        if(choice=="no"):
            await ctx.send("no monsters where found")
        else:
            msg=await ctx.send("Monster found, do you wanna fight it?")
            await msg.add_reaction(emoji=u"\U0001F44D")
            await msg.add_reaction(emoji=u"\U0001F44E")
            @client.event
            async def on_reaction_add(reaction,message):
                if reaction.emoji=="????":
                    channel=message.channel.id
                    msg=await channel.send("test")
    client.run("code")

但是当我运行它时,它显示错误代码 AttributeError: 'Member' object has no attribute 'channel'。如何解决?

【问题讨论】:

  • 你能更正这段代码的格式吗? on_reaction_add 事件是否在战斗命令中?

标签: python discord discord.py


【解决方案1】:

on_reaction_add 的第二个参数不是message。这是user。我想你想要的是

@client.event
async def on_reaction_add(reaction, user):
    if reaction.emoji=="?":
        channel=reaction.message.channel.id
        msg=await channel.send("test")

老实说,我什至从未使用过 Discord 模块,对此一无所知。我只是查看了文档中您试图从参数中获取 channel 的唯一函数。然后我一直在点击reaction 中的成员链,直到很明显reaction.message.channel 存在。我告诉你这个是因为你也可以那样做。基本上,我给了你一条鱼,也试着教你如何钓鱼。

【讨论】:

  • 父亲教导,谚语可以“.. 养活他一辈子”?️ ...如果他学会了how to interpret the signal AttributeError 以及消息如何提示搜索内容'<TYPE>' object has no attribute '<NAME>'
猜你喜欢
  • 1970-01-01
  • 2019-03-15
  • 2021-08-06
  • 2019-01-10
  • 2019-09-25
  • 2019-11-24
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
相关资源
最近更新 更多