Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9130

【基礎】 discord.js v13 でボット作成 #4 ~コマンドログ~

$
0
0
前書き 前回、コマンドをさらに使いやすく、ということでコマンドに情報を入れてみたりしました。 今回は、だれがいつ、どこでコマンドを使用したか、というログをとれるようにしたいと思います。 中型くらいのボットになると、トラブルを防ぐためにユーザーが使用したコマンドのログを取る事が多いと思われます(多分) 今回は、そのようなログを取る方法を紹介していきたいと思います。(意外と簡単かもです) 開発環境 前回 と変わりません Windows10 npm 7.6.3 node 17.0.1 discord.js 13.3.1 コーディング 前回触れたindex.jsを編集します。 index.js const fs = require('fs'); const { Client, Collection, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } - client.on('ready',() => { - console.log('起動完了'); - }); + const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); //eventsフォルダーからjsファイルを取得 + for (const file of eventFiles) { + const event = require(`./events/${file}`); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args, client)); + } + else { + client.on(event.name, (...args) => event.execute(...args, client)); + } + } ~ ~ ~ そしたらeventsというフォルダーを作成します。 eventsフォルダーの中にready.jsというファイルを作成してください。 シリーズ通り全く同じでやっている方は下記のようになっているかと思われます。 commands ├── ping.js ├── server.js └── user.js events └── ready.js deploy-commands.js index.js ready.jsに下記のコードを書いてください。 ready.js module.exports = { name: 'ready', //使用するイベントの名前 once: true, // true -> 一度だけ async execute(client) { console.log(`起動完了:ログイン中=> ${client.user.tag}`); }, }; そしたらボットを起動してください。 コンソールに「起動完了:ログイン中=> ボットの名前」が表示されれば完了です。 console.cmd > node . 起動完了:ログイン中=> ボットの名前 では、本命のログの取り方を紹介します。 eventsフォルダーの中にlog.jsファイルを作成してください(何度も言いますがファイル名は任意で大丈夫です) 作成したファイルに下記のコードを書いてください。 log.js const ch = '送信したいチャンネルID'; module.exports = { name: 'interactionCreate', async execute(interaction, client) { if (interaction.channel.type == 'GUILD_TEXT') { //サーバーの場合 await client.channels.cache.get(ch).send({content: `[${new Date().toLocaleString()}][${interaction.guild.name}(${interaction.guild.id})][${interaction.channel.name}]${interaction.user.tag} > /${client.commands.get(interaction.commandName).data.name}`}); } if (interaction.channel.type == 'DM') { //DMの場合 await client.channels.cache.get(ch).send({content: `[${new Date().toLocaleString()}][DM]${interaction.user.tag} > /${client.commands.get(interaction.commandName).data.name}`}); } }, }; 使用したコマンドの名前はclient.commands.get(interaction.commandName).data.nameで取得できます(多分...) そしたら起動し、コマンドを打つと指定したチャンネルにログが送信されるはずです。 今回のコーディングはこれで以上です。お疲れさまでした! 構文一覧 index.js const fs = require('fs'); const { Client, Collection, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of eventFiles) { const event = require(`./events/${file}`); if (event.once) { client.once(event.name, (...args) => event.execute(...args, client)); } else { client.on(event.name, (...args) => event.execute(...args, client)); } } 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: 'コマンド実行時にエラーが発生しました。', ephemeral: true}); } }); client.login('OTA2NzkyMTU3MTAzNDE5NDQy.YYdyAQ.7lq_oby9FfFHbXof5TY6wJ0tkyE'); ready.js module.exports = { name: 'ready', once: true, async execute(client) { console.log(`起動完了:ログイン中=> ${client.user.tag}`); }, }; log.js const ch = '906808703276175390'; module.exports = { name: 'interactionCreate', async execute(interaction, client) { if (interaction.channel.type == 'GUILD_TEXT') { await client.channels.cache.get(ch).send({content: `[${new Date().toLocaleString()}][${interaction.guild.name}(${interaction.guild.id})][${interaction.channel.name}]${interaction.user.tag} > /${client.commands.get(interaction.commandName).data.name}`}); } if (interaction.channel.type == 'DM') { await client.channels.cache.get(ch).send({content: `[${new Date().toLocaleString()}][DM]${interaction.user.tag} > /${client.commands.get(interaction.commandName).data.name}`}); } }, };

Viewing all articles
Browse latest Browse all 9130

Trending Articles