Discord bot 在特定频道发送消息

我知道之前有人问过这个问题,但我找不到适合我的解决方案。但我希望我的 discord 机器人(当我编写命令时)在特定频道上发送消息,并且仅在该频道上发送消息。


目前,我的工作区中只有基本文件和文件夹 + 一个命令文件夹。索引文件如下所示:


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');

 },

};


繁星coding
浏览 354回答 1
1回答

精慕HU

client不是你从需要discord.js模块中得到的东西。它是在您的机器人开始时创建的,您可以通过以下方式获取您的客户端实例message.clientconst channel = message.client.channels.cache.get('CHANNEL_ID').send('message');
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript