Skip to content

Using slash command in V3 and V4

Maxime Malgorn edited this page Aug 30, 2022 · 1 revision

🚀 This page targets module versions 3.x and 4.x.
➡️ Are you using legacy module version 2? Check this dedicated page.

Version 3+ of the module fully supports Discord slash commands by default! ✅
Check more info about them in the following blog post: https://discord.com/blog/slash-commands-are-here

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

1. Setup the internal command

This version uses a slash command by default named tictactoe. You can change it by updating config value command to what you need.
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.

Mention the bot with 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

Example: @TicTacToeBot tttdeploy to deploy the command.

What about the text command?

⚠️ You should no longer use text commands. They are deprecated in the module from version 4.
More info on Discord blog: https://discord.com/blog/welcome-to-the-new-era-of-discord-apps

You can use both commands in parallel or just one of them:

  • To enable text command, fill the value of configuration key textCommand to what you need.
  • And if you want to disable slash command support, set configuration value of command to an empty text.

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) (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.application.commands.create(
        {
            name: 'tictactoe',
            description: 'Play tictactoe'
        },
        'GUILD_ID'
    );

    // Listening for interactions
    client.on('interactionCreate', interaction => {
        if (interaction instanceof Discord.CommandInteraction && interaction.commandName === 'tictactoe') {
            game.handleInteraction(interaction);
        }
    });
});

client.login('TOKEN');