我一直想知道当有人邀请我的机器人加入他们的服务器时如何发送消息。
请在这方面帮助我,我不知道 Python 中有它,但我还没有习惯它。
感谢您的帮助
最佳答案
以上所有答案都假设您对服务器有所了解,但在大多数情况下您并不了解!
你需要做的就是循环浏览公会中的 channel ,找到一个你有权发短信的 channel 。
您可以从公会获取 channel 缓存。 以下面的例子为例:
bot.on("guildCreate", guild => {
let defaultChannel = "";
guild.channels.cache.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
})
//defaultChannel will be the channel object that the bot first finds permissions for
defaultChannel.send('Hello, Im a Bot!')
});
您当然可以做进一步的检查,以确保您可以在发短信之前发短信,但这将使您走上正确的道路
更新discord.js 12+ Discord.JS 现在使用缓存 - 这里是 12+ 的相同答案
bot.on("guildCreate", guild => {
let found = 0;
guild.channels.cache.map((channel) => {
if (found === 0) {
if (channel.type === "text") {
if (channel.permissionsFor(bot.user).has("VIEW_CHANNEL") === true) {
if (channel.permissionsFor(bot.user).has("SEND_MESSAGES") === true) {
channel.send(`Hello - I'm a Bot!`);
found = 1;
}
}
}
}
});
})
关于javascript - 如何在 Discord.js 事件 "guildCreate"中发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232626/