【第二回】discord.jsでbot制作。コマンド制作と登録
discord.js botのコマンドファイルの制作と登録
コマンドはファイルを書くだけじゃダメという話
本稿ではスラッシュコマンドファイルの中身、そしてそれを使うためのメインファイル(本シリーズではindex.jsとする)、そしてスラッシュコマンドをdeploy-commands.jsを使って登録するという流れまで紹介します。ざーっと流していくので、色んなバリエーションを確認したい人は公式を読むようにしてください。マジで基本的な情報なので、この辺りはチュートリアル見てやった方が早いっす。
DIsocodのbotと言えば、やっぱりコマンド。開発的にはスラッシュコマンドと呼んでいます。「/ping」「/hello」みたいなスラッシュの後に続くコマンドがそれです。
コマンドは公式ガイド ではcommandsフォルダの下に作っています。本記事でもそれに従っていきます。
cpmmandsフォルダを作ったら、さらにその下にutilityというフォルダを作ります。その下に、ping.jsというファイルを作りましょう。ここにコマンドの内容を書いていきます。とりあえず、完成したコードからお見せします。
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
コードをちょこっと説明
これはスラッシュコマンド「/ping」というものを送ると、botが「pong!」と返してくれるというシンプルなコマンドです。ここで重要なのは「.setName」がコマンド名を表し、「.serDescription」がどういうコマンドなのかという説明文になるということです。
asyncは非同期処理ですね。そしてdiscord.jsでよく出てくるintaerationとはユーザとbotが対話するときに発生するイベントで、さまざまな処理を行うために必要なオブジェクトです。つまり、このinteractionという名称はdiscord.js開発における慣習のようなもので、名称自体は任意のもの(例えばeventにしても)動くっぽいです。
index.js(メインファイル)を修正する。Command handling
スラッシュコマンドのファイルを作りましたが、このままでは動きません。ひとまず、メインファイルのindex.jsを修正して、コマンドファイルの読み込みに必要な処理などを書いていきます。完成したコードの全文は以下の通り。
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
}
}
});
client.login(token);
色々追加しましたが、追加内容はファイルの読み込みに必要なコードということですね。fsやらpathやら必要なものをインポートしています。これらを使うことで、commandsフォルダ以下の「〜.js」で終わるファイルを探し出して、読み込みそれを定数commandに入れているという流れです。この部分はfor文で入れ子にして回しているという感じなので、ぜひチェックしてみてください。
コマンドを登録するためのファイル
プロジェクト直下にdeploy-commands.jsというファイルを作りましょう。中身は以下のように。
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);
// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
そして、config.jsonに二つの項目を足していきます。以下はその全文。
TOKENは以前、書いたものですね。clientIdはDiscord DeveloperのGeneral Informationから確認可能。GuildIDはディスコードの開発者モードをオンにした状態でサーバーのアイコンを右クリックすると、サーバーのIDが確認できるのでコピってください。
{
"token": "your-token-goes-here",
"clientId": "your-application-id-goes-here",
"guildId": "your-server-id-goes-here"
}
ここまできたらあとはターミナルでnode deploy-commands.jsと送信すればcommandsフォルダ以下のコマンドファイルが登録されます。新しいコマンドファイルを作ったらこの作業をターミナルでお忘れなく。
Discussion