Imaginary
Imaginary

Reputation: 125

How can I turn my discord bot into one which works with slash commands instead?

I made a nice and simple discord bot, here's the source code:

import discord

TOKEN = 'hi:)'


intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)

    print(f'{username}: {user_message} ({channel})')  # chat logs

    if message.author == client.user:  # so that the bot wouldn't reply to itself to infinity
        return

    if message.channel.name == 'bot-commands':
        if user_message.lower() == 'hello':
            await message.channel.send(f'Oh hello')
            return

        if user_message.lower() == 'bye':
            await message.channel.send(f'Goodbye')
            return

    if user_message.lower() == '!anywhere':
        await message.channel.send('This can be used anywhere!')
        return


client.run(TOKEN)

The bot also has a bunch of other cool (and more complex) commands that aren't shown here.

Now, how can I upgrade it all so that it'd work based on slash commands instead? I don't want to start from scratch, it took a long time to develop it. So is there some quick "upgrade" that I could apply to it which would make it operate on slash commands?

If not, how can I implement slash commands in addition to what I have? I think having both normal and slash commands is a good compromise.

Is there some template I can use for slash commands? I mean for normal commands there clearly is a template, you simply insert:

if user_message.lower() == 'insert some command here':
    # you do coding magic and then do:
    await message.channel.send(f'your result is here')
    return

If there is such template, what is it? Do I need to insert other things to the code so that such a template would work?

If you don't know how to help with any of the questions above, what tutorials do you recommend? I tried a few yt tutorials already (from Civo and Glowstik), but neither of them worked. One had errors, even though I copied the code word-for-word. The other just wouldn't show slash commands at all.

Upvotes: 0

Views: 796

Answers (1)

Prashant
Prashant

Reputation: 366

The code you posted is for event listeners mainly.

Have like this if you want both prefix & slash command for your bot.

from discord.ext.commands import when_mentioned_or

intents = discord.Intents.all()
client = Client(command_prefix=when_mentioned_or("/"), intents=intents)

To have slash commands, you have to use like:

commands.slash_command() => if you using in cogs

client.slash_command() => if you using in single file

for example:

@commands.slash_command(description="test")
async def test(self, ctx):
await ctx.respond("Test complete!")

Upvotes: 1

Related Questions