从一个通道记录消息

Logging messages from one channel

我已经尝试了好几次来解决这个问题,但我不明白这个错误,我收到了,但我会解释我想做什么:基本上我想记录来自一个频道的消息,将这些消息粘贴到不同的频道。这是我到目前为止的代码;

client.on(`message`, message => {
    if (message.author.bot) return; // If the message is by a bot return.
    if (!message.guild) return; // If the message isn't in a guild return.
    if (message.guild) {
        const msgLog = `[MESSAGE] [${message.guild.name}] [#${message.channel.name}] ${message.author.username}#${message.author.discriminator}: ${message.content}\n` // You can change this to whatever you want.
        client.channels.get(`814685640088223795`).send(msgLog); // Replace CHANNEL ID with the channel ID you want the logs to go to.
        return;
    }
})

我收到的错误如下:

(node:17260) UnhandledPromiseRejectionWarning: ReferenceError: client is not defined
    at Object.<anonymous> (D:\stuff\S1 Discord Bot\s1-bot\src\events\message\message.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at registerEvents (D:\stuff\S1 Discord Bot\s1-bot\src\utils\registry.js:33:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17260) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17260) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

谢谢

您的索引文件中缺少此内容:

const Discord = require("discord.js");
//under const Discord etc
const client = new Discord.Client();

如果你想只允许 guildMessages,你可以这样做:

if (message.channel.type === "dm") return; //dont react to dms

最后,通过id向频道发送消息:

message.guild.channels.cache.get('814685640088223795').send(msglog); //using 'cache' since v12 uses managers

您似乎遗漏了两行重要的代码:

const Discord = require('discord.js');
const client = new Discord.Client();

在 index.js 文件的开头附加 discord.js 文档中的这两行应该可以使它起作用。