【问题标题】:Sending a message in a specific channel - not working在特定频道中发送消息 - 不起作用
【发布时间】:2021-09-30 05:16:23
【问题描述】:

我正在使用 discord.js 12,我尝试了以下方法:

client.channels.cache.find(x => x.id == "id").send('message')

client.channels.cache.find(x => x.name == "channel-name").send('message')

...and...

client.channels.cache.get('id').send('message')

并且它们都返回一个错误说'无法读取未定义的属性'发送'。

现在,我知道这通常意味着频道的 ID 错误。但是,我已经复制了它并重复了五次。我向你保证,这是正确的 ID。

我已经使用这个系统几个月了,现在它不能工作了。

注意:我在文件中定义了以下内容:

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

请帮帮我!

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您要么没有缓存频道,要么它不存在。如果它不存在,我无法帮助你。它返回未定义,因为它在缓存中看不到它。您可以使用简单的fetch() 方法轻松缓存频道。

    await client.channels.fetch();
    client.channels.cache.get(…).send(…)
    

    您也可以使用 API 直接获取频道

    (await client.channels.fetch('id')).send(…)
    

    如果您收到一条错误提示 await 仅在异步函数中有效,请将侦听器更改为如下所示:

    client.on(event, async (variable) => {
    //code here
    //notice it says 'async' before 'variable'
    })
    //'event' can be any event, variable is the object provided (message events provide message objects)
    

    【讨论】:

      【解决方案2】:

      试试

      const channel = await client.channels.fetch(<id>);
      
      await channel.send('hi')
      

      这应该可行。

      你也可以试试这个

      client.channels.fetch(channelID).then(channel => {
        channel.send("hi");
      }); 
      

      【讨论】:

      • 不会只是发送频道本身并引发错误哈哈?比如我如何向这个频道发送消息?
      • 哦,抱歉,我没看错,来晚了。我编辑了它
      • 哦,好吧,在那种情况下,这对我也不起作用,哈哈
      • 它也会抛出一个关于在异步函数之外使用 await 的错误。
      • 顶一下,试试新的@AndenWieseler
      猜你喜欢
      • 2020-01-28
      • 2021-02-28
      • 1970-01-01
      • 2023-03-27
      • 2021-10-19
      • 2020-07-02
      • 2021-08-21
      • 2020-04-04
      相关资源
      最近更新 更多