Skip to content

Using slash command in V2

Maxime Malgorn edited this page Aug 30, 2022 · 5 revisions

⛔ This page targets module versions 2.2.x which is DEPRECATED and does not receive support.
➡️ Are you using new module v3 with discord.js v13 support? Check this dedicated page.

Starting from version 2.2.0, support for Discord slash commands has been added to the module! 👍
Check more info about them in the following blog post: https://blog.discord.com/slash-commands-are-here-8db0a385d9e6

Module supports two options for handling slash command, choose the one you prefer!

1. Using internal command

First you must set the value of configuration slashCommand with the name you want for the command, for example tictactoe. You can check config.example.json file if you want to see a configuration preview.

Slash command final result

Start the bot and you are ready to go! But, why the slash command is not available yet?
You must register it manually into your guild. Find more info here to know why.

Use one of these commands to deploy or delete the slash command:

Command Description Needed rights
?tttdeploy Register slash command based on name you configured Guild administrator
?tttdelete Delete registered slash command Guild administrator

ℹ️ You do not want to use the legacy text command in parallel?
Leave the value of configuration key command to an empty text and it will be disabled.

2. Using it with a custom handler

A new method has been added to the module to programatically start a game from an interaction.
So, you can create a custom command but you will have to register it yourself.

handleInteraction(interaction, client) (source code here)

A complete example with a slash command registered at bot startup and listening for interactions:

const TicTacToe = require('discord-tictactoe');
const Discord = require('discord.js');
const client = new Discord.Client();
const game = new TicTacToe({ language: 'en' });

client.on('ready', () => {
    // Register your command
    client.api.applications(client.user.id)
        .guilds('GUILD_ID')
        .commands.post({
            data: {
                name: 'tictactoe',
                description: 'Play tictactoe'
            }
        });

    // Listening for interactions
    client.ws.on('INTERACTION_CREATE', interaction => {
        if (interaction.data.type === 1 && interaction.data.name === 'tictactoe') {
            game.handleInteraction(interaction, client);
        }
    });
});

client.login('TOKEN');