如何使用 v12+(12.0.0 及更高版本)向 discord.js 中的所有公会发送消息
How do I send a message to all guilds in discord.js with v12+ (12.0.0 and up)
if (command === "sendguildmessages") {
if (message.author.id === "231956829159161856") {
var guildList = client.guilds.array();
try {
guildList.forEach(guild => guild.defaultChannel.send("messageToSend"));
} catch (err) {
console.log("Could not send message to a (few) guild(s)!");
}
} else {
message.reply(`You cant do that!`)
}
} else
我尝试使用 v11.2,但那是 K.O。
它说它已过时,需要更新。我可以用此代码替换什么?
defaultChannel()
已经被弃用并且没有替代品。
并且您需要在发送消息的位置指定通道,但是由于某些服务器具有唯一的通道名称,因此它不会工作......除非它们都具有相同的通道名称并保持不变(有些偷看更改名称一般)。
好吧..我为此编写了一个代码(如果频道名称为 "general" 则有效)
if (command === "sendguildmessages") {
if (message.author.id === "231956829159161856") {
try {
let toSay = "messageToSend"
this.client.guilds.map((guild) => {
let found = 0
guild.channels.map((c) => {
if (found === 0) {
if (c.type === "text") {
if (c.permissionsFor(this.client.user).has("VIEW_CHANNEL") === true) {
if (c.permissionsFor(this.client.user).has("SEND_MESSAGES") === true) {
c.send(toSay);
found = 1;
}
}
}
}
});
});
}
catch (err) {
console.log("Could not send message to a (few) guild(s)!");
}
} else {
message.reply("You cant do that!")
}
}
取自:https://github.com/itsYuuki/SmoreBot/blob/master/commands/control/gann.js
if (command === "sendguildmessages") {
if (message.author.id === "231956829159161856") {
var guildList = client.guilds.array();
try {
guildList.forEach(guild => guild.defaultChannel.send("messageToSend"));
} catch (err) {
console.log("Could not send message to a (few) guild(s)!");
}
} else {
message.reply(`You cant do that!`)
}
} else
我尝试使用 v11.2,但那是 K.O。 它说它已过时,需要更新。我可以用此代码替换什么?
defaultChannel()
已经被弃用并且没有替代品。
并且您需要在发送消息的位置指定通道,但是由于某些服务器具有唯一的通道名称,因此它不会工作......除非它们都具有相同的通道名称并保持不变(有些偷看更改名称一般)。
好吧..我为此编写了一个代码(如果频道名称为 "general" 则有效)
if (command === "sendguildmessages") {
if (message.author.id === "231956829159161856") {
try {
let toSay = "messageToSend"
this.client.guilds.map((guild) => {
let found = 0
guild.channels.map((c) => {
if (found === 0) {
if (c.type === "text") {
if (c.permissionsFor(this.client.user).has("VIEW_CHANNEL") === true) {
if (c.permissionsFor(this.client.user).has("SEND_MESSAGES") === true) {
c.send(toSay);
found = 1;
}
}
}
}
});
});
}
catch (err) {
console.log("Could not send message to a (few) guild(s)!");
}
} else {
message.reply("You cant do that!")
}
}
取自:https://github.com/itsYuuki/SmoreBot/blob/master/commands/control/gann.js