为什么该程序不能作为不和谐机器人中具有自动角色的验证系统工作?
Why isn't the program working as a verification system with auto role in a discord bot?
请看下面的代码,我为验证做的thing.I想要一个程序,当一个新用户来到服务器并输入时msg:verify
我会得到一个反应验证要做的事情如果它在 15 秒内没有反应,程序将终止,如果其他用户代替请求验证的用户做出反应,则不会发生任何事情,并且在验证后用户将自动获得机器人的角色,并且角色名为 'members'。代码在 python 中。该程序会一直运行,直到没有任何反应,所以请help.thanks大家帮忙!!
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()))```
我刚刚测试了这段代码,它对我有用。您是否收到任何错误?
此外,您确定您的 bot 具有添加角色的权限 (manage_roles
),并且正在调用的服务器中存在具有该 ID 的角色吗?
最后,确保您尝试添加的角色低于您的机器人在角色层次结构中的角色。机器人可以不 添加一个比它自己的角色更高的角色,“它自己的角色”是当你的机器人加入服务器时创建的角色。该角色包含您的机器人的名称,您的机器人应该会自动分配它。
如果角色高于你的机器人的角色,你只需要把它拖下来。
请看下面的代码,我为验证做的thing.I想要一个程序,当一个新用户来到服务器并输入时msg:verify
我会得到一个反应验证要做的事情如果它在 15 秒内没有反应,程序将终止,如果其他用户代替请求验证的用户做出反应,则不会发生任何事情,并且在验证后用户将自动获得机器人的角色,并且角色名为 'members'。代码在 python 中。该程序会一直运行,直到没有任何反应,所以请help.thanks大家帮忙!!
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()))```
我刚刚测试了这段代码,它对我有用。您是否收到任何错误?
此外,您确定您的 bot 具有添加角色的权限 (manage_roles
),并且正在调用的服务器中存在具有该 ID 的角色吗?
最后,确保您尝试添加的角色低于您的机器人在角色层次结构中的角色。机器人可以不 添加一个比它自己的角色更高的角色,“它自己的角色”是当你的机器人加入服务器时创建的角色。该角色包含您的机器人的名称,您的机器人应该会自动分配它。
如果角色高于你的机器人的角色,你只需要把它拖下来。