From 626ff85ae7616a59c95d7338c0df9baead412ce3 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Sun, 1 Aug 2021 17:58:41 +0200 Subject: [PATCH] docs: change example in readme to slash command (#6250) Co-authored-by: Noel --- README.md | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 55dd01c5b..8253672dc 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,35 @@ npm install discord.js ## Example usage +First, we need to register a slash command against the Discord API: +```js +const { REST } = require('@discordjs/rest'); +const { Routes } = require('discord-api-types/v9'); + +const commands = [{ + name: 'ping', + description: 'Replies with Pong!' +}]; + +const rest = new REST({ version: '9' }).setToken('token'); + +(async () => { + try { + console.log('Started refreshing application (/) commands'); + + await rest.put( + Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), + { body: commands }, + ); + + console.log('Sucessfully reloaded application (/) commands.'); + } catch (error) { + console.error(error); + } +})() +``` + +Afterwards we can create a quite simple example bot: ```js const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); @@ -49,9 +78,11 @@ client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); -client.on('messageCreate', message => { - if (message.content === 'ping') { - message.channel.send('pong'); +client.on('interactionCreate', async interaction => { + if (!interaction.isCommand()) return; + + if (interaction.commandName === 'ping') { + await interaction.reply('Pong!'); } });