我知道之前有人问过这个问题,但我找不到适合我的解决方案。但我希望我的 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');
},
};
精慕HU
相关分类