因此,我有一个有效的代码示例,该示例将DM发送给行会的所有成员。 但是,我希望整个过程以一条消息结尾,例如:“成功发送XX条消息”。
const sentMessages = 0;
if (!message.member.hasPermission(`ADMINISTRATOR`)) {
message.channel.send(`You don't have permission to use this command!`);
message.client.channels.cache.get(logchannel).send(`${message.author} tried using DMALL! ` + dato.toLocaleTimeString() + dato.toLocaleDateString());
return;
} else {
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
const sendMessage = args.join(" ");
message.channel.send(`Sending messages, please wait...`);
await delay(1000);
let interval = 500; // how much time should the delay between two iterations be (in milliseconds)?
let promise = Promise.resolve();
message.guild.members.cache.forEach(function (user) {
promise = promise.then(function () {
if (user.id != message.client.user.id) {
user.send(sendMessage);
sentMessages++;
return new Promise(function (resolve) {
setTimeout(resolve, interval);
});
}
});
message.channel.send(`Successfully sent ${sentMessages} messages!`);
});
无论何时何地放置,结尾的消息(成功...)始终返回0。 如果我在 sentMessages ++ 下面放一个这样的行: message.channel.send(`Sent:$ {sentMessages}); ; 我会得到一个期望的递增数字。>
我在哪里做错什么?
答案 0 :(得分:1)
最终找到了一个比我更擅长编码的朋友。 他想出了一种完全不同的到达目的地的方式:
if (!message.member.hasPermission(`ADMINISTRATOR`)) {
message.channel.send(`You don't have permission to use this command!`);
message.client.channels.cache.get(logchannel).send(`${message.author} tried using DMALL in **${message.guild.name}**!` + dato.toLocaleTimeString() + dato.toLocaleDateString());
return;
} else {
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
const sendMessage = args.join(" ");
message.channel.send(`Sending messages, please wait...`);
await delay(1000);
const interval = 500;
let array;
try {
const all = await message.guild.members.fetch();
array = all.array();
} catch {
array = message.guild.members.cache.array();
}
let sentMessages = 0;
for (var i = 0; i < array.length; i++) {
const user = array[i];
if (!user.bot) {
try {
await user.send(sendMessage);
sentMessages++;
await delay(interval);
} catch { }
}
}
message.client.channels.cache.get(logchannel).send(`${message.author} sent a DM to all members of: **${message.guild.name}**!` + dato.toLocaleTimeString() + dato.toLocaleDateString());
message.channel.send(`Finished sending messages to all users, ${sentMessages} messages sent!`);
}
此代码经过测试,发现可以正常工作
答案 1 :(得分:0)
使用let sentMessages = 0
代替const sentMessages = 0
常量无法更新或重新分配,因此sentMessages++
不会做任何事情
答案 2 :(得分:-1)
如果您不想成为Discord的禁令,请不要这样做,禁止将私人消息发送给行会的所有成员。