首页 > 解决方案 > Discord.js 错误:无法读取未定义的属性“then”

问题描述

我正在为我的不和谐机器人发出静音命令,目前出现错误;

TypeError: Cannot read property 'then' of undefined

我不知道究竟是什么导致了这个问题,想知道这段代码是否还有更多错误

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');

module.exports = class MuteCommand extends BaseCommand {
  constructor() {
    super('mute', 'moderation', []);
  }

  async run(client, message, args) {
    if(!message.member.hasPermission("MUTE_MEMBERS")) return message.channel.send("You do not have Permission to use this command.");
    if(!message.guild.me.hasPermission("MUTE_MEMBERS")) return message.channel.send("I do not have Permissions to mute members.");
    const Embedhelp = new Discord.MessageEmbed()
    .setTitle('Mute Command')
    .setColor('#6DCE75')
    .setDescription('Use this command to Mute a member so that they cannot chat in text channels nor speak in voice channels')
    .addFields(
      { name: '**Usage:**', value: '=mute (user) (time) (reason)'},
      { name: '**Example:**', value: '=mute @Michael stfu'},
      { name: '**Info**', value: 'You cannot mute yourself.\nYou cannot mute me.\nYou cannot mute members with a role higher than yours\nYou cannot mute members that have already been muted'}
   )
    .setFooter(client.user.tag, client.user.displayAvatarURL());

    let role = 'Muted'
    let newrole = message.guild.roles.cache.find(x => x.name === role);
    if (typeof newrole === undefined) {
      message.guild.roles.create({
        data: {
          name: 'muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })
      .catch(console.log(err)); {
        message.channel.send('Could not create muted role');
      }
    } 
    let muterole = message.guild.roles.cache.find(x => x.name === role);

    const mentionedMember = message.mentions.members.first() || await message.guild.members.fetch(args[0]);
    let reason = args.slice(1).join(" ");
    const banEmbed = new Discord.MessageEmbed()
     .setTitle('You have been Muted in '+message.guild.name)
     .setDescription('Reason for Mute: '+reason)
     .setColor('#6DCE75')
     .setTimestamp()
     .setFooter(client.user.tag, client.user.displayAvatarURL());

   if (!reason) reason = 'No reason provided';
   if (!args[0]) return message.channel.send(Embedhelp);
   if (!mentionedMember) return message.channel.send(Embedhelp);
   if (!mentionedMember.bannable) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == message.author.id) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == client.user.id) return message.channel.send(Embedhelp);
   if (mentionedMember.roles.cache.has(muterole)) return message.channel.send(Embedhelp);
   if (message.member.roles.highest.position <= mentionedMember.roles.highest.position) return message.channel.send(Embedhelp);

   await mentionedMember.send(banEmbed).catch(err => console.log(err));
   await mentionedMember.roles.add(muterole).catch(err => console.log(err).then(message.channel.send('There was an error while muting the member')))

  } 
}

我的猜测是错误与最后一行代码有关,我不确定这段代码是否有更多错误,但我非常想知道并且也愿意接受任何改进代码的建议本身。

标签: javascriptnode.jsdiscorddiscord.jsbots

解决方案


你放then()错地方了。您将执行then()反对add(muterole)(返回一个承诺),或者您可以将其应用到catch()或内catch(),但您将其应用到console.log()不返回任何内容也不是Promise. 尝试以下操作:

await mentionedMember.roles
  .add(muterole)
  .then()
  .catch((err) => {
    console.log(err);
    // You are trying to send an error message when an error occurs?
    return message.channel.send("There was an error while muting the member")
  });

或者

await mentionedMember.roles
  .add(muterole)
  .catch((err) => console.log(err))
  .then(() =>
    message.channel.send("There was an error while muting the member")
  );

希望这会有所帮助!


推荐阅读