pythonnextcord

nextcord.py Change Bot Activity per slash command (Python)


I changed the activity of my discord bot, so that its set to "Listening to" Now, i want to add a slash command that can manually change the activity I have absolutely no idea how this could work xD

Im expecting, a command like this: /setbotstatus <ACTIVITY> <NAME> Member Permission is: default_member_permissions=128


Solution

  • Assuming your commands.Bot instance is named bot

    Creating the slash command

    You can use the @bot.slash_command decorator to create a slash command.

    Adding arguments to the command

    nextcord automatically uses arguments passed to your command function to transform them into discord parameters, you just need to use correct typing on the parameters.

    The code

    @bot.slash_command()
    async def setbotstatus(interaction: nextcord.Interaction, activity: nextcord.SlashOption(choices={"Playing": nextcord.ActivityType.playing, "Watching": nextcord.ActivityType.watching, "Listening": nextcord.ActivityType.listening]), name: str):
        await interaction.client.change_presence(activity=nextcord.Activity(type=activity, name=name))
        await interaction.send("Activity changed")
    

    You can add more parameters to the command, for example to set the bot's status to online, idle, do not disturb or offline.