Skip to content

Usage examples with Node.js

Maxime Malgorn edited this page May 26, 2022 · 4 revisions

Here are some examples of the use of the module in versions 2.* and 3.*.

The configurations used are indicative and you should customize them as you wish.
➡️ Check config.example.json file to know all available configuration keys and default values.

Use as an independent bot

const TicTacToe = require('discord-tictactoe');
new TicTacToe({ language: 'en' })
   .login('YOUR_BOT_TOKEN')
   .then(() => console.log('TicTacToe bot is ready to be used.'));

Use it with your own bot (discord.js v13)

const TicTacToe = require('discord-tictactoe');
const Discord = require('discord.js');
const client = new Discord.Client({
    intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES,
        Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
    ],
});
   
new TicTacToe({ language: 'fr', gameBoardReactions: true })
    .attach(client);
   
client.login('YOUR_BOT_TOKEN');

Use text command instead of slash command (discord.js v13)

const TicTacToe = require('discord-tictactoe');
// set "command" to an empty string and "textCommand" with prefix you want
new TicTacToe({ textCommand: '-tictactoe', command: '' })
   .login('YOUR_BOT_TOKEN')
   .then(() => console.log('TicTacToe bot is ready to be used.'));

Handle messages by your own

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

client.on('message', message => {
    if (message.content.startsWith('-tictactoe')) {
        game.handleMessage(message);
    }
});

client.login('YOUR_BOT_TOKEN');

Handle interactions by your own (for commands) (discord.js v13)

const TicTacToe = require('discord-tictactoe');
const Discord = require('discord.js');
const client = new Discord.Client({
  intents: [
    Discord.Intents.FLAGS.GUILDS,
    Discord.Intents.FLAGS.GUILD_MESSAGES
  ],
});
const game = new TicTacToe({ language: 'en', commandOptionName: 'user' });

client.on('ready', () => {
    // Register your command
    client.application.commands.create(
        {
            name: 'tictactoe',
            description: 'Play tictactoe',
            options: [
                {
                    type: 'USER',
                    name: 'user',
                    description: "Mention the User",
                    required: false
                }
            ]
        },
        'GUILD_ID'
    );

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

client.login('YOUR_BOT_TOKEN');