Advertisement
Tranquility3

Nextcord dynamic buttons

Nov 13th, 2021
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. #=========================================#
  2. # This code will create a dynamic number  #
  3. # of dynamically labeled buttons attached #
  4. # to an embed                             #
  5. #=========================================#
  6.  
  7. import nextcord
  8.  
  9. from nextcord.ext import commands
  10.  
  11.  
  12. # Define a class and callback method to handle button clicks
  13. class Yea(nextcord.ui.Button):
  14.     async def callback(self, interaction):
  15.         # check if the interaction.user is the ctx.author
  16.         print("ctx.author.id: ",self.view.ctx.author.id)
  17.         print("interaction.user.id: ",interaction.user.id)
  18.         if interaction.user.id == self.view.ctx.author.id:
  19.                
  20.             #Prints the button label for the button clicked
  21.             print(self.label, self.style, interaction.user)
  22.            
  23.             #responds with an ephemeral message showing button clicked
  24.             await interaction.response.send_message(self.label, ephemeral=True)
  25.  
  26.             #Sets the view.value to the label of the button clicked
  27.             self.view.value = self.label
  28.  
  29.             #Ends the interation to stop other buttons being clicked
  30.             self.view.stop()
  31.        
  32.  
  33.  
  34.  
  35. # Define a simple View that gives us a confirmation menu
  36. class btn_create(nextcord.ui.View):
  37.     def __init__(self, ctx, data):
  38.         super().__init__()
  39.         self.value = None
  40.         self.ctx = ctx
  41.    
  42.     #######################################
  43.     #Create dynamic buttons based on data passed
  44.    
  45.         for x in data:
  46.             #creates an instance and sets the label
  47.            
  48.             btn = Yea(label = x['label'])
  49.            
  50.            
  51.             #Sets the label colour to grey if colour given not in
  52.             #the list of available colours
  53.            
  54.             if x['colour'].lower()=='green':
  55.                 btn.style = nextcord.ButtonStyle.green
  56.             elif x['colour'].lower()=='red':
  57.                 btn.style = nextcord.ButtonStyle.red
  58.             elif x['colour'].lower()=='blurple':
  59.                 btn.style = nextcord.ButtonStyle.blurple
  60.             else:
  61.                 btn.style = nextcord.ButtonStyle.grey
  62.            
  63.             #add the button to the view
  64.             self.add_item(btn)
  65.  
  66.  
  67.  
  68. async def ask(self, ctx, msg_title, msg_desc, btn_data, img = None, thumb = None):
  69.     """displays an embed with dynamically generated buttons beneath"""
  70.     # We create the view and assign it to a variable so we can wait for it later.
  71.     view = btn_create(ctx, btn_data)
  72.  
  73.     # Create an embed for the buttons
  74.     embed = nextcord.Embed(
  75.         title=msg_title,
  76.         description=msg_desc,
  77.         colour = self.bot.colors["BRANN"],
  78.     )
  79.     # Set an img if provided
  80.     if img:
  81.         embed.set_image(url=img)
  82.     # Set a thumbnail if provided
  83.     if thumb:
  84.         embed.set_thumbnail(url=thumb)
  85.    
  86.  
  87.     # Sending a embed containing our view
  88.     msg = await ctx.send(embed=embed, view=view)
  89.    
  90.     # Wait for the View to stop listening for input...
  91.     await view.wait()
  92.  
  93.     #
  94.    
  95.     if view.value is None:
  96.         print('Timed out...')
  97.         await msg.delete
  98.         return
  99.    
  100.     else:
  101.         return(view.value)
  102.  
  103.  
  104. #=========================================#
  105. # This is the command that sends the button list and recieves the answer (button clicked) by user
  106.  
  107. @commands.command(
  108.         name='pong',
  109.         description="button test function",
  110.  
  111.     )
  112.     #if you want to restrict command to a role
  113.     #@commands.has_role("DiscordAdmin")
  114.     @commands.has_permissions(send_messages=True)
  115.     async def pong(self, ctx):
  116.         #create embed title and description
  117.         msg_title = 'Button Test Embed'
  118.         msg_desc = 'Choose a button'
  119.         #create button list - actually a list of dictionaries
  120.         btn_data = [
  121.             {'label':'Button A', 'colour': 'red'},
  122.             {'label':'Button B', 'colour': 'green'},
  123.             {'label':'Button C', 'colour': 'blurple'},
  124.             {'label':'Button D', 'colour': 'grey'},
  125.            
  126.  
  127.         ]
  128.         #Launch the ask function
  129.         btn_pressed= await ask(self, ctx, msg_title, msg_desc, btn_data, None, self.bot.user.avatar.url)
  130.         await ctx.send(f"The button pressed: {btn_pressed}")
  131.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement