我为自己一直在使用并进行赠品的私有不和谐服务器制作了一个批量dm机器人。因此,我编写了一个机器人程序,可以让所有人(例如:赠品送达或进行游戏比赛等)。每当我运行命令+ damall时,一切都会正常进行,但是当没有我的朋友结识时,机器人发出的dm文件完全停止,并且出现错误。
现在这里是代码:
const { Command } = require('discord-akairo');
const { resolve } = require('path');
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
class RecentCommand extends Command {
constructor() {
super('massdm',{
aliases: ['dmall'],
args: [
{
id: 'ID'
}
],
channel: 'guild'
});
}
async exec(message, client) {
let Owner = message.author;
if(Owner.id !== "727447049892659200") return message.reply("Only the bot owner can use this command!")
let text = message.content.slice('+dmall'.length); // cuts off the /private part
setTimeout(function(){
try {
message.guild.members.cache.forEach(member => {
delay(100);
member.send(text)
})
}catch(e) {
}
}, 1);
return message.channel.send(`dming ${message.guild.members.cache.size} members`)
}
}
module.exports = RecentCommand;
这是当机器人给一个不友善的人时出现的错误:
答案 0 :(得分:0)
某些用户关闭了服务器dms,因此您无法对其进行dm。但是,您可以捕获并忽略这些错误以防止您的进程崩溃:
const { Command } = require('discord-akairo');
const { resolve } = require('path');
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
class RecentCommand extends Command {
constructor() {
super('massdm',{
aliases: ['dmall'],
args: [
{
id: 'ID'
}
],
channel: 'guild'
});
}
async exec(message, client) {
let Owner = message.author;
if(Owner.id !== "727447049892659200") return message.reply("Only the bot owner can use this command!")
let text = message.content.slice('+dmall'.length).split(/ +/); // cuts off the /private part
setTimeout(async function(){
try {
message.guild.members.cache.forEach(member => {
await delay(1_000);
member.send(text.join(" ")).catch(x => { console.log("Couldn't DM " + member.user.tag) })//catch the error and do NOTHING with it.
})
}catch(e) {
}
}, 1);
return message.channel.send(`dming ${message.guild.members.cache.size} members`)
}
}
module.exports = RecentCommand;