Search code examples
pythondiscorddiscord.py

how to restrict people from using the help selection menu if they aren't the author & have a time limit


i want to make it so only the author of the command (so if you use /help) you'd be the only to be able to use the select menu. alongside that, i want to make it so there's a time limit.

i've made it ephemeral temporarily, i don't know where to start or begin as there isn't much online that is newer, majority of videos and information are outdated or in a different coding language

class Dropdown(discord.ui.Select):
    def __init__(self):

        options = [
            discord.SelectOption(label='General', value="1", description='Provides you with general commands and information about them.', emoji='1️⃣'),
            discord.SelectOption(label='Moderation', value="2", description='Provides you with moderation commands and information about them.', emoji='2️⃣'),
        ]
      
        super().__init__(placeholder='Choose what type of help you need below.', min_values=1, max_values=1, options=options)

    async def callback(self, interaction: discord.Interaction):

        generalEmbed=discord.Embed(title="General Commands", description="Everyone can use these commands, abusing them will result in punishment.", color=0xFF5349)
        generalEmbed.add_field(name="``/help``", value="Provides you with this embed.", inline=False)
        generalEmbed.add_field(name="``/suggest``", value="Sends a suggestion to <#1059976520376012811>.", inline=False)
        generalEmbed.set_footer(text="Made by justin;#6868")
      
        if self.values[0]=="1":
          await interaction.response.edit_message(embed=generalEmbed)

        moderationEmbed=discord.Embed(title="Moderation Commands", description="Staff with the correct permissions can use these commands. The following commands will be logged.", color=0xFF5349)
        moderationEmbed.add_field(name="``/purge``", value="Purges the specified amount of messages.", inline=False)
        moderationEmbed.add_field(name="``/kick``", value="Kicks the specified user with the reason.", inline=False)
        moderationEmbed.add_field(name="``/ban``", value="Bans the specified user with the reason.", inline=False)
        moderationEmbed.add_field(name="``/unban``", value="Unbans the specified user with the reason.", inline=False)
        moderationEmbed.add_field(name="``/mute``", value="Mutes the specified user with the reason and amount of time.", inline=False)
        moderationEmbed.add_field(name="``/unmute``", value="Unmutes the specified user with the reason.", inline=False)
        moderationEmbed.set_footer(text="Made by justin;#6868")
      
        if self.values[0]=="2":
          await interaction.response.edit_message(embed=moderationEmbed)

class DropdownView(discord.ui.View):
    def __init__(self):
        super().__init__()

        self.add_item(Dropdown())


class Bot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True

        super().__init__(command_prefix=commands.when_mentioned_or('k!'), intents=intents)

    async def on_ready(self):
        print('hi')


bot = Bot()

@bot.tree.command(name="help", description="Provides you with a help embed.")
async def _help(interaction):
    
  embed=discord.Embed(title="Help Menu", description="To find the help you're looking for, look below in this embed for whatever you need help with!", color=0xFF5349)
  embed.set_footer(text="Made by justin;#6868")

  view = DropdownView()
  
  await interaction.response.send_message(embed=embed, view=view, ephemeral=True)

Solution

  • You can override the interaction_check in the View class.

    #Class
    class DropdownView(discord.ui.View):
        def __init__(self, author): #taking an argument
            super().__init__()
    
            self.add_item(Dropdown())
            self.author = author
    
        async def interaction_check(self, interaction: discord.Interaction):
            return interaction.user.id == self.author.id
    
    #Command
    @bot.tree.command(name="help", description="Provides you with a help embed.")
    async def _help(interaction):
      embed=discord.Embed(title="Help Menu", description="To find the help you're looking for, look below in this embed for whatever you need help with!", color=0xFF5349)
      embed.set_footer(text="Made by justin;#6868")
    
      view = DropdownView(interaction.user) #giving the author argument
    
      await interaction.response.send_message(embed=embed, view=view, ephemeral=True)
    

    To add a timeout:

    #Class
    class DropdownView(discord.ui.View):
        def __init__(self, author): #taking an argument
            super().__init__(timeout=5) #in seconds
    
            self.add_item(Dropdown())
            self.author = author
    
        async def interaction_check(self, interaction: discord.Interaction):
            return interaction.user.id == self.author.id
    
        async def on_timeout(self):
            #do something