mirror of
				https://github.com/Rapptz/discord.py.git
				synced 2025-11-02 14:32:53 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# This example requires the 'message_content' privileged intent to function.
 | 
						|
 | 
						|
from discord.ext import commands
 | 
						|
 | 
						|
import discord
 | 
						|
 | 
						|
 | 
						|
class CounterBot(commands.Bot):
 | 
						|
    # Suppress error on the User attribute being None since it fills up later
 | 
						|
    user: discord.ClientUser
 | 
						|
 | 
						|
    def __init__(self):
 | 
						|
        intents = discord.Intents.default()
 | 
						|
        intents.message_content = True
 | 
						|
 | 
						|
        super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)
 | 
						|
 | 
						|
    async def on_ready(self):
 | 
						|
        print(f'Logged in as {self.user} (ID: {self.user.id})')
 | 
						|
        print('------')
 | 
						|
 | 
						|
 | 
						|
# Define a simple View that gives us a counter button
 | 
						|
class Counter(discord.ui.View):
 | 
						|
    # Define the actual button
 | 
						|
    # When pressed, this increments the number displayed until it hits 5.
 | 
						|
    # When it hits 5, the counter button is disabled and it turns green.
 | 
						|
    # note: The name of the function does not matter to the library
 | 
						|
    @discord.ui.button(label='0', style=discord.ButtonStyle.red)
 | 
						|
    async def count(self, interaction: discord.Interaction, button: discord.ui.Button):
 | 
						|
        number = int(button.label) if button.label else 0
 | 
						|
        if number + 1 >= 5:
 | 
						|
            button.style = discord.ButtonStyle.green
 | 
						|
            button.disabled = True
 | 
						|
        button.label = str(number + 1)
 | 
						|
 | 
						|
        # Make sure to update the message with our updated selves
 | 
						|
        await interaction.response.edit_message(view=self)
 | 
						|
 | 
						|
 | 
						|
bot = CounterBot()
 | 
						|
 | 
						|
 | 
						|
@bot.command()
 | 
						|
async def counter(ctx: commands.Context):
 | 
						|
    """Starts a counter for pressing."""
 | 
						|
    await ctx.send('Press!', view=Counter())
 | 
						|
 | 
						|
 | 
						|
bot.run('token')
 |