【问题标题】:Can't send a message to a specific channel无法向特定频道发送消息
【发布时间】:2022-01-08 13:20:35
【问题描述】:

我正在尝试创建一个 modmail 系统,每当我尝试创建它时,它都会显示“channel.send 不是函数,这是我的代码。”

const Discord = require("discord.js")
const client = new Discord.Client()
const db = require('quick.db')
// ...
client.on('message', message => {
  if(db.fetch(`ticket-${message.author.id}`)){
    if(message.channel.type == "dm"){
      const channel = client.channels.cache.get(id => id.name == `ticket-${message.author.id}`)
      channel.send(message.content)
    }
  }
})
// ...
client.login("MYTOKEN")

我正在尝试使用 12.0.0 版

编辑: 我发现了我的问题,由于某种原因,保存的 ID 是机器人 ID,而不是我的 ID

【问题讨论】:

    标签: javascript node.js npm discord.js quick.db


    【解决方案1】:

    正如 MrMythical 所说,您应该使用 find 函数而不是 get。我认为问题在于您正在抓取一个非文本频道,因为定义了 channel,所以您无法向其发送任何内容。

    您可以通过添加额外的捕获来解决此问题,以确保您获得的是文本频道,而不是类别或语音频道。如果channel 未定义,我也会return(或发出各种错误消息)。

    Discord.js v12:

    const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'text');
    

    Discord.js v13:

    const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'GUILD_TEXT');
    

    编辑: 你可以说channel 是被定义的,因为如果不是,它会说类似Cannot read property 'send' of undefined 的内容。

    【讨论】:

    • 它仅在创建票证时定义(在另一个 client.on() 函数中)
    • 当我尝试您的修复时,我刚刚收到错误 Cannot read property 'send' of undefined
    • 您 100% 确定您要查找的频道存在并且您的机器人可以访问它?
    【解决方案2】:

    你正试图用一个函数来找到它。请改用.find

    const channel = client.channels.cache.find(id => id.name == `ticket-${message.author.id}`)
    

    【讨论】:

    • 我之前做过,但没用
    • 频道确定存在吗?
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 2021-08-21
    • 2020-04-04
    • 2021-06-10
    • 2018-12-09
    • 2021-01-26
    相关资源
    最近更新 更多