【问题标题】:How do I use message fetch in discord js?如何在不和谐 js 中使用消息获取?
【发布时间】:2021-08-24 06:48:29
【问题描述】:

我怎样才能让我的不和谐 js 机器人从频道中获取消息并将它们添加到嵌入并在频道中发送它们。 我试过https://stackoverflow.com/questions/53696875/get-last-message-sent-to-channel#:~:text=You%20can%20call%20the%20MessageManager,and%20get%20the%20latest%20message.&text=There%20is%20a%20property%20containing,let%20lm%20%3D%20channel。 但它会抛出

channel.messages.fetch({ limit: 1 }).then(messages => {
                  ^

TypeError: Cannot read property 'fetch' of undefined
    at Object.<anonymous> (/home/container/index.js:35:19)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)

在控制台中。 请帮忙。

【问题讨论】:

  • 您使用的是哪个 discord.js 版本。你的频道是什么样子的
  • channel.messagesundefined,你的channel 变量中存储了什么?请添加console.log(channel)的输出

标签: node.js discord discord.js


【解决方案1】:

首先,我不知道你的 channel 变量是什么,但它应该是 Discord.Channel

但是,要像您想要的那样获取频道的消息,您首先需要获得一个频道。 如何获取频道:

const channel_by_ID = client.channels.cache.get("CHANNEL-ID-HERE");

然后要从中获取消息,你不能只添加.fetch(),你需要请求.cache,所以像这样:

const channel = client.channels.cache.get("CHANNEL-ID-HERE");
const messageFetched = channel.messages.cache.fetch({ limit: 1 }).then(messages => {
     /* CODE HERE */
});

希望这对您的问题有所帮助,如果没有,我非常抱歉。 :

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2021-05-04
    • 2020-12-12
    • 2021-01-10
    • 2020-09-25
    • 2019-04-14
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多