如何通过按钮添加和移除角色?
我需要确保按下一个按钮后,可以移除一个角色,同时添加另一个角色。
这是我的代码:
class SimpleView(discord.ui.View):
def __init__(self):
self.start_time = None
self.stop_time = None
super().__init__(timeout=None)
@discord.ui.button(label="⬆️", style=discord.ButtonStyle.green, custom_id="1")
async def entrata(self, interaction: discord.Interaction, button: discord.ui.Button):
orario_it = datetime.now(pytz.timezone('Europe/Rome'))
orario = orario_it.strftime('%H:%M')
allowed_role_id = 1215328270443094128
added_role_id = 1215318583702454352
removed_role_id = 1215328270443094128
allowed_role = discord.utils.get(interaction.guild.roles, id=allowed_role_id)
user = interaction.user
if allowed_role is None or allowed_role not in interaction.user.roles:
await interaction.response.send_message(f"***Per poter entrare in servizio dei essere in: {allowed_role.mention}!***", ephemeral=True)
if allowed_role in interaction.user.roles:
SimpleView.start_time = time.time()
channel = bot.get_channel(1214495527924928522)
embed=discord.Embed(color=0x23a559)
embed.set_thumbnail(url="https://shft.cl/img/c/cdn.discordapp.com-101395680655364460.webp")
embed.add_field(name="⬆️⬆️", value="", inline=False) #BOTTONE ENTRATA
embed.add_field(name="NOME:", value=f"{interaction.user.mention}", inline=True)
embed.add_field(name="", value="", inline=True)
embed.add_field(name="", value="", inline=False)
embed.add_field(name="", value="", inline=False)
embed.add_field(name="DATA:", value=f"{data}", inline=True)
embed.add_field(name="ORARIO:", value=f"{orario}", inline=True)
embed.set_footer(text="ARMERIA200")
await user.remove_roles(removed_role_id)
await user.add_roles(added_role_id)
await channel.send(embed=embed)
await interaction.response.send_message(f"***SEI ENTRATO IN SERVIZIO ALLE: {orario}.***", ephemeral=True)
我的代码不太好使,我该怎么办?
它应该是添加一个角色并移除另一个角色,但现在不行,请帮帮我。
1 个回答
0
在上面的代码中,你需要在 user.add_roles()
的参数中传入 discord.Role
(在 user.remove_roles
中也是如此),但是你现在传入的是角色的 ID。你需要通过 discord.guild.get_role()
来获取这个角色。
你可以用下面的代码来解决这个问题:
class SimpleView(discord.ui.View):
def __init__(self):
self.start_time = None
self.stop_time = None
super().__init__(timeout=None)
@discord.ui.button(label="⬆️", style=discord.ButtonStyle.green, custom_id="1")
async def entrata(self, interaction: discord.Interaction, button: discord.ui.Button):
orario_it = datetime.now(pytz.timezone('Europe/Rome'))
orario = orario_it.strftime('%H:%M')
allowed_role_id = 1215328270443094128
added_role_id = 1215318583702454352
removed_role_id = 1215328270443094128
allowed_role = interaction.guild.get_role(allowed_role_id) #here also you can use get_role instead of utils.get
user = interaction.user
if allowed_role is None or allowed_role not in interaction.user.roles:
await interaction.response.send_message(f"***Per poter entrare in servizio dei essere in: {allowed_role.mention}!***", ephemeral=True)
if allowed_role in interaction.user.roles:
SimpleView.start_time = time.time()
channel = bot.get_channel(1214495527924928522)
embed=discord.Embed(color=0x23a559)
embed.set_thumbnail(url="https://shft.cl/img/c/cdn.discordapp.com-101395680655364460.webp")
embed.add_field(name="⬆️⬆️", value="", inline=False) #BOTTONE ENTRATA
embed.add_field(name="NOME:", value=f"{interaction.user.mention}", inline=True)
embed.add_field(name="", value="", inline=True)
embed.add_field(name="", value="", inline=False)
embed.add_field(name="", value="", inline=False)
embed.add_field(name="DATA:", value=f"{data}", inline=True)
embed.add_field(name="ORARIO:", value=f"{orario}", inline=True)
embed.set_footer(text="ARMERIA200")
added_role = interaction.guild.get_role(added_role_id) #getting the role here
removed_role = interaction.guild.get_role(removed_role_id) #getting the role here
await user.remove_roles(removed_role) #removing the role here
await user.add_roles(added_role) #adding the role here
await channel.send(embed=embed)
await interaction.response.send_message(f"***SEI ENTRATO IN SERVIZIO ALLE: {orario}.***", ephemeral=True)
另外,你也可以使用这个方法来代替 discord.utils.get
。
记住:这个机器人在服务器中需要有 Manage_roles
的权限。
参考资料: