Javascript 从一个通道记录消息

Javascript 从一个通道记录消息,javascript,node.js,discord,discord.js,channels,Javascript,Node.js,Discord,Discord.js,Channels,我已经尝试了好几次,但我不理解错误,我正在接收,但我会解释我正在尝试做什么:基本上我想记录来自一个通道的消息,并将这些消息粘贴到另一个通道。这是我到目前为止的代码 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 i

我已经尝试了好几次,但我不理解错误,我正在接收,但我会解释我正在尝试做什么:基本上我想记录来自一个通道的消息,并将这些消息粘贴到另一个通道。这是我到目前为止的代码

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.
(节点:17260)未处理的PromisejectionWarning:ReferenceError:未定义客户端
反对。(D:\stuff\S1 Discord Bot\S1 Bot\src\events\message\message.js:1:1)
at模块编译(内部/modules/cjs/loader.js:1063:30)
在Object.Module._extensions..js(internal/modules/cjs/loader.js:1092:10)
在Module.load(内部/modules/cjs/loader.js:928:32)
at Function.Module._load(内部/modules/cjs/loader.js:769:14)
at Module.require(内部/modules/cjs/loader.js:952:19)
根据需要(内部/modules/cjs/helpers.js:88:18)
在registerEvents(D:\stuff\S1 Discord Bot\S1 Bot\src\utils\registry.js:33:21)
(使用`node--trace warnings…`显示警告的创建位置)
(节点:17260)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:17260)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。

谢谢

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

const Discord=require(“Discord.js”);
//在不和谐等情况下
const client=new Discord.client();
如果您试图只允许guildMessages,请按如下方式执行:

if(message.channel.type==“dm”)返回//不要对dms有反应
最后,要通过id向通道发送消息:

message.guild.channels.cache.get('814685640088223795').send(msglog)//使用“缓存”,因为v12使用管理器

您似乎缺少两行重要的代码:

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

将文档中的这两行添加到index.js文件的开头应该可以正常工作。

我添加了这一行,但现在它给了我一个错误(节点:8640)未处理PromisejectionWarning:ReferenceError:Discord未定义查看我的编辑,你似乎错过了Discord的定义,我是一个使用Discord.js和node进行编码的初学者,但我已经将你所说的合并到了一起,这是我迄今为止的代码:我也收到了另一个错误,但这可能是因为我输入了错误的代码,但这是我的错误:(node:2552)未经处理的PromisejectionWarning:TypeError:无法读取MessageEvent.run上未定义的属性“tag”(D:\stuff\S1 Discord Bot\S1 Bot\src\events\MessageEvent.js:9:30
标记
未包含在您的代码中,因此我无法调试您的错误,您似乎在尝试运行之前就可以走到这里-因为您似乎正在尝试使用处理程序(我不确定
MessageEvent.js
是什么)。首先,我强烈建议您在所有命令中使用1
index.js
文件。如果您需要更多帮助,请在我的个人资料中添加我的不一致。如果您认为上述问题的答案正确,请将其标记为正确。您也没有注意到我对上述代码所做的更改,我建议将这些添加到中以保存未来的错误。好吧,我会加上你的不和谐。我感觉他错过的不仅仅是那些台词