如何设置只能由特定角色按下的按钮?

问题描述 投票:0回答:1

这是代码:

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")
    @commands.has_role("🙍‍♂️ FUORI SERVIZIO")
    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')


            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 channel.send(embed=embed)
            await interaction.response.send_message(f"***SEI ENTRATO IN SERVIZIO ALLE: {orario}.***", ephemeral=True)


    @discord.ui.button(label="𝑼𝑺𝑪𝑰𝑻𝑨⬇️", style=discord.ButtonStyle.red, custom_id="2")
    @commands.has_role("🙋‍♂️ IN SERVIZIO")
    async def uscita(self, interaction: discord.Interaction, button: discord.ui.Button):
            
            orario_it = datetime.now(pytz.timezone('Europe/Rome'))
            orario = orario_it.strftime('%H:%M')

            
            SimpleView.stop_time = time.time()
            elapsed_time = SimpleView.stop_time - SimpleView.start_time
            time_d = timedelta(seconds=int(elapsed_time))

            channel = bot.get_channel(1215061241270374400)           
            embed=discord.Embed(color=0xda373c)
            embed.set_thumbnail(url="https://shft.cl/img/c/cdn.discordapp.com-101395680655364460.webp")
            embed.add_field(name="⬇️𝑼𝑺𝑪𝑰𝑻𝑨⬇️", value="", inline=False)
            embed.add_field(name="NOME:", value=f"{interaction.user.mention}", inline=True)                                        #BOTTONE USCITA
            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.add_field(name="TEMPO IN SERVIZIO:", value=f"{time_d}", inline=True)
            embed.set_footer(text="ARMERIA200")
            await channel.send(embed=embed)
            await interaction.response.send_message(f"***SEI USCITO DI SERVIZIO ALLE: {orario}, DOPO: {time_d}***", ephemeral=True)

我希望这两个按钮只能由具有“🙋u200d♂️ IN SERVIZIO”和“🙍u200d♂️ FUORI SERVIZIO”角色的人按下,我不知道该怎么做。 我尝试寻找不同的解决方案,但找不到任何有用的东西。 请帮助我🥺

python discord discord.py discord-buttons
1个回答
0
投票

您当前使用的装饰器仅设计用于discord.py的命令扩展,请将其删除。

为了使按钮仅对特定角色可用:

通过 Discords 开发者设置获取您要检查的角色 ID。

然后检查用户是否具有角色,并仅在他具有角色时运行其余代码,否则推迟交互。

role = interaction.guild.get_role({id here})
if role in interaction.user.roles:
   # code here
else:
   await interaction.response.defer()
© www.soinside.com 2019 - 2024. All rights reserved.