Node.js ์ค์นํ๊ณ npm init
https://discordjs.guide/preparations/#installing-node-js
discord.js ์ค์น
npm install discord.js
https://discordjs.guide/preparations/#opening-the-terminal
bot application ์ค์
OAuth2 URL generator ๋ก ๊ฐ์ bot, applications.commands ์ ํ ํ๊ณ Copyํ URL ๋ค์ด๊ฐ์ ์ํ๋ ์๋ฒ์ ๋ด ์ด๋!
https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot
๋ด ์ฝ๋ ์์ฑ ์์
config.json ํ์ผ ๋ง๋ค์ด์ token ์ ๋ ฅ
{
"token": "your-token-goes-here"
}
index.js
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
https://discordjs.guide/creating-your-bot/#creating-configuration-files
์ปค๋งจ๋ ์์ฑ
deploy-commands.js ํ์ผ์ ์์ฑํจ. ์ด ํ์ผ๋ก ๋ด ๋ด์ ์ฌ๋์ ์ปค๋งจ๋๋ฅผ ๋ฑ๋กํ๊ณ ์ ๋ ํ๋ค.
npm ์ค์น๊ฐ ํ์ํจ !
npm install @discordjs/builders @discordjs/rest discord-api-types
deploy-commands.js
const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info!'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info!'),
]
.map(command => command.toJSON());
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
์๋ ๋ช ๋ น์ด๋ฅผ ์คํํด์ ์ปค๋งจ๋๋ฅผ ๋ด ๊ธธ๋์ ๋ฑ๋กํ์! ํ๋ฒ๋ง ์คํํ๋ฉด ๋๋ค.
์ปค๋งจ๋๋ฅผ ์์ ํ๊ฑฐ๋ ์ถ๊ฐํ์ ๊ฒฝ์ฐ ๋ค์ ์คํํด์ผ ํ๋ค.
node deploy-commands.js
config.json ์์
- clientId: Discord Developter Portal / General Information ์์ APPLICATION ID
- guildId: ์๋ฒ ID
{
"clientId": "123456789012345678",
"guildId": "876543210987654321",
"token": "your-token-goes-here"
}
index.js ์์
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
} else if (commandName === 'server') {
await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
} else if (commandName === 'user') {
await interaction.reply(`Your tag: ${interaction.user.tag}\nYour id: ${interaction.user.id}`);
}
});
client.login(token);
https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script
Command handling
๋ด ๋ด ํ๋ก์ ํธ์ ๊ท๋ชจ๊ฐ ์์๋ ํ๋์ ํ์ผ์ ์ปค๋งจ๋๋ฅผ if / else if ๋ก ๋๋ฆฌ๋๊ฑด ์ข์ง ์๋ค.
์ปค๋งจ๋ ํธ๋ค๋ฌ๋ฅผ ๊ตฌํํ์!
npm install @discordjs/rest discord-api-types
index.js
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
} else if (commandName === 'beep') {
await interaction.reply('Boop!');
}
});
client.login(token);
deploy-commands.js
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');
const commands = [];
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
commands ํด๋๋ฅผ ๋ง๋ค์. ์ฌ๊ธฐ์ ๋ชจ๋ ์ปค๋งจ๋๋ฅผ ์ ์ฅํ ๊ฒ์ด๋ค.
์๊น ์ค์น ํ๋๋ฐ, ๋ ์ค์น ํ๋ผ๊ณ ๋์ด(?)
npm install @discordjs/builders
commands/ping.js ํ์ผ์ ๋ง๋ ๋ค. execute() ํจ์ ์์ ์๊น ์์ฑํ ๋ถ๋ถ ์ถ๊ฐ
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
index.js ํ์ผ์ ๋ค์์ ์ถ๊ฐ
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
fs.readdirSync() ๋ฉ์๋๋ ๋๋ ํ ๋ฆฌ์์ ๋ชจ๋ ํ์ผ๋ช ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ๋ค. e.g. ['ping.js', 'beep.js']
Dynamically executing commands
์ปค๋งจ๋ ์คํ์ ์ธํ ํ๋ client.commands Collection์ ์ฌ์ฉํ ์ ์๋ค.
interactionCreate ์ด๋ฒคํธ ์์ if / else if ์์ ๊ณ ๋ค์์ผ๋ก ๋์ฒดํ์.
๋จผ์ Collection ์์ ์ปค๋งจ๋ ๋ค์์ผ๋ก ์ปค๋งจ๋๋ฅผ ๊ฐ์ ธ์์ command ๋ณ์์ ํ ๋นํ๋ค.
ํด๋น ์ปค๋งจ๋๊ฐ ์กด์ฌํ๋ฉด .interaction ๋ณ์๋ฅผ ์ธ์๋ก execute() ๋ฅผ ํธ์ถํ๋ค.
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
์๋ก์ด ์ปค๋งจ๋๋ฅผ ๋ง๋๋ ค๋ฉด commands ํด๋์ ์ ํ์ผ์ ๋ง๋ค๋ฉด ๋๋ค.! (ํ์ผ๋ช ์ ์ฌ๋์ ์ปค๋งจ๋๋ ๋์ผํ๊ฒ)
๊ทธ๋ฆฌ๊ณ ์ปค๋งจ๋๋ฅผ ๋ฑ๋กํ๋ ค๋ฉด node deploy-commands.js ์คํ์ํค๋๊ฑธ ์์ง๋ง์!
https://github.com/discordjs/guide/tree/main/code-samples/creating-your-bot/command-handling
https://discordjs.guide/creating-your-bot/command-handling.html#individual-command-files
'Bot > Discord Bot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Discord.py] Bookmarks (0) | 2022.08.01 |
---|---|
discord.js bot ๋ง๋ค๊ธฐ (3) - Interactions (0) | 2022.07.13 |
discord.js bot ๋ง๋ค๊ธฐ (2) - Events (0) | 2022.07.13 |