๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Bot/Discord Bot

discord.js bot ๋งŒ๋“ค๊ธฐ (1)

๋ฐ˜์‘ํ˜•

 

Node.js ์„ค์น˜ํ•˜๊ณ  npm init

 

https://thebook.io/080229/

 

๋”๋ถ(TheBook): Node.js ๊ต๊ณผ์„œ ๊ฐœ์ • 2ํŒ

 

thebook.io

 

 

https://discordjs.guide/preparations/#installing-node-js

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

 

discord.js ์„ค์น˜

npm install discord.js

 

 

https://discordjs.guide/preparations/#opening-the-terminal

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

bot application ์„ค์ •

OAuth2 URL generator ๋กœ ๊ฐ€์„œ bot, applications.commands ์„ ํƒ ํ•˜๊ณ  Copyํ•œ URL ๋“ค์–ด๊ฐ€์„œ ์›ํ•˜๋Š” ์„œ๋ฒ„์— ๋ด‡ ์ดˆ๋Œ€! 

 

 

 

https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

 

 

๋ด‡ ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ์ž‘

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

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

 

์ปค๋งจ๋“œ ์ƒ์„ฑ

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

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

 

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

 

GitHub - discordjs/guide: The official guide for discord.js, created and maintained by core members of its community

The official guide for discord.js, created and maintained by core members of its community - GitHub - discordjs/guide: The official guide for discord.js, created and maintained by core members of i...

github.com

 

https://discordjs.guide/creating-your-bot/command-handling.html#individual-command-files

 

discord.js Guide

Imagine a guide... that explores the many possibilities for your discord.js bot.

discordjs.guide

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•

'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