如何处理无效数字和.catch()



我试图修复此代码,但失败了这是一个向成员发送消息的代码当我选择任何号码时,它会发送一个无效的号码,这个错误就会出现在我面前

我试着修复它,但失败了。如果有任何帮助,我将不胜感激。大约一个月前,代码运行良好,但现在已经停止。我认为这与更新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 membersn[2] Connected Membersn[3]Special ranksn[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.

如果您使用Discord。Js 12,你必须使用缓存。所以message.guild.roles.cache.find()

文档

discord.jsv12及更高版本使用Managers,因此必须通过cache属性。

// 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文档

最新更新