[英]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
我认为这与更新 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.如果您使用 Discord.Js 12,则必须使用缓存。 So
message.guild.roles.cache.find()
所以
message.guild.roles.cache.find()
discord.js
v12 and higher use Managers
, so you'll have to pass through the cache
property. discord.js
v12 及更高版本使用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
Documentation GuildMemberRoleManager
文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.