pythondiscord.pyslash

Slash command in discord bot


import random
from random import choice

MAPS = ('Ascent' , 'Bind' , 'Breeze' , 'Fracture' , 'Haven' , 'Icebox' , 'Lotus' , 'Pearl' , 'Split')
AGENTS = ('Brimstone' , 'Chamber', 'Cypher' , 'Deadlock' , 'Fade' , 'Gekko', 'Harbor' , 'Jett' , 'KAY/O' , 'Killjoy' , 'Neon' , 'Omen' , 'Phoenix' , 'Raze' , 'Reyna' , 'Skye' , 'Sova' , 'Viper' , 'Yoru' , 'Astra' , 'Breach')

def get_response(message: str) -> str:
    p_message = message.lower()

    if p_message == 'rollmap':
        return str(random.choice(MAPS))

    if p_message == 'rollagent':
        return str(random.choice(AGENTS))

I have this script but it works when typing the word rollmap or rollagent. Is there any way to remake it so that it would be written /rollmap or /rollagent?

I tried many solutions but none helped me, I hope You will help me here)


Solution

  • I am not sure what exactly your question is, but if I understand correctly you are asking how to make a command that both works with a prefix and as a slash command?

    In that case, using discord.py you can do something like this:

    import random
    from discord.ext import commands
    
    @commands.hybrid_command(
        name="rollmap",
        description="Rolls a random map",
    )
    async def rollmap(self, context: Context) -> None:
        MAPS = ('Ascent' , 'Bind' , 'Breeze' , 'Fracture' , 'Haven' , 'Icebox' , 'Lotus' , 'Pearl' , 'Split')
        await context.send(str(random.choice(MAPS)))
    

    You can do the same for the rollagent command. You can find the documentation Here