docs: change example in readme to slash command (#6250)

Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
Superchupu
2021-08-01 17:58:41 +02:00
committed by GitHub
parent 7c540764f0
commit 626ff85ae7

View File

@@ -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!');
}
});