How can I get roles and channels of a guild - discord.js

I am fairly new to making discord bots and I am making a !serverinfo command. The problem I have is that I cant get the channels and the roles, and the roles cannot be clickable(colored)

You can use <Guild>.roles.cache or <Guild>.channels.cache to return the respective manager. Replacing <Guild> with your guild object, most likely you will use <Message>.guild
Example
message.guild.channels.cache
// Will return Collection [Map] of all the guild's channels
message.guild.roles.cache
// Will return Collection [Map] of all the guild's roles

Related

Discord bot issuing roles based on member usernames

I'm creating a bot for the purpose of Discord moderation, one of the components of this bot is the following task:
"When every new user comes to server, his role by default will be #NotMember, but when he changes his server nickname to the "Nickname | (Real name)", for example, CoolPerson (Alex), then his role is automatically changing to the #Member".
The one way of doing this that I can see is to check if the username contains the brackets, if not then his role is old #NotMember.
Is there any another way to detect if server members have changed their name to a nickname? And is this actually possible?
I'm making this bot in JavaScript, but Python is also welcome here.
You can use the guildMemberUpdate event that is triggered whenever a member changes their name, with this you will have access to the guild member and their old/new username. Note that your bot will need to have the Server Members Intent
enabled on the developer portal.
Example in Discord.js:
client.on("guildMemberUpdate", function(oldMember, newMember) {
// oldMember is the old guild member object before the change, you
// can use it to fetch their old username.
// Add a role to the newMember object.
newMember.roles.add(YOUR_ROLE_HERE);
});

Discord API - Get a list of all the servers a bot is part of

I created a Discord bot that, once invited to a guild, is able to make API calls such as
GET/guilds/{guild.id}/members (https://discord.com/developers/docs/resources/guild#list-guild-members)
But how I do get the guild.id beforehand? Is it possible to make an API call to get a list of all the servers a bot is part of?
The client you create will have a guilds attribute you can use to access the servers it's in. This will be in the form of a GuildManager object which has a fetch() method to get a collection of guild objects.
Here is an example that gets all of a bot's guilds and prints their ids:
await bot.guilds.fetch()
.then(guilds => {
guilds.forEach(guild => {
console.log(guild.id);
});
});

How to remove roles from a user in every server? Discord.py

I am making a verification bot and I want to do a command where if somebody unlinks their Roblox account then it removes the verified role from them in all the servers they are in. I know how to do it in a single server like this:
role1 = discord.utils.get(ctx.guild.roles, name='Verified')
await ctx.author.remove_roles(role1)
But how would I do it across all of the servers the member is in with the bot. Thanks!!!
A role is unique per guild, you'd need to iterate through every guild, get the member and role, and remove it.
for guild in bot.guilds:
member = guild.get_member(member_id) # Change the ID accordingly
if member is not None:
role = discord.utils.get(guild.roles, name='Verified')
await member.remove_roles(role)
This code loops through every guild the bot is in, tried to get the member, if it's not a nonetype (cause the member doesn't have to share that specific guild with the bot), it gets the role obj and removes it.
Reference:
Bot.guilds
Guild.get_member
Also you need intents.guilds and intents.members

How to get #everyone role ID?

I need to get #everyone role ID to create a private chat for to server members (I'll restrict others except for those 2 users from reading and sending messages). How?
I tried typing
\#Admin
to get Admin's role ID as an example.
But then I typed
\#everyone
and it creates a regular message mentioning everyone on the server (like typing #everyone in chat). No ID.
How do I get #everyone role ID?
The ID for the everyone role is the Guild ID.
If you try to do <#&123123> replacing 123123 with your guild id, it will almost tag everyone
Update: 10th April 2020
Now to mention everyone, all you need is to send the string #everyone: message.channel.send("#everyone");
To obtain the ID for the everyone role, you need to have the guild, which you get on some events like: message, guildCreate, and others.
From there: <something>.guild.roles.everyone, and with that you should be able to get the ID with <something>.guild.roles.everyone.id.
If you don't have access to the guild on a event, you can get the guild by it's ID with something like:
client.guilds.cache.get('guildID').roles.everyone.id, if you know that it will be in cache. If you want to be safe, you can do it like this:
client.guilds.fetch('guildID').then(guild => {
let id = guild.roles.everyone.id;
// do something with id
}
or with await:
let guild = await client.guilds.fetch('guildID');
let id = guild.roles.everyone.id;
Need to get #everyone Role?
<guild>.roles.everyone
It will get a role object. You can check that in Role Class
Discord.js
V12
The guild class has a roles property, which is a RoleManager.
This manager has the property everyone:
The #everyone role of the guild
You can get its id with the id property of the role class:
client.on('message', (msg) => {
console.log(msg.guild.roles.everyone.id);
});
V11
In V11 the guild class has a defaultRole property.
So the code is slightly different:
client.on('message', (msg) => {
console.log(msg.guild.defaultRole.id);
});
Discord
On Discord side, with dev option activated, you can right click in almost everything to grab it ID. For example go in the server settings, in role and right click on a role, you should have a copy ID option.

Using guilds and channels without needing to read a message

I'm hosting my bot on glitch.com, but it restarts every night.
When my bot gets started, it reads a message from a specific channel and then writes in the others. The problem with this is that I can't use any guild or channel until the bot receives a message.
How can I avoid doing this?
You can use guild and channel IDs. Every element in Discord has a unique ID (called Snowflake) that identifies it.
If you want to get a specific guild or channel, you can save its ID and use it in the code. IDs are public, so there's no risk using them (they're not like passwords or tokens).
Guilds and channels are stored in Collections and mapped by their IDs, so you can use them like this:
let guild = client.guilds.get('guild id as a string');
let channel = guild.channels.get('channel id as a string');
To get your guild's ID (or the ID of pretty much any element in Discord) you can enable the Developer mode in the settings, then right-click on the guild and select "Copy ID".

Resources