【问题标题】:Discord.js sending a message to a specific channelDiscord.js 向特定频道发送消息
【发布时间】:2021-08-21 01:32:00
【问题描述】:

我未能实现非常简单的目标。我无法向特定频道发送消息。我已经浏览了有关堆栈溢出的文档和类似的线程。

client.channels.get().send()

不起作用。 它不是一个函数。 我也不认为它是官方文档中 Channel 类的方法,但到目前为止我发现的每个线程都告诉我使用它。

我设法让机器人通过收听消息然后使用message.reply() 来回复消息,但我不希望这样。我希望我的机器人在client.on('ready')的特定频道中说些什么

我错过了什么?

【问题讨论】:

    标签: node.js bots send discord.js


    【解决方案1】:

    你没有提供任何你已经测试过的代码,所以我会给你准备好的事件代码!

        client.on('ready', client => {
            client.channels.get('CHANNEL ID').send('Hello here!');
        })
    

    请注意您的频道 ID 是一个字符串。

    如果成功了请告诉我,谢谢!

    2020 年 6 月 13 日编辑:

    Discord.js 更新在 get 方法之前需要频道的 cache 成员。

    如果您的模块是旧版,上述方法仍然有效。我最近的解决方案是按如下方式更改发送线路。

            client.channels.cache.get('CHANNEL ID').send('Hello here!')
    

    如果您使用的是 TypeScript,则需要强制转换通道才能使用 TextChannel.send(message) 方法而不会出现编译器错误。

    import { TextChannel } from 'discord.js';
    
    ( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
    

    【讨论】:

    • 问题是同时存在同名的语音通道和文本通道。我试图向无法接收消息的频道发送消息。你的回答是正确的,我已经接受了。谢谢。
    【解决方案2】:

    对于 v12,它将如下:

      client.on('messageCreate', message => {
            client.channels.cache.get('CHANNEL ID').send('Hello here!');
        })
    

    编辑:我将其更改为记录消息。

    【讨论】:

    • 我使用 Typescript 并收到错误 Property 'send' does not exist on type 'GuildChannel'.
    • @callmenikk 您需要将其转换为 TextChannel,因为它只返回一个不包含“Send”方法的“Channel”。
    【解决方案3】:

    这适用于最新版本的 node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)

    【讨论】:

      【解决方案4】:

      每次删除消息时,我都会向特定频道发送消息。如果您想在没有频道 ID 的情况下发送消息,这将很有帮助。

      client.on("messageDelete", (messageDelete) => {
        const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
        channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
      }); 
      

      如果您不使用 messageDelete,请务必定义它。

      【讨论】:

        猜你喜欢
        • 2020-04-04
        • 2021-08-09
        • 2020-05-22
        • 2022-01-25
        • 2020-06-25
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        相关资源
        最近更新 更多