我知道之前有人問過這個問題,但我找不到適合我的解決方案。但我希望我的 discord 機器人(當我編寫命令時)在特定頻道上發(fā)送消息,并且僅在該頻道上發(fā)送消息。目前,我的工作區(qū)中只有基本文件和文件夾 + 一個命令文件夾。索引文件如下所示:const discord = require('discord.js');const config = require('./config.json');const client = new discord.Client();const prefix = '>';const fs = require('fs');client.commands = new discord.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.name, command);}client.once('ready', () => { console.log(`${client.user.username} is online`);});client.on('message', (message) => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ + /); const command = args.shift().toLowerCase(); if (command === 'cmd') { client.commands.get('cmd').execute(message, args); }});client.login('CLIENT_ID');然后有一個與 index.js 處于同一級別的名為“commands”的文件夾,它包含 cmd.js,如下所示const client = require('discord.js');module.exports = { name: 'cmd', description: 'cmd', execute(message, args) { const channel = client.channels.cache.get('CHANNEL_ID').send('message'); },};
Discord bot 在特定頻道發(fā)送消息
繁星coding
2023-03-18 16:51:48