使用 Discord.js 获取所有公会 ID

2022-01-10 00:00:00 discord javascript discord.js

所以我想从我的机器人所在的服务器上获取每个 ID.请以最好的方式列在一个列表中,这样我就可以一个接一个地获得一个 ID,因为我的前缀系统需要它们.我尝试了很多设计,但都无法正常工作.

So I want to get every ID from the servers my bot is on. In the best way please in a list so I could get one ID after another because I need them for my prefix system. I tried a lot of designs but it didn't work right.

推荐答案

你可以使用 Collection#map 映射 Client.guilds.cache (GuildManager#cache) 按 ID.

You can use Collection#map to map Client.guilds.cache (GuildManager#cache) by ID.

const Discord = require("discord.js");
const client = new Discord.Client();

client.on("ready", () => {
    const Guilds = client.guilds.cache.map(guild => guild.id);
    console.log(Guilds);
});

client.login(process.env.DISCORD_ACCESS_TOKEN)

Discord JS V13

const Discord = require("discord.js");
const client = new Discord.Client({
    intents: ["GUILDS"],
});

client.on("ready", () => {
    const Guilds = client.guilds.cache.map(guild => guild.id);
    console.log(Guilds);
});

client.login(process.env.DISCORD_ACCESS_TOKEN);

相关文章