【问题标题】:Type Error: Cannot read the property of "send" of undefined类型错误:无法读取未定义的“发送”属性
【发布时间】:2021-01-02 05:00:29
【问题描述】:

抱歉,今天有很多问题。我正在处理我的票命令,但收到此错误类型错误:无法读取未定义的“发送”属性

知道我做错了什么吗? 任何形式的帮助都将不胜感激。

这是我的代码:

bot.on("message", message =>{
    const args = message.content.substring(PREFIX.length).split(" ")
    if(message.author.bot) return
    if(message.channel.type === "dm") return
    if(message.content.startsWith(`${PREFIX}openticket`)){
        const reason = message.content.split(" ").slice(1).join(" ");
        const role2 = message.guild.roles.cache.find(role2 => role2.name == "@everyone");

        

    message.guild.channels.create(`ticket-${message.author.username}`,  {
        permissionOverwrites: [
            {
              id: role2.id,
              deny: 'VIEW_CHANNEL',
              deny: 'READ_MESSAGE_HISTORY',
              deny: 'SEND_MESSAGES'
            }, {
              id: message.author.id,
              allow: 'VIEW_CHANNEL',
              allow: 'READ_MESSAGE_HISTORY',
              allow: 'SEND_MESSAGES'
            }
          ]

    })

        const embedcreated = new Discord.MessageEmbed()
        .setColor("#f00202")
        .setDescription(`You ticket has been created ` + `#ticket-${message.author.username}` )
        .setTitle("Tickets")
        .setFooter(`Tickets System`)
        .setTimestamp()

        message.channel.send(embedcreated);
        const embedticket = new Discord.MessageEmbed()
        .setColor("#f00202")
        .addField(`Hey ${message.author.username}!`, ` Please wait until support arrives`)
        .setTimestamp();

        const ticketchannel = message.guild.channels.cache.get(channel => channel.name == `ticket-${message.author.username}`)
            ticketchannel.send(embedticket),
            ticketchannel.send(`${message.author}`)
        }})

错误在这里:ticketchannel.send

我试过let ticketchannel = message.guild.channels.cache.get(channel => channel.name == `ticket-${message.author.username}`)

并将代码放在不同的地方但仍然不起作用

【问题讨论】:

  • 这个错误意味着ticketchannelundefined

标签: javascript discord bots


【解决方案1】:

与其尝试在变量中获取通道,不如使用回调。此外,allowdeny 属性允许 arrays,因此您可以将所有权限标志组合到 array

message.guild.channels
 .create(`ticket-${message.author.username}`, {
  permissionOverwrites: [
   {
    id: role2.id,
    deny: ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES'],
   },
   {
    id: message.author.id,
    allow: ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES'],
   },
  ],
 })
 .then((channel) => {  // pass the `channel` parameter in the callback

  // ...

  channel.send(embedticket);
  channel.send(`${message.author}`);
 });

【讨论】:

    【解决方案2】:

    您似乎正在执行来自message.guild.channels.cache 的一些对象,该对象应该具有send 方法。这里可能会发生两件事 -

    1. channel.name == ticket-${message.author.username} message.guild.channels.cache 内部的任何东西都不能满足这个条件。
    2. 无论您从 message.guild.channels.cache.get 获得的任何对象都没有 send 方法。

    letconst 与值没有任何关系。除非您重新分配变量,否则它们是可替换的。

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多