【问题标题】:JavaScript discord bot TypeError: Cannot read property 'guild' of undefinedJavaScript discord bot TypeError:无法读取未定义的属性“公会”
【发布时间】:2021-09-14 15:16:23
【问题描述】:

我正在我的 discord 机器人中尝试此 JavaScript 票证代码,但错误 TypeError: Cannot read property 'guild' of undefined 不断出现。我不明白为什么。有人可以指导我正确的方向吗?

module.exports = {
    name: "ticket",
    aliases: [],
    permissions: [],
    description: "Open a ticket!",
    async execute(message, args, cmd, client, discord) {
      const channel = await message.guild.channels.create(`ticket: ${message.author.tag}`);
      channel.setParent("820276801652916270");
  
      channel.updateOverwrite(message.guild.id, {
        SEND_MESSAGE: false,
        VIEW_CHANNEL: false,
      });
      channel.updateOverwrite(message.author, {
        SEND_MESSAGE: true,
        VIEW_CHANNEL: true,
      });
  
      const reactionMessage = await channel.send("Thank you for contacting support!");
  
      try {
        await reactionMessage.react("????");
        await reactionMessage.react("⛔");
      } catch (err) {
        channel.send("Error sending emojis!");
        throw err;
      }
  
      const collector = reactionMessage.createReactionCollector(
        (reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("ADMINISTRATOR"),
        { dispose: true }
      );
  
      collector.on("collect", (reaction, user) => {
        switch (reaction.emoji.name) {
          case "????":
            channel.updateOverwrite(message.author, { SEND_MESSAGES: false });
            break;
          case "⛔":
            channel.send("Deleting this channel in 5 seconds!");
            setTimeout(() => channel.delete(), 5000);
            break;
        }
      });
  
      message.channel
        .send(`We will be right with you! ${channel}`)
        .then((msg) => {
          setTimeout(() => msg.delete(), 7000);
          setTimeout(() => message.delete(), 3000);
        })
        .catch((err) => {
          throw err;
        });
    },
}

【问题讨论】:

  • 执行文件时好像没有定义message。你能给我们提供执行这个文件的代码吗?
  • 是的...你是指其中一个部分吗? if(command === 'ticket'){ client.commands.get('ticket').execute(message.args); } }) const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js')) for(const file of commandFiles){ const command = require(./commands/${file}) ; client.commands.set(command.name, command); } async execute(message, args, cmd, client, discord) {

标签: discord.js


【解决方案1】:

使用您评论中的信息,您的命令处理程序设置不正确。当您输入execute(message.args) 时,代码会尝试传递消息参数的args 属性,该属性返回undefined

相反,您应该使用execute(message, args) 为您的命令正确传递每个参数。

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2021-03-31
    • 2020-10-15
    • 2021-02-01
    • 2021-07-02
    • 2021-11-13
    • 2021-06-25
    • 2020-06-20
    相关资源
    最近更新 更多