# --- Core library imports ---fromscurrypyimport(Client,EventTypes,InteractionEvent,InteractionTypes,SlashCommandPart,MessagePart,ActionRowPart,ButtonPart,ButtonStyles)# --- Setup bot ---client=Client(token=TOKEN)asyncdefcreate_commands():awaitclient.guild_command(APP_ID,GUILD_ID).create(SlashCommandPart('button_demo','A command with a button!'))asyncdefhandle_button(event:InteractionEvent):ifevent.type==InteractionTypes.APPLICATION_COMMAND:ifevent.data.name!='button_demo':returnawaitclient.interaction(event.id,event.token).respond(MessagePart(content='Press the button!',components=[ActionRowPart([ButtonPart(ButtonStyles.PRIMARY,'btn_demo','Press me!')])]))elifevent.type==InteractionTypes.MESSAGE_COMPONENT:ifevent.data.custom_id!='btn_demo':returnawaitclient.interaction(event.id,event.token).update("You pressed the button!")client.add_startup_hook(create_commands)client.add_event_listener(EventTypes.INTERACTION_CREATE,handle_button)# --- Run the bot ---client.run()
asyncdefrefresh_commands():commands=awaitclient.command(APP_ID,GUILD_ID).fetch_all()forcmdincommands:awaitclient.command(APP_ID,GUILD_ID,cmd.id).delete()# then create your new commandsclient.add_startup_hook(refresh_commands)...
If you are using ScurryKit, this should be done for you.
# --- Core library imports ---fromscurrypyimportClient,EventTypes,SlashCommandPart,InteractionEvent,InteractionTypesimportasyncio# --- Setup bot and addons ---classStatefulBot:def__init__(self,client:Client):self.bot=clientself.user_points={}self.dict_lock=asyncio.Lock()client.add_startup_hook(self.register_commands)client.add_event_listener(EventTypes.INTERACTION_CREATE,self.dispatch)asyncdefregister_commands(self):"""Register slash commands on startup (before READY)."""bot_commands=self.bot.guild_command(APP_ID,GUILD_ID)commands=[SlashCommandPart('points','Check your points'),SlashCommandPart('addpoints','Give points')]forcmdincommands:awaitbot_commands.create(cmd)asyncdefpoints(self,event:InteractionEvent):"""Get points for the invoking user."""user=event.member.user.idpts=self.user_points.get(user,0)awaitself.bot.interaction(event.id,event.token).respond(f"You have {pts} points!")asyncdefaddpoints(self,event:InteractionEvent):"""Add points for the invoking user."""user=event.member.user.idasyncwithself.dict_lock:self.user_points[user]=self.user_points.get(user,0)+1awaitself.bot.interaction(event.id,event.token).respond("Point added!")asyncdefdispatch(self,event:InteractionEvent):"""Main entry point for commands."""ifevent.type!=InteractionTypes.APPLICATION_COMMAND:return# ignore non-command interactionsmatchevent.data.name:case'points':awaitself.points(event)case'addpoints':awaitself.addpoints(event)case_:awaitself.bot.interaction(event.id,event.token).respond(f"No command named '{event.data.name}'!")client=Client(token=TOKEN)StatefulBot(client)# --- Run the bot ---client.run()
# --- Core library imports ---fromscurrypyimportClient,Interaction,InteractionEventfromscurry_kitimportCommandsAddon,setup_default_logger# --- Logger ---logger=setup_default_logger()# --- Setup bot and addons ---client=Client(token=TOKEN)commands=CommandsAddon(client,APP_ID)importasynciouser_points={}dict_lock=asyncio.Lock()@commands.slash_command("points","Check your points",guild_ids=GUILD_ID)asyncdefpoints(bot:Client,interaction:Interaction):event:InteractionEvent=interaction.contextuser=event.member.user.idpts=user_points.get(user,0)awaitinteraction.respond(f"You have {pts} points!")@commands.slash_command("addpoints","Give points",guild_ids=GUILD_ID)asyncdefaddpoints(bot:Client,interaction:Interaction):event:InteractionEvent=interaction.contextuser=event.member.user.id# lock for asyncasyncwithdict_lock:user_points[user]=user_points.get(user,0)+1awaitinteraction.respond("Point added!")# --- Run the bot ---client.run()
# --- Core library imports ---fromscurrypyimportClient,EventTypes,SlashCommandPart,InteractionEvent,InteractionTypesimportasyncio# --- Setup bot state separate from event handlers ---classStatefulBot:def__init__(self,client:Client):self.bot=clientself.user_points={}self.dict_lock=asyncio.Lock()defget_points(self,user_id:int):"""Get points for the invoking user."""returnself.user_points.get(user_id,0)asyncdefaddpoints(self,user_id:int):"""Add points for the invoking user."""asyncwithself.dict_lock:self.user_points[user_id]=self.user_points.get(user_id,0)+1# --- Setup bot and bot state ---client=Client(token=TOKEN)asyncdefregister_commands():"""Register slash commands on startup (before READY)."""bot_commands=client.guild_command(APP_ID,GUILD_ID)commands=[SlashCommandPart('points','Check your points'),SlashCommandPart('addpoints','Give points')]forcmdincommands:awaitbot_commands.create(cmd)client.add_startup_hook(register_commands)bot_state=StatefulBot(client)asyncdefon_points(event:InteractionEvent):pts=bot_state.get_points(event.member.user.id)awaitclient.interaction(event.id,event.token).respond(f"You have {pts} points!")asyncdefon_add_points(event:InteractionEvent):awaitbot_state.addpoints(event.member.user.id)awaitclient.interaction(event.id,event.token).respond("Point added!")asyncdefdispatch(event:InteractionEvent):"""Main entry point for commands."""ifevent.type!=InteractionTypes.APPLICATION_COMMAND:return# ignore non-command interactionsmatchevent.data.name:case'points':awaiton_points(event)case'addpoints':awaiton_add_points(event)case_:awaitclient.interaction(event.id,event.token).respond(f"No command named '{event.data.name}'!")client.add_event_listener(EventTypes.INTERACTION_CREATE,dispatch)# --- Run the bot ---client.run()