【问题标题】:Discord bot send msg in specific channelDiscord bot 在特定频道中发送消息
【发布时间】:2023-03-27 08:20:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    client 不是你从需要 discord.js 模块中得到的东西。它是在您的机器人开始时创建的,您可以通过message.client 获取您的客户端实例

    const channel = message.client.channels.cache.get('CHANNEL_ID').send('message');
    

    【讨论】:

      最近更新 更多