为什么该程序不能在discord机器人中充当自动角色的验证系统?

2024-09-30 04:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

请看下面的代码,我为验证做了准备。我想要一个程序,当一个新用户来到服务器并键入msg:verify时,我将得到一个反应验证,如果它在15秒内没有反应,程序将终止,另外,如果其他用户代替请求验证的用户做出反应,则不会发生任何事情,验证后,用户将自动获得机器人程序的角色,该角色称为“成员”。代码为python。该计划的工作,直到反应后,什么也没有发生,所以请帮助。谢谢大家的帮助

async def verify(ctx):
        #define the reactions
        reactiontypes = ["🔔", "❎"]
        #delete the users message ("msg:verify")
        await ctx.message.delete()
        #create the embed...
        embed = discord.Embed()
        embed.title = "Verification!"
        embed.description = "Click the :bell: down below to get yourself verified!"
        embed.colour = discord.Colour.gold()
        #send the message and have "verifymsg" contain the sent message object
        verifymsg = await ctx.send(embed=embed)
        #add reactions that the user can click to verify (the first one is the bell, the second one is the x)
        await verifymsg.add_reaction(reactiontypes[0])
        await verifymsg.add_reaction(reactiontypes[1])
        #a function that will make sure that the user that later can react actually is the person that requested to verify themself
        def check(reaction, user):
            return user == ctx.author
        #"try" is necessary to catch eventual error
        try:
            #wait for the user to react, also set a timeout on 15 seconds for the user to answer
            reaction, user = await bot.wait_for("reaction_add", timeout=15, check=check)
        #if the user didn't react within 15s
        except asyncio.TimeoutError:
            #delete the verificaiton message
            await verifymsg.delete()
            await ctx.send(embed=discord.Embed(title="Timed out", description="You didn't react in time!", colour=discord.Colour.red()))
        #if the user DID answer within time
        else:
            #delete the verificaiton message
            await verifymsg.delete()
            #check if the bell was clicked
            if str(reaction.emoji) == reactiontypes[0]:
                #get the member role
                role = discord.utils.get(ctx.guild.roles, id=744014055248887890)
                #add the member role to the user
                await ctx.author.add_roles(role)

                await ctx.send(embed=discord.Embed(title="You have been verified", description="You are now verified, and can access this server!", colour=discord.Colour.green()))
            #if another reaction than the bell was clicked
            else:
                await ctx.send(embed=discord.Embed(title="You were not verified", description="You did not click the :bell:, please retry!", colour=discord.Colour.red()))```


Tags: thetosendaddmessageembedawaitdelete
1条回答
网友
1楼 · 发布于 2024-09-30 04:35:21

我刚刚测试了这段代码&;它对我有用。你有什么错误吗

另外,您是否确定您的bot具有添加角色(manage_roles)的权限,并且正在调用的服务器中存在具有该id的角色

最后,确保要添加的角色位于角色层次结构中机器人角色的下方。bot可以而不是添加一个位置高于其自身角色的角色,“it's own role”是在bot加入服务器时创建的角色。该角色拥有您的机器人的名称,您的机器人应该自动分配该角色

如果角色高于bot的角色,则只需将其向下拖动即可

相关问题 更多 >

    热门问题