簡體   English   中英

AttributeError: 'Member' object 沒有屬性 'channel'

[英]AttributeError: 'Member' object has no attribute 'channel'

我想制作一個基於反應行動的 discord 機器人,代碼如下:

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的第二個參數不是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的唯一 function 的文檔。 然后我不斷點擊reaction.message.channel中的成員鏈,直到reaction .message.channel 的存在變得明顯。 我告訴你這個是因為你也可以那樣做。 基本上,我給了你一條魚,並試圖教你如何釣魚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM