Discord.js仅从1台服务器过滤Discord事件



我正在努力只过滤一台服务器的事件,而不是机器人所在的所有服务器,但我正在努力找出如何将机器人所在的每个公会准确地保存为一个集合,这样我就可以根据我想要的公会ID过滤事件。这可能吗?这是我迄今为止的一段代码,它可以显示服务器名称和机器人当前所在的ID,事件正常工作,但会触发所有服务器,而不是我想要的服务器,我该如何过滤一个公会收藏?

const Discord = require('discord.js')
const bot = new Discord.Client()
const config = require('./config.json')
bot.on('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
//Sets Activity
//client.user.setStatus('invisible')
bot.user.setActivity("Discord Cooks", { type: "WATCHING"})
console.log("Set User Activity!");
//Online Webhook
const webhookClient = new Discord.WebhookClient('','');
const embed = new Discord.MessageEmbed()    
.setTitle(`${bot.user.tag} is online`)
.setColor('#FFFF00')
.setTimestamp()
webhookClient.send(embed);

bot.guilds.cache.forEach((guild) => {
console.log(guild.name, guild.id);
});
});
bot.on("channelCreate", (channel) => {
console.log(`channelCreate: ID: ${channel.id} Name: ${channel.name}`);
});
bot.on("channelUpdate", (oldChannel, newChannel) => {
console.log(`channelUpdate -> ${oldChannel.name} to ${newChannel.name}`);
});
bot.on("channelDelete", (channel) => {
console.log(`channelDelete: ID: ${channel.id} Name: ${channel.name}`);
});
bot.login(config.bottoken)

只有当事件发生在确切的服务器上时,您才能以更简单的方式执行代码:

if(guild.id == "GUILD_ID") {
// code
} 

正如@MrMythical所说,如果你只需要为1个公会运行你的代码,你就可以使用if (message.guild.id !== "GUILD_ID") return;

最新更新