์ด ๋ฌธ์์ ์๋ณธ์ ์ธ๋ถ ์ํค์์ ๊ฐ์ ธ์์ต๋๋ค.
discord.js | |
ํ์ํ Node.js ๋ฒ์ | v12.0.0 ์ด์ |
์ต์ ๋ฒ์ | v12.5.1 |
์ง์๋๋ ๊ฐ์ฅ ์ค๋๋ ๋ฒ์ | v11.0.0[1] |
๊ด๋ จ ๋งํฌ | |
1. ๊ฐ์[ํธ์ง]
๋์ค์ฝ๋ API๋ฅผ ์ฌ์ฉํ์ฌ ๋ด์ ๋ง๋๋ Node.js ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
discord.js๋ npm๋๋ yarn์์ ์ค์นํ ์ ์๋ค.
npm์์๋
discord.js๋ npm๋๋ yarn์์ ์ค์นํ ์ ์๋ค.
npm์์๋
npm install discord.js
yarn์์๋
yarn add discord.js
์ด๋ ๊ฒ ์ค์น ๊ฐ๋ฅํ๋ค.
ํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋์ค์ฝ๋ ๋ด์ ๊ฐ๋ฐํ ๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๊ณ ์์ผ๋ฉฐ, discord RPC๋ฅผ ์ง์ํ๊ณ Commando๋ผ๋ ๋ช ๋ น์ด๋ฅผ ์์์ ํธ๋ค๋งํด์ฃผ๋ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ค.
์ฐธ๊ณ ๋ก ๊ตฌ๋ฒ์ ์ด ํ์ํ ๊ฒฝ์ฐ์๋ npm์ ๊ฒฝ์ฐ
ํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋์ค์ฝ๋ ๋ด์ ๊ฐ๋ฐํ ๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๊ณ ์์ผ๋ฉฐ, discord RPC๋ฅผ ์ง์ํ๊ณ Commando๋ผ๋ ๋ช ๋ น์ด๋ฅผ ์์์ ํธ๋ค๋งํด์ฃผ๋ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ค.
์ฐธ๊ณ ๋ก ๊ตฌ๋ฒ์ ์ด ํ์ํ ๊ฒฝ์ฐ์๋ npm์ ๊ฒฝ์ฐ
npm install discord.js@<๋ฒ์ ๋ฒํธ>๋ก ์ค์นํ ์ ์๋ค[2]2. ๊ฐ๋ฐ ์ ์ฃผ์์ฌํญ[ํธ์ง]
์ฐ์ ์ด ์์ ๋ฅผ ๋ฐ๋ผํ๊ธฐ ์ ์, discord.Collection() ๊ฐ์ฒด๋ฅผ ์๋ฒฝํ ์ฌ์ฉํ ์ ์์ด์ผ ํ๋ค. Collection ๊ฐ์ฒด๋ ๊ฑฐ์ ๋ชจ๋ ์ข
๋ฅ์ cache์์[3] ๋ฆฌํดํ๋ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ .filter, .map ๋ฑ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋๋ฐ ์ต์ํ์ง ์๋ค๋ฉด ํด๋น ๊ธฐ๋ฅ๋ค์ ํ์ฉํ๊ธฐ ์ด๋ ต๋ค.
3. ์์ [ํธ์ง]
3.1. ์์ 1[ํธ์ง]
const Discord = require('discord.js');
const client = new Discord.Client(); // Discord.Client ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
client.on('ready', () => { // ready ์ด๋ฒคํธ์ ์คํํ ํจ์
console.log(`Logged in as ${client.user.tag}!`); // client.user ๋ ์์ ์ ์ ์ ๊ฐ์ฒด์ด๊ณ tag ๋ ์ ์ ๊ฐ์ฒด์ ํ๋กํผํฐ ์
๋๋ค.
});
client.on('message', msg => { // message ์ด๋ฒคํธ์ msg (Discord.Message) ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๊ณ ์คํํ ํจ์
if (msg.content === 'ping') { // Discord.Message ๊ฐ์ฒด์ content ํ๋กํผํฐ๊ฐ 'ping' ์ผ ๋
msg.reply('Pong!'); // reply ๋ ๋ฉ์
+ , msg ๋ก ์ถ๋ ฅ๋ฉ๋๋ค.
}
});
client.login('token'); // ํ ํฐ์ ์
๋ ฅํฉ๋๋ค. ์ฌ๋ฐ๋ฅด์ง ์์ ํ ํฐ์ผ ์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.์ด ์ฝ๋๋ if ๋ฌธ์ผ๋ก ๋ช ๋ น์ด๋ฅผ ํ๋จํ๋ค. ํ์ง๋ง ์ด๋ฐ ์์ผ๋ก ํ ํ์ผ์ if ๋ฌธ์ ๋๋ ค๊ฐ๋ค ๋ณด๋ฉด ์ด๋์๊ฐ ์ฝ๋๊ฐ 300์ค, 500์ค, ์ฌํ๋ฉด 3000์ค๊น์ง ๋ถ์ด๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ํด๋น ๋ฌธ์ ๋ ์คํ๊ฒํฐ ์ฝ๋๋ฅผ ๋ฐ์์ํฌ์ ์์ด ํ์ ์ ์ง ๋ณด์๊ฐ ๋งค์ฐ ์ด๋ ค์์ง๊ธฐ ๋๋ฌธ์ ๋ง์ ์ ๋ช ํ discord.js ๋ด ๋ค์ ๊ฑฐ์ ๋ค ๋ช ๋ น์ด ๋๋ ์ด๋ฒคํธ ํ์ผ์ ๋ถ๋ฆฌํ๊ณ , ๋ช ๋ น์ด ์ถ๊ฐ๋ฅผ ์๋ํํ๊ณ , ๊ฐ๋ ์ฑ์ ์ค์์ํ๋ค. ๋ฐ๋ผ์ ์์ 1์ ๋ณ๋ก ์ข์ง ์์ ์์์ด๋ค. ์ด ๋ฌธ์ ์ ์ discord.py๋ก ๊ฐ๋ฐํ๋ ์ฌ๋๋ค์๊ฒ์๋ ๋ง์ด ๋ณผ ์ ์๋ ๋ฌธ์ ์ด๋ค.
3.2. ์์ 2[ํธ์ง]
index.js
commands/ping.js
์ด๋ฐ ์์ผ๋ก ํ์ผ์ ๋ถ๋ฆฌํ๊ณ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์๋ํํ๋ฉด ์์ ๋ฌธ์ ๋ฅผ ๋ฐ๋ก ํด๊ฒฐํ ์ ์๋ค.
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
const prefix = '!';
client.commands = new Discord.Collection()
// ๋ช
๋ น์ด ์บ์ ์ปฌ๋ ์
์ ํด๋ผ์ด์ธํธ ๋ด์ ์ ์ธํ๋ค. ํด๋น ๋ฐฉ๋ฒ์ผ๋ก ๋ช
๋ น์ด ํ์ผ ๋ด์์๋ client.commands๋ก ๋ค๋ฅธ ๋ช
๋ น์ด๋ค์ ์ ๊ทผํ ์ ์๋ค.
client.commands.load = dir => {
for (const file of fs.readdirSync(dir)) {
const cmd = require(`./commands/${file}`);
client.commands.set(cmd.name, cmd);
}
console.log(client.commands.map(c => c.name).join(', ') + ' ๋ช
๋ น์ด๊ฐ ๋ก๋๋จ.');
}
client.commands.load(__dirname + "/commands");
//ํด๋น ํ์ผ์ด ์์นํ ๋๋ ํฐ๋ฆฌ์์ "/commands" ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐ
client.on('ready', () => console.log(`${client.user.tag} ์ ๋ก๊ทธ์ธ๋จ`));
client.on('message', msg => {
if (msg.author.bot) return;
if (!msg.content.startsWith(prefix)) return;
if (msg.content.slice(0, prefix.length) !== prefix) return;
const args = msg.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
let cmd = client.commands.get(command);
//get๋ ์ปฌ๋ ์
๋ด์ ํด๋น key ๊ฐ์ ๊ฐ์ง ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด falsy ๊ฐ์ ๋ฐํํ๋ฏ๋ก ๋ถ๋ถ์ ์ผ๋ก Collection#has์ฒ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
if(cmd) cmd.run(client, msg, args);
})
client.login('token');commands/ping.js
const Discord = require('discord.js');
//run์ด๋ผ๋ ๋ฉ์๋(function)์ export(์์ถ)
exports.run = (client, msg, args) => {
msg.reply(`${client.ws.ping}ms`);
};
exports = {
name: 'ping'
};์ด๋ฐ ์์ผ๋ก ํ์ผ์ ๋ถ๋ฆฌํ๊ณ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์๋ํํ๋ฉด ์์ ๋ฌธ์ ๋ฅผ ๋ฐ๋ก ํด๊ฒฐํ ์ ์๋ค.
4. Commando ๋ฐฉ์[ํธ์ง]
์ด๋ discord.js Client๊ฐ ์๋ discord.js CommandoClient๋ฅผ ์ฌ์ฉํ์ฌ ๋ด์ ๋ง๋๋ ๊ฒ์ด๋ค.
์์ ํ์ผ ํธ๋ค๋ง์ ๋ฐฉ์์ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋์ฒดํ ์ ์์ผ๋ฉฐ, ๋ sqlite3๋ฅผ ์ง์ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฅ๋ ์ฝ๋ค.
CommandoClient๋ ๋จ์ํ discord.js Client์ ํ์ฅ์ด๋ฏ๋ก Client์ ์๋ ๋ฉ์๋, ํด๋์ค ๋ฑ์ ์ฌ๊ธฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
์์ ํ์ผ ํธ๋ค๋ง์ ๋ฐฉ์์ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋์ฒดํ ์ ์์ผ๋ฉฐ, ๋ sqlite3๋ฅผ ์ง์ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฅ๋ ์ฝ๋ค.
CommandoClient๋ ๋จ์ํ discord.js Client์ ํ์ฅ์ด๋ฏ๋ก Client์ ์๋ ๋ฉ์๋, ํด๋์ค ๋ฑ์ ์ฌ๊ธฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
4.1. ์์ [ํธ์ง]
const Commando = require('discord.js-commando')
const client = new Commando.Client({
owner: '์์ ์ ์์ด๋'
})
const path = require('path')
client.registry
// ๋ช
๋ น์ด๋ค์ ๊ทธ๋ฃน๋ค์ ๋ฑ๋กํฉ๋๋ค.
.registerGroups([
['fun', 'Fun commands'],
['some', 'Some group'],
['other', 'Some other group']
])
// ๊ธฐ๋ณธ ๋ช
๋ น์ด, ๊ทธ๋ฃน ๋ฑ์ ๋ฑ๋กํฉ๋๋ค.
.registerDefaults()
// ๋ค๋ฅธ ํด๋ (์ฌ๊ธฐ์๋ commands) ์ ์๋ ๋ช
๋ น์ด ํ์ผ ๋ค์ ๋ถ๋ฌ์ค๊ณ ๋ฑ๋กํฉ๋๋ค.
.registerCommandsIn(path.join(__dirname, 'commands'));
const sqlite = require('sqlite');
// Commando์๋ ๊ธธ๋ ๋ณ ์ ๋์ฌ, ๋ช
๋ น์ด ํ์ฑํ ๋๋ ๋นํ์ฑํ ๋ฑ์ ๊ธฐ๋ฅ์ด ์์ง๋ง, ์ด๋ฅผ ์ ์ฅํด ๋์ผ๋ ค๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ํ์ํ๊ธฐ ๋๋ฌธ์ sqlite๋ฅผ ์ด์ฉํฉ๋๋ค.
client.setProvider(
sqlite.open(path.join(__dirname, 'settings.sqlite3')).then(db => new Commando.SQLiteProvider(db))
).catch(console.error);
client.login('token goes here'); // ๋ง์ง๋ง์ผ๋ก discord.js Client ์ฒ๋ผ ๋ก๊ทธ์ธํฉ๋๋ค.์ด๋ฌ๋ฉด ๋ชจ๋ ์ค๋น๋ ๋์ด๋ค.
๋ช ๋ น์ด๋ฅผ commands ํด๋์ ์ ์ฅํ๊ณ ์ฝ๋๋ฅผ ์ง๋๊ฑด ์ฌ๊ธฐ์์ ํ์ธํ๋ฉด ๋๋ค.
5. discord RPC[ํธ์ง]
discord.js์๋ RPC ๊ธฐ๋ฅ๋ ์๋ค. ์ฐธ ์ ๋ง ๊ธฐ๋ฅ์ด ๋ง๋ค
5.1. ์์ [ํธ์ง]
const clientId = '187406016902594560';
const scopes = ['rpc', 'rpc.api', 'messages.read'];
const client = new RPC.Client({ transport: 'websocket' });
client.on('ready', () => {
console.log('Logged in as', client.application.name);
console.log('Authenticated as user: ' + client.user.username);
client.selectVoiceChannel('81384788862181376');
});
client.login({ clientId, scopes });5.2. ์ฌ์ฉ ๋ถ๊ฐ[ํธ์ง]
Discord Developer Portal์, RPC์ ๋ํ ์ ์ฒญ์ ๋ ์ด์ ๋ฐ์ง ์๋๋ค๊ณ ๋์์๋ค.
๋ฐ๋ผ์ ๋ง๋ค๊ณ ์ถ์ด๋ ๋ง๋ค์ง ๋ชปํ๋ฉฐ, ๊ฑฐ์ ํ์๊ฐ ์์ด์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๋ฐ๋ผ์ ๋ง๋ค๊ณ ์ถ์ด๋ ๋ง๋ค์ง ๋ชปํ๋ฉฐ, ๊ฑฐ์ ํ์๊ฐ ์์ด์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
6. ์ฌ๋ด[ํธ์ง]
- Commando๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ, ๋ช ๋ น์ด ์ธ์ ๋ฑ์ด ์๋ํ ๋์ด์์ด ๋ ํธํ๋ค๊ณ ๋๊ปด์ง ์ ์๊ฒ ์ง๋ง ๋๋ถ๋ถ์ ์ฌ์ฉํ์ง ์๋๋ค. Commando๋ ๋๋ฌด ํ์ ๋ง์ถฐ์ ธ์์ด ์ ํธ๋์ง ์๋๋ค.
- Windows XP/Vista์์[4] ๋ด์ ํธ์คํ ํ ์ ์๋ ๋ง์ง๋ง ๋ฒ์ ์ 8.2.0์ด๋ค. 9.x๋ถํฐ Node.js 6.x, 12.x๋ถํฐ๋ Node.js 12.x ๋ฒ์ ์ ์๊ตฌํ๋ค.