DiscordJS机器人启动时获取所有服务器列表及邀请链接需求
Got it, let's get this sorted for you! Your current code only targets a single server (and you don't even know the ID anyway), so we need to adjust it to loop through all the guilds your bot is in instead. Here's how to do it properly, with attention to Discord.js version differences, permissions, and error handling:
核心思路
You don't need to know individual server IDs—we can directly iterate through all guilds your bot is part of, then generate an invite link for each (as long as the bot has the right permissions).
Discord.js v14+ (Latest Stable Version)
Starting with v14, Discord requires invites to be tied to a specific channel. We'll look for the first valid text/voice channel the bot can create invites for:
// Put this inside your client's ready event client.on('ready', async () => { console.log(`Logged in as ${client.user.tag}!`); console.log('I am in the following servers:'); // Loop through every guild the bot is in for (const guild of client.guilds.cache.values()) { try { // Find the first channel the bot can create an invite for const channel = guild.channels.cache.find( ch => (ch.type === 0 /* Text channel */ || ch.type === 2 /* Voice channel */) && ch.permissionsFor(client.user).has('CreateInstantInvite') ); if (!channel) { console.log(`${guild.name} - No permission to create invites or no valid channel found`); continue; } // Create a permanent invite (adjust maxAge/maxUses if needed) const invite = await channel.createInvite({ maxAge: 0, // 0 = never expires maxUses: 0 // 0 = unlimited uses }); console.log(`${guild.name} - ${invite.url}`); } catch (error) { console.log(`${guild.name} - Failed to create invite: ${error.message}`); } } });
Discord.js v12/v13
If you're using an older version, the API syntax for channel types and permissions is slightly different:
client.on('ready', async () => { console.log(`Logged in as ${client.user.tag}!`); console.log('I am in the following servers:'); for (const guild of client.guilds.cache.values()) { try { // Find valid channel for invite creation const channel = guild.channels.cache.find( ch => (ch.type === 'text' || ch.type === 'voice') && ch.permissionsFor(client.user).has('CREATE_INSTANT_INVITE') ); if (!channel) { console.log(`${guild.name} - No permission to create invites or no valid channel found`); continue; } const invite = await channel.createInvite({ maxAge: 0, maxUses: 0 }); console.log(`${guild.name} - ${invite.url}`); } catch (error) { console.log(`${guild.name} - Failed to create invite: ${error.message}`); } } });
Critical Notes
- Permissions: Your bot must have the
Create Instant Invitepermission in a server to generate an invite. If it doesn't, the code will catch the issue and log a message instead of crashing. - Error Handling: The
try/catchblocks prevent your bot from crashing if something goes wrong (like a server blocking invite creation, or network issues). - Invite Settings: Adjust
maxAgeandmaxUsesin the invite creation options if you don't want permanent, unlimited invites.
内容的提问来源于stack exchange,提问作者Chris ROBLOX Tutorials

