扫一扫分享
discord.js 是一个强大的Node.js模块,可让您轻松与Discord api交互 。其特点:
Discord 是一款专为社群设计的免费网路即时通话软体与数位发行平台,拥有 1.3 亿注册用户;
Node.js版本需要大于16.6.0
npm install discord.js
yarn add discord.js
pnpm add discord.js
Register a slash command against the Discord API:
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('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
Afterwards we can create a quite simple example bot:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login('token');
手机预览