简体   繁体   中英

How do I deal with invalid number and .catch()

I am trying to fix this code but I failed This is a code to send a message to members When I choose any number, it sends an invalid number, and this error appears to me.

I tried to fix it but failed if there is any help I would be grateful. The code was working fine about a month ago, but it has stopped now. I think it has something to do with updating the Discord.js

client.on('message ', async (message) => {
  if (!message.guild || message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;
  if (message.content.startsWith(`${prefix}bc`)) {
    if (!message.member.hasPermission('ADMINISTRATOR'))
      return message.reply('You dont have Permissions.');
    if (message.guild.interval)
      return message.reply(
        '**Another broadcast is running, please wait for it to finish ** '
      );
    const args = message.content.split(' ').slice(1).join(' ');
    if (!args)
      return message.reply(
        '**Please send a message after the command to send it**'
      );

    message.channel
      .send(
        '>>> **[1] All members\n[2] Connected Members\n[3]Special ranks\n[0] Cancel**'
      )
      .then((m) => {
        message.channel
          .awaitMessages((msg) => msg.author.id === message.author.id, {
            max: 1,
            time: 1000 * 60 * 2,
            errors: ['time'],
          })
          .then(async (c) => {
            var members = null;
            if (c.first().content === '1') {
              members = message.guild.members.array();
              c.first().delete();
              m.delete();
            }
            if (c.first().content === '2') {
              members = message.guild.members
                .filter((m) => m.presence.status !== 'offline')
                .array();

              c.first().delete();
              m.delete();
            }
            if (c.first().content == '0') {
              c.first().delete();
              m.delete();
              message.channel.send(
                '**The order has been canceled successfully**'
              );
            }
            if (c.first().content === '3') {
              m.edit('**>>> Please enter the rank name**').then((ms) => {
                message.channel
                  .awaitMessages((msg) => msg.author.id === message.author.id, {
                    max: 1,
                    time: 1000 * 60 * 2,
                    errors: ['time'],
                  })
                  .then((c) => {
                    const role = message.guild.roles.find(
                      (role) => role.name === c.first().content
                    );
                    if (!role)
                      return message.channel
                        .send('**:x:I cannot find the rank for the message**')
                        .then(() => {
                          ms.delete();
                          c.first().delete();
                        });
                    const roleID = role.id;
                    members = message.guild.roles.get(roleID).members.array();
                    c.first().delete();
                    m.delete();
                  });
              });
            }

            if (members == null) return message.reply('**invalid number**');
            if (members.length == 0)
              return message.reply('**The number was not found.**');

            const msg = await message.channel.send(
              `**Sending the message to ... ${members.length} Member...**`
            );
            var count = 0;
            var ycount = 0;
            var xcount = 0;
            message.guild.interval = await setInterval(() => {
              if (!members[count]) {
                clearInterval(message.guild.inter);
                msg.edit(
                  new Discord.RichEmbed()
                    .setDescription(
                      `** :mailbox_with_mail:  ؛ The message has been sent  ${ycount} عضواً\n:lock: ؛ And I could not send the message ${xcount} Member**`
                    )
                    .setTimestamp()
                );
                message.guild.interval = false;
              } else if (!members[count].user.bot) {
                members[count]
                  .send(`${args}`)
                  .then(() => {
                    ycount++;
                  })
                  .catch((err) => {
                    return xcount++;
                  });
              }
              count++;
            }, 500);
          })
          .catch(() => m.delete());
      });
  }
});
(node:841) UnhandledPromiseRejectionWarning: TypeError: message.guild.roles.find is not a function
at message.channel.awaitMessages.then.c (/app/server.js:77:52)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:841) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:841) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

If you working with Discord.Js 12, you have to use cache. So message.guild.roles.cache.find()

Documentation

discord.js v12 and higher use Managers , so you'll have to pass through the cache property.

// change
const role = message.guild.roles.find(
  (role) => role.name === c.first().content
);

// to
const role = message.guild.roles.cache.find(
  (role) => role.name === c.first().content
);


// and change
members = message.guild.roles.get(roleID).members.array();

// to
members = message.guild.roles.cache.get(roleID).members.array();

GuildMemberRoleManager Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM