Discord.py 消失的反应故障

时间:2021-05-10 11:53:07

标签: python discord.py

我制作了一个非常简单的反应角色系统,该系统通过在任何非客户端用户的 raw_reaction_add 上运行并为用户提供具有他/她反应的表情符号名称的角色来工作。 例如:响应名称为“Python”的表情符号,响应的用户将获得表情符号名称的角色,在这种情况下为“Python”。 当反应启动并运行时,这非常有效。我让机器人首先添加反应,这样用户就不必停留在有反应的消息上。然而,随着时间的推移,反应完全消失了,我可以在下面向您展示我的代码,但我认为这更像是一个不和谐的小故障,我正在努力寻找解决方法。

反应角色代码

@client.event
async def on_raw_reaction_add(payload):
    Guild = client.get_guild(payload.guild_id) #gets guild object from the parameter, should work on multiple servers
    if Guild.id != 794843921501913108: #to just keep this while its in the works on this server
        return
    
    if payload.channel_id == 828876919936253952:
        Channel = discord.utils.get(Guild.channels, name="?rules")
        Message = await Channel.fetch_message(payload.message_id)
        Emoji = payload.emoji
        Role = discord.utils.find(lambda x: x.name == Emoji.name, Guild.roles) #should work as long as role name and emoji name are the same
        reactor = discord.utils.find(lambda u: u.id == payload.user_id, Guild.members)
        #Will run if info above is viable
        if reactor is not None and not payload.user_id == client.user.id:
            if Role in reactor.roles:
                await reactor.remove_roles(Role)
                try: #Runs if the user already has the role
                    await dm_user(id=reactor.id,message=f"Now why did you do that?")
                except:
                    pass
            else: #Runs if the user does not already have the role
                await reactor.add_roles(Role)
                try:
                    await dm_user(id=reactor.id,message=f"Welcome to the server!")
                except:
                    pass

            #Removes reactors reaction
            snowflake = discord.Object(reactor.id)
            await Message.remove_reaction(Emoji, snowflake)

    #React Roles channel only react roles
    if payload.channel_id == 839854066922160138:
        li = []
        #@TESTING
        for role in message.author.roles:
            li.append(discord.utils.find(lambda x: "python" in x.name.lower() and not x in li, message.author.roles))
        li = [x for x in li if x is not None]
        
        Channel = discord.utils.get(Guild.channels, name="?react_roles")
        Message = await Channel.fetch_message(payload.message_id)

        #RETURNS >> "LANGUAGE_NAME", "LANGUAGE_NAME EXPERTISE"|
        #EX: "Python Expert" >> "Python" >> "Python Expert"
        lanuage, expertise = parseLang(payload.emoji) #EMOJI MUST BE FORMATED AS "LANG EXPERTISE"
        """
        Have to make a toggle feature that realises that any reactions with (Python *expertise*) when those are gone
        to have the python role toggle off, which can bug if the user puts in two roles for the same language
        """ 
        Role, Role_expertise = discord.utils.find(lambda x: x.name == language.name, Guild.roles), discord.utils.find(lambda x: x.name == Emoji.name, Guild.roles)
        reactor = discord.utils.find(lambda u: u.id == payload.user_id, Guild.members)
        #Will run if info above is viable
        if reactor is not None and not payload.user_id == client.user.id:
            if Role in reactor.roles:
                await reactor.remove_roles(Role)
                try:
                    await dm_user(id=reactor.id,message=f"You have removed the role: {Role.name}")
                except:
                    pass
            else:
                await reactor.add_roles(Role)
                try:
                    await dm_user(id=reactor.id,message=f"You have gained the role: {Role.name}")
                except:
                    pass
            #Removes reactors reaction
            snowflake = discord.Object(reactor.id)
            await Message.remove_reaction(Emoji, snowflake)
    
    else:
        return

第二个通道的反应 if 语句工作正常,没有遇到此错误。然而,第一个是,我尝试通过让机器人重新添加反应 on_member_join 来解决这个问题,但这只会在人们加入时为他们修复它,它仍然没有阻止这样一个事实,即当它想要时它实际上会自行消失.这是以防万一的代码:

会员加入

@client.event
async def on_member_join(member):
    channel = client.get_channel(828876919936253952)
    message = await channel.fetch_message(840474974262919198)
    await message.clear_reactions()
    await add_reactions_to_message("MixedEngineers","?rules",840474974262919198)

add_reactions_to_message()

async def add_reactions_to_message(emoji_name,channel_name,message_id,guild=794843921501913108):
    Guild = client.get_guild(guild)
    Channel = discord.utils.get(Guild.channels, name=channel_name)
    Message = await Channel.fetch_message(message_id)
    Emoji = discord.utils.find(lambda x: x.name == emoji_name,Guild.emojis)
    await Message.add_reaction(Emoji)

0 个答案:

没有答案