Skip to content

Instantly share code, notes, and snippets.

@Sv443
Last active November 13, 2022 12:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Sv443/1abfd269d15006a8689470cc14946d1b to your computer and use it in GitHub Desktop.
Save Sv443/1abfd269d15006a8689470cc14946d1b to your computer and use it in GitHub Desktop.
Discord.js - Get all invites from all your bot's guilds - only works with Discord.js v11 or lower!
// only works with Discord.js v11 or lower!
const Discord = require("discord.js");
const dev_ids = ["YOUR_ID(s)_HERE"]; // an array of IDs of the bot's developers - only these people will be able to execute this command
// Note: this snippet requires the variables "client" and "message" to work, these need to be provided by your script
var allowedToUse = false;
dev_ids.forEach(id => {
if(message.author.id == id)
allowedToUse = true;
});
if(allowedToUse) {
let invites = ["ignore me"], ct = 0;
client.guilds.forEach(g => {
g.fetchInvites().then(guildInvites => {
invites[invites.length + 1] = (g + " - `Invites: " + guildInvites.array().join(", ") + "`");
ct++;
if(ct >= client.guilds.size) {
invites.forEach((invite, i) => {
if(invite == undefined)
invites.splice(i, 1);
});
invites.shift();
invites.forEach((invite, i) => invites[i] = "- " + invite);
invites = invites.join("\n\n");
let embed = new Discord.RichEmbed()
.setTitle("All Invites:")
.setDescription(invites);
message.channel.send(embed);
}
}).catch(err => {
ct++;
});
});
}
else {
message.reply("this command can only be used by a developer.");
}
@Sv443
Copy link
Author

Sv443 commented Jul 27, 2019

btw the way line 10 should be:
for(let i = 0; i < dev_ids.length; i++) if(message.author.id == dev_ids[i]) allowedToUse = true;

Thanks for the correction! I'd love to edit the gist but the edit form is completely broken for some odd reason. I'll try to remember it and edit it later once it works again.

@anishshobithps
Copy link

anishshobithps commented Sep 17, 2019

btw the way line 10 should be:
for(let i = 0; i < dev_ids.length; i++) if(message.author.id == dev_ids[i]) allowedToUse = true;

Thanks for the correction! I'd love to edit the gist but the edit form is completely broken for some odd reason. I'll try to remember it and edit it later once it works again.

well its an array you could even use for...of loop or array.includes its more easier than this
There are few places you could do that too

@Sv443
Copy link
Author

Sv443 commented Sep 17, 2019

btw the way line 10 should be:
for(let i = 0; i < dev_ids.length; i++) if(message.author.id == dev_ids[i]) allowedToUse = true;

well its an array you could even use for...of loop or array.includes its more easier than this
There are few places you could do that too

GitHub finally let me edit the gist and I implemented both of these

@annajosephjm
Copy link

var invites = ["I am required else it won't work"]
May i know in this what i have to mention, am not getting invites of my bot guilds

@Sv443
Copy link
Author

Sv443 commented Oct 15, 2019

var invites = ["I am required else it won't work"]
May i know in this what i have to mention, am not getting invites of my bot guilds

You don't have to put anything in there, I've just been getting issues with the array being implicitly converted to something else since it was empty so I had to add that. Make sure your client and message variables are present and you added your own user ID to the array at the top.

@igssoap
Copy link

igssoap commented Mar 6, 2020

is this working still in more than 50+ servers? its not working on me and not replying invites in guilds. I have a bot in top.gg thats why

@Sv443
Copy link
Author

Sv443 commented Mar 6, 2020

is this working still in more than 50+ servers? its not working on me and not replying invites in guilds. I have a bot in top.gg thats why

It should theoretically work but I didn't really spend any time testing it.
I might some day rewrite it but I have a whole lot of work to do in my other projects so don't expect it any time soon.

@Anonymyous678
Copy link

Hi how to fix error: 'message' is not defined?

@Sv443
Copy link
Author

Sv443 commented Mar 8, 2020

Hi how to fix error: 'message' is not defined?

You need to provide the message variable. For example:

client.on("message", message => {
	// code here
});

@igssoap
Copy link

igssoap commented Mar 12, 2020

is this working still in more than 50+ servers? its not working on me and not replying invites in guilds. I have a bot in top.gg thats why

It should theoretically work but I didn't really spend any time testing it.
I might some day rewrite it but I have a whole lot of work to do in my other projects so don't expect it any time soon.

Okay i understand

@saad81726354
Copy link

im confused what do i say to get the invites like whats the command sorry im new to coding

@GDMX-dev
Copy link

im confused what do i say to get the invites like whats the command sorry im new to coding

Same , i don't know how to use it

@GDMX-dev
Copy link

im confused what do i say to get the invites like whats the command sorry im new to coding

Same , i don't know how to use it

if(message.author.id == id) this is the commad

@Sv443
Copy link
Author

Sv443 commented May 29, 2020

There is no command, this is the raw code that returns all invites. How you trigger it is up to you. It is recommended to have it in the callback of the client.on("message") method. This snippet broke in the last couple of Discord.js versions so you'll need an older version of Discord.js for this to work.

@TejasLamba2006
Copy link

its saying client is not defined

@Sv443
Copy link
Author

Sv443 commented Apr 10, 2021

@TajuModding like line 7 says, you need to provide the variables yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment