【问题标题】:Discord.js Error: Cannot read property 'then' of undefinedDiscord.js 错误:无法读取未定义的属性“then”
【发布时间】:2021-07-10 18:14:46
【问题描述】:

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

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')))

  } 
}

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

【问题讨论】:

    标签: javascript node.js discord discord.js bots


    【解决方案1】:

    您将then() 放在错误的位置。您可以针对add(muterole)(返回一个承诺)执行then(),或者您可以将其应用于catch()catch(),但您将其应用于console.log(),它不会返回任何内容,也不是@ 987654327@。请尝试以下操作:

    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")
      );
    

    希望对您有所帮助!

    【讨论】:

    • @AlexanderStaroselky 此代码有效,但我收到另一个错误Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes. 我的猜测是它与 ``.add(muterole) 行有关,如果您知道修复的方法这个也请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2021-02-06
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多