【问题标题】:Logging messages from one channel从一个通道记录消息
【发布时间】:2021-06-02 04:06:27
【问题描述】:

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

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.

谢谢

【问题讨论】:

    标签: javascript node.js discord discord.js channels


    【解决方案1】:

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

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

    如果你试图只允许公会消息,你可以这样做:

    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
    

    【讨论】:

    • 我添加了,但现在它给了我这个错误 (node:8640) UnhandledPromiseRejectionWarning: ReferenceError: Discord is not defined
    • 看看我的编辑,你似乎也缺少 Discord 定义
    • 我几乎是使用 Discord.js 编码和使用 node 的初学者,但我已经整合了你所说的内容,这是我迄今为止的代码:codepile.net/pile/WKBVdwKP 我也收到了另一个错误,但是这可能是因为我输入了错误的代码,但这是我的错误: (node:2552) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'tag' of undefined at MessageEvent.run (D:\stuff\S1 Discord Bot\s1-bot \src\events\MessageEvent.js:9:30
    • tag 不在您附加的代码中,因此我无法调试您的错误,看来您正在尝试运行,然后才能走到这里 - 因为您似乎正在尝试使用处理程序(我不确定MessageEvent.js 是什么)。首先,我强烈建议您对所有命令使用 1 个index.js 文件。如果您需要更多帮助,请在我的个人资料中添加我的 Discord,如果您认为以上是您上述问题的正确答案,请将其标记为正确。您也没有注意到我对上面代码的更改,我建议您添加这些更改以保存将来的错误。
    • 好的,我会添加你的不和谐。
    【解决方案2】:

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

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

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

    【讨论】:

    • 我感觉他缺少的远不止这些台词
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2019-03-05
    • 1970-01-01
    • 2014-10-08
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多