Discord js создание роли
Permissions are Discord's primary feature, enabling users to customize their server's workings to their liking. Essentially, Permissions and permission overwrites tell Discord who is allowed to do what and where. Permissions can be very confusing at first, but this guide is here to explain and clarify them, so let's dive in!
If you want to keep your bot's permission checks simple, you might find it sufficient to check if the member executing the command has a specific role.
If you have the role ID, you can check if the .roles Collection on a GuildMember object includes it, using .has() . Should you not know the ID and want to check for something like a "Mod" role, you can use .some() .
If you want to enhance this system slightly, you can include the guild owner by comparing the executing member's ID with message.guild.ownerId .
To include permission checks like ADMINISTRATOR or MANAGE_GUILD , keep reading as we will cover Discord Permissions and all their intricacies in the following sections.
- Permission: The ability to execute a certain action in Discord
- Overwrite: Rule on a channel to modify the permissions for a member or role
- Bit field: Binary representation of Discord permissions
- Flag: Human readable string in MACRO_CASE (e.g., 'KICK_MEMBERS' ) that refers to a position in the permission bit field. You can find a list of all valid flags on the Permissions.FLAGS
You can provide permission decimals wherever we use flag literals in this guide. If you are interested in a handy permission calculator, you can look at the "Bot" section in the Discord developer portal
Base permissions are set on roles, not the guild member itself. To change them, you access a Role object (for example via member.roles.cache.first() or guild.roles.cache.random() ) and use the .setPermissions() method. This is how you'd change the base permissions for the @everyone role, for example:
Any permission not referenced in the flag array or bit field is not granted to the role.
Note that flag names are literal. Although VIEW_CHANNEL grants access to view multiple channels, the permission flag is still called VIEW_CHANNEL in singular form.
Alternatively you can provide permissions as a property of the CreateRoleOptions
open in new window typedef during role creation as an array of flag strings or a permission number:
open in new window and provide a permission flag, array, or number to check for. You can also specify if you want to allow the ADMINISTRATOR permission or the guild owner status to override this check with the following parameters.
If you provide multiple permissions to the method, it will only return true if all permissions you specified are granted.
You can learn more about the .has() method here.
Permission overwrites control members' abilities for this specific channel or a set of channels if applied to a category with synchronized child channels.
As you have likely already seen in your desktop client, channel overwrites have three states:
- Explicit allow ( true , green ✓)
- Explicit deny ( false , red X)
- Default ( null , gray /)
To add a permission overwrite for a role or guild member, you access the channel's PermissionOverwriteManager
open in new window and use the .create() method. The first parameter is the target of the overwrite, either a Role or User object (or its respective resolvable), and the second is a PermissionOverwriteOptions
Let's add an overwrite to lock everyone out of the channel. The guild ID doubles as the role id for the default role @everyone as demonstrated below:
Any permission flags not specified get neither an explicit allow nor deny overwrite and will use the base permission unless another role has an explicit overwrite set.
You can also provide an array of overwrites during channel creation, as shown below:
To edit permission overwrites on the channel with a provided set of new overwrites, you can use the .edit() method.
To replace all permission overwrites on the channel with a provided set of new overwrites, you can use the .set() method. This is extremely handy if you want to copy a channel's full set of overwrites to another one, as this method also allows passing an array or Collection of PermissionOverwrites
To remove the overwrite for a specific member or role, you can use the .delete() method.
If the permission overwrites on a channel under a category match with the parent (category), it is considered synchronized. This means that any changes in the categories overwrites will now also change the channels overwrites. Changing the child channels overwrites will not affect the parent.
To easily synchronize permissions with the parent channel, you can call the .lockPermissions() method on the respective child channel.
To check your bot's permissions in the channel the command was used in, you could use something like this:
The .permissionsFor() and .permissionsIn() methods return a Permissions object with all permissions set if the member or role has the global ADMINISTRATOR permission and does not take overwrites into consideration in this case. Using the second parameter of the .has() method as described further down in the guide will not allow you to check without taking ADMINISTRATOR into account here!
If you want to know how to work with the returned Permissions objects, keep reading as this will be our next topic.
open in new window object is a discord.js class containing a permissions bit field and a bunch of utility methods to manipulate it easily. Remember that using these methods will not manipulate permissions, but rather create a new instance representing the changed bit field.
discord.js provides a toArray() method, which can be used to convert a Permissions object into an array containing permission flags. This is useful if you want to display/list them and it enables you to use other array manipulation methods. For example:
Additionally, you can serialize the Permissions object's underlying bit field by calling .serialize() . This returns an object that maps permission names to a boolean value, indicating whether the relevant "bit" is available in the Permissions instance.
Some methods and properties in discord.js return permission decimals rather than a Permissions object, making it hard to manipulate or read them if you don't want to use bitwise operations. However, you can pass these decimals to the Permissions constructor to convert them, as shown below.
You can also use this approach for other PermissionResolvable
open in new window s like flag arrays or flags.
The Permissions object features the .has() method, allowing an easy way to check flags in a Permissions bit field. The .has() method takes two parameters: the first being either a permission number, single flag, or an array of permission numbers and flags, the second being a boolean, indicating if you want to allow the ADMINISTRATOR permission to override (defaults to true ).
Let's say you want to know if the decimal bit field representation 268550160 has MANAGE_CHANNELS referenced:
The Permissions object enables you to easily add or remove individual permissions from an existing bit field without worrying about bitwise operations. Both .add() and .remove() can take a single permission flag or number, an array of permission flags or numbers, or multiple permission flags or numbers as multiple parameters.
You can utilize these methods to adapt permissions or overwrites without touching the other flags. To achieve this, you can get the existing permissions for a role, manipulating the bit field as described above and passing the changed bit field to role.setPermissions() .
If you want to compare your code to the code we've constructed so far, you can review it over on the GitHub repository here
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8 contributors
Users who have contributed to this file
- Open with Desktop
- View raw
- Copy raw contents Copy raw contents
Copy raw contents
Copy raw contents
Roles and Permissions
Roles are a powerful feature in Discord, and admittedly have been one of the hardest parts to master in discord.js. This walk through aims at explaining how roles and permissions work. We'll also explore how to use roles to protect your commands.
Let's start with a basic overview of the hierarchy of roles in Discord.
. or actually not, they already explain it better than I care to: Role Management 101. Read up on that, then come back here. I'll wait. (Yeah I know that's cheesy, so sue me).
Let's get down to brass tacks. You want to know how to use roles and permissions in your bot.
Get Role by Name or ID
This is the "easy" part once you actually get used to it. It's just like getting any other Collection element, but here's a reminder anyway!
To get the ID of a role, you can either mention it with a \ before it, like \@rolename , or copy it from the role menu. If you mention it, the ID is the numbers between the <> . To get the ID of a role without mentioning it, enable developer mode in the Appearance section of your user settings, then go to the role menu in the server settings and right click on the role you want the ID of, then click "Copy ID".
Check if a member has a role
In a messageCreate handler, you have access to checking the GuildMember class of the message author:
To grab members and users in different ways see the FAQ Page.
Get all members that have a role
Add a member to a role
Alright, now that you have roles, you probably want to add a member to a role. Simple enough! Discord.js provides 2 handy methods to add, and remove, a role. Let's look at them!
Alright I feel like I have to add a little precision here on implementation:
- You can not add or remove a role that is higher than the bot's. This should be obvious.
- The bot requires MANAGE_ROLES permissions for this. You can check for it using the code further down this page.
- Because of global rate limits, you cannot do 2 role "actions" immediately one after the other. The first action will work, the second will not. You can go around that by using .roles.set([array, of, roles]) . This will overwrite all existing roles and only apply the ones in the array so be careful with it.
Check specific permission of a member on a channel
To check for a single permission override on a channel:
We pass false for the checkAdmin parameter because Administrator channel overwrites don't implicitly grant any permissions, unlike in Roles or when you are the Guild Owner. (The API will allow you to create an overwrite with Administrator, and even tell D.JS that a channel overwrite has had Administrator permissions set. Discord developers have stated this is intended behavior.)
Get all permissions of a member on a guild
Just as easy, woah!
Now get to coding!
ADDENDUM: Permission Names
Click here for the full list of internal permission names, used for .has(name) in the above examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
/* |
NOTE: This gist is no longer supported and won't get updated. |
Please refer to https://github.com/JakyeRU/discord.js-examples for the latest version. |
If you have any issues/suggestions you can open an issue/pull request there. |
*/ |
client . on ( "message" , async message => |
// Checking if the message author is a bot. |
if ( message . author . bot ) return false ; |
// Getting the role by ID. |
const Role1 = message . guild . roles . cache . get ( "RoleID" ) ; |
// Creating a filter. |
const Filter = ( reaction , user ) => user . id == message . author . id ; |
// Creating the embed message. |
const Embed = new discord . MessageEmbed ( ) |
. setDescription ( `Choose a role: $ < Role1 . name >` ) |
// Awaiting for the embed message to be sent. |
const reactionMessage = await message . channel . send ( Embed ) ; |
// Reacting to the embed message. |
await reactionMessage . react ( "😎" ) ; |
// Awaiting a reaction to the embed message. Time is measured in ms. (30000 ms - 30 seconds) |
reactionMessage . awaitReactions ( Filter , < max : 1 , time : 30000 , errors : [ "time" ] >) . then ( collected => |
// Getting the first reaction in the collection. |
const reaction = collected . first ( ) ; |
// Creating a switch statement for reaction.emoji.name. |
switch ( reaction . emoji . name ) |
case "😎" : |
// Checking if the member already has the role. |
if ( message . member . roles . cache . has ( Role1 . id ) ) < return message . channel . send ( "You already have the role." ) >; |
// Adding the role. |
message . member . roles . add ( Role1 ) . then ( message . channel . send ( "Role added!" ) ) ; |
// Breaking the switch statement to make sure no other cases are executed. |
break |
> |
> ) |
> ) ; |
raunakrajadh commented Dec 1, 2020
Thanks Man, It works, I started with Reaction Role from this code, got the basic idea of making a reaction role from your code!
t1mofe1 commented Dec 1, 2020
It works partially, like when I run the command and I react, I get the role, others dont, but if they run it, they do, I dont, how do I fix that so that only me can run the command and everyone gets the role
the filter is checking is the message author id and reaction user id same. (const Filter = (reaction, user) => user.id == message.author.id)
t1mofe1 commented Dec 1, 2020 •
It works partially, like when I run the command and I react, I get the role, others dont, but if they run it, they do, I dont, how do I fix that so that only me can run the command and everyone gets the role
JakyeRU commented Dec 10, 2020
hi jakye i want to ask question that can i make in another file or not because i want in another file we need client.on("message", async message => // Checking if the message author is a bot.
if (message.author.bot) return false
code also when we do in another file ??
A1an-org commented Dec 10, 2020
It does. You just don't know how to set it up.
Alright, Why ichigo >:c
plant-to-flower commented Dec 17, 2020
You have to change the role1 ("RoleId")
TheMutedPlayer commented Dec 22, 2020
It wont work this is the code i have const Discord = require("discord.js");
module.exports = name: "Coolperson",
aliases: ["cool", "Cool"],
args: true, // or false
cooldown: 1, // in seconds
description: "description",
execute(message, args, client) // command code
client.on("message", async message => // Checking if the message author is a bot.
if (message.author.bot) return false;
TheMutedPlayer commented Dec 22, 2020
Nigelrex commented Dec 24, 2020
All This is fine But i wanted it to happen in a specific channel and how do i do that?
JakyeRU commented Dec 24, 2020
All This is fine But i wanted it to happen in a specific channel and how do i do that?
After this tutorial you are able to code a cool bot with many commands and a level system.
Did you ever want to write your own bot but you think you are too bad for it or you don't have motivation to do so? Stop thinking that way. Writing discord bots using discord.js is really, really, easy if you know some javascript basics.
In this tutorial we will use some ES6, which makes javascript more powerful. If you have never heard of ES6, I'll explain some stuff whenever I use it. Found a mistake? Just comment.
Discord.js is a node.js package which requires at least Node.js v8. If you don't know your node.js version, execute node -v in any command line program. I'm not going to explain how to install node.js, because you can just google it. Node.js allows you to run serverside javascript code and the node package manager (npm) provides more than one million packages, free to use.
Enough about node.js, let's get into it. To install the discord.js package, just run npm i discord.js (or npm i discord.js --save if you want to write it automatically in your package.json file) in your bots' directory. Important for windows users: Normally cmd opens in system32, so navigate into your directory using cd (otherwise it'll install that package in system32). Just ignore those warnings, they're not important to us. If installed, create a file - name doesn't matter, I recommend index.js or something that is similar to that and open it with your text editor.
Your first lines of code
Node.js has a method for implementing packages or files: require(); As I mentioned before, ES6 offers much more stuff which makes JS better, for example more datatypes: const and let . If you create a const, you won't be able to modify its value anymore. It is read-only. So let's create a new const for require(); (since we want to make sure that we don't modify it by mistake.)
Now we can login with our application token using client.login() .
Token? What? What token? How do I get one? Go to the discord developer page, login, create an application, create a bot user and copy the token.
If you got your token, call client.token() with your token as parameter.
Warning! Keep your token PRIVATE. It is just like a password.
Congrats! You wrote your first bot. So we want to test if it works and if our bot goes online. Open your command prompt, navigate into bots' folder and type node . ( being the filename of your file that you've created some seconds ago). If nothing is in your console, it works.
Cool! Our bot is online, but . how can we invite our bot to our server? It's actually really simple. Just generate an OAuth2 url on the developer page.
If your bot joined the server, you should see that your bot is online.
Events and handling messages
Some node.js packagss have events. But, what is an event? An “EventEmitter“ takes two parameters: the event type and a function that will be evaluated when the event gets fired. Discord.js has many of these; our most important event is message . It gets fired every time a message is being sent. Here's the syntax for these Event Emitters:
PLEASE don't attach an event emitter for each command. Just use if-statements!
So now you know what events are and what they do. Let's make one to interact with incoming messages.
Oooh, what is going on there? Let me explain: as I said, client has an event of type message , so we use "message" as first parameter. The second parameter looks a bit weird if you've never used ES6. Those things are called “arrow functions“. There's actually no difference between
First command: ping
So we know about eventemitters and other stuff, let's make our first command. First of all, we check if the content of the message equals to !ping . If that condition is true, the bot should send a message with pong! . Here's how it works:
So, as the name says, .send() sends a message to a text channel. You can only call .send() on a text channel (otherwise it'll throw an error and your bot crashes)
By the way: Calling .send() on user objects such as message.author will send a dm to the user. That's how you send private messages.
Congrats another time! You successfully made your first command.
Editing a message
But what if you want to edit a message or delete one? What if you want to pin a message?
You can only edit your own messages, it doesn't matter if you have permissions to manage messages. To edit a message, you call .edit() on a message object with the new message content as parameter:
If you want to send a message and edit it, you have to resolve the promise that .send() returns (or using an asynchronous function) It's uncommon that a method which sends a request doesn't return a promise. Editing it's own message could look like this:
To resolve a promise, call .then on it. That method takes a function as parameter and I used an arrow function. On the docs you can see what a method returns (and want parameter the promise returns, .send() for example returns the message object)
If you use an asynchronous function, you can await that promise.
Other message methods
Guess, there's a .delete() method which deletes the message you call .delete() on. Normally you have to use setTimeout to wait some time, but the delete method offers a timeout parameter which waits an amount of time. Pinning a message is easy too, just call .pin() on the message - and .unpin() unpins a message. Another cool thing you can do is adding reactions on messages. Simply call .react() on the message object. As parameter you use a unicode emoji or a custom emote id.
A real ping command
Let's make a "real" ping command which displays how long it took to send the message Since the message object has a 'createdTimestamp' property - which returns the seconds since 1/1/1970 00:00 AM -, we can substract the current timestamp with message.createdTimestamp
This is really neat, isn't it? But there is much more stuff that I want to show you.
Difference between GuildMember / User Objects
As I said before, there's a huge difference between them. You can only call/read guild-related methods/properties from the member object (message.member) User related stuff like tag, discriminator, avatar has to be called/read on a user object (message.author) Examples:
Of course Discord.js can add roles to members, edit roles and more stuff. GuildMember objects have a .roles property, which returns a Collection (more about collections later) with all roles the member has. To check if a member has a specific role, you use the .roles property and call .has() on that collection. .has() requires a snowflake (it actually takes the key of the element that we used on the extended map, but since they are keyed by ID. ) as parameter. Here's an example to check if a member has a role and if they do, send "Hi"
You can create a role by calling .createRole() on a guild object. That method takes an object as parameter. To give that role a name, use the name property. Also you can set role colors with the color property. A short example on creating a role in current guild
To give a role to a member, call .addRole on a guildmember object. You give
You may have heard of collections (respectively maps) and if you've never heard of them, don't worry. I'm going to explain what collections are. client has a property guilds. This property is a collection. There is actually no big difference between collections and maps (collection extends map). Collections are like Maps, but have specific methods which makes it easy to get objects from it. If you still have no clue what a map is or how they're built. To get a specific object from a map you can call .get() on it. That method takes one parameter: the key
The key of the object is the "title" of the object there are much more methods for maps. Example of mapping:
We have to know what maps are for the next section.
Get guild, user, etc. by ID
Nice, now we know how to send a message to the channel the message was sent in, but what if we want to send a message to a channel in another guild? If you remember, client.guilds returns a collection of guilds the client has access to and since it's an extended map you can call .get() on it to get a specific object from it. If you didn't get it, here is an example:
After calling .get() on it, you can read any property of it that the guild class has (roles, members, . ) Then, read property channels of that guild - which returns a collection of channels the guild has - and call .get() on it again and provide the channel id as parameter.
As I said before, you can call .get() on ANY collection. (docs show if something returns a collection) Also you can check if something is in a collection, for example checking if the message member has a specific role:
It's easy, isn't it?
Catching promise rejections
Sometimes something goes wrong and we normally catch errors with try-catches, but you can't catch Promise Rejections with these try-catches (unless the scope is async). For now (node.js v8.9.4) it will catch it but it's deprecated, means that your process will exit with an edit code that isn't 0 in the future if you continue not catching them.
Catching promises is actually really easy: Just append a .catch() on the Promise. That method takes a function as parameter. If that Promise gets rejected, e.g. a Discord API Error, it'll evaluate the code in your function. Let me give you an example:
I personally don't like that way of catching, but that's how it has to be done. ¯_(ツ)_/¯
Taking arguments - user input
One of the coolest things bots can have are user inputs, like letting the user append something on a command: !ban @User , you know? As you already know, message.content returns a string with the message content. We can split that string by " " using message.content.split(" ") . That returns an array.
So what we could do to get everything after "!ban @User", is splitting the string into an array by whitespace, slicing the array (removes an index) and join that array again to a string.
Bulk deleting (as bot)
You can only delete up to 100 messages at one and try not to delete messages older than 14 days.
bulkDelete doesn't work on user accounts. In the next section we'll discuss how you do it with selfbots.
Bulk deleting (as selfbot)
Note: This no longer works in Discord.js v12+
Warning! Using a selfbot is against the terms of services. Use them on your own risk! I've warned you.
Please don't do that 1000 times a minute, else it's API abuse and there's a good chance that youd get banned. Here's an example on how it works:
Custom welcome messages
What about custom welcome messages? Let"s make one. Discord.js provides an event called guildMemberAdd . That gets fired every time a user joins a guild. It requires one parameter: The guildmember object. So let's create one:
Cool thing, but how can we send a message to the main channel? Well, some months ago, Guild.defaultChannel became deprecated and you can't use it anymore. But with some tricks we can do it. There's a property called 'systemChannel' on Guild. It is the channel where the discord welcome messages will be sent in.
Really cool, isn't it?
There's a guildMemberRemove event which emitts if a user leaves a guild
Did you see these messages with a colored border on the left side? These things are called 'embeds'. Discord.js has an own class for RichEmbeds. To create an instance of it, you call the constructor: new Discord.RichEmbed()
Bonus: Use a JSON file for token and prefix
Do you want to have a custom file where variables are stored? I'll show you how. The key is require() . It let's us implement - as mentioned at the beginning of the tutorial - npm packages and other js/json files. If you want to require js files and call/read properties from it, you have to export it. After you've required the json file, you can read properties from it.
Our json file, located in ./config.json
Bonus: Commands in different files
Everythings fine, but all commands in one file isn't very beautiful. Let's make it a bit more cleaner. Let's create an other file for a command: ping.js Paste your command in that file and export it using module.exports Syntax for exporting a module is:
(You can export variables with module.exports.variableName too.)
In our example it shall look like this:
So you might wonder why we need a parameter in that function. If we wouldn't do that, it'll throw an error, because there's no variable declared as 'message'.
In our main file we will call that module and use message as parameter, so it is defined for that scope.
After exporting the module, we can require it in our main file:
Notice the ./ in require() ? It means that we want a file in the current directory. If we wouldn't use a ./ , it would search for a module in node_modules .
Bonus: Levelsystem with JSON
I'd recommend using SQL for a levelsystem due to corruptions and the way fs works, but we want to keep it simple.
The FS module comes with node.js, means we don't have to install another package. First of all, we want to make sure that bots don't get an entry in our file. Simply put a if(message.author.bot) return; at the beginning of your message event. It is reading property bot of message.author which returns a boolean - whether the author is a bot. And if it's a bot, return. Everything after return wont be evaluated. Create a .json file - name isn't important and put <> in it (so it's a blank object). Now we want to require it to read/write to it. It is automatically formatted as an object, so you don't have to stringify it.
So let's create an object for the message author if there is no.
Make sure that message is defined. Now, we wan't to increase the number of points by 25 every time a message was sent.
After that, we want to write the new object into the json file. FS provided a method called writeFileSync() , which writes something to a file synchronously. Syntax for that is:
Soo, at the end it would look like:
JSON.stringify() stringifies an object. If we don't stringify it, the file would be [object Object] since it implicitly calls toString on the object.
For a more detailed explanation of the notations commonly used in this guide, the docs, and the support server, see here.
Because you cannot ping a user who isn't in the server, you have to pass in the user id. To do this, we use a CommandInteractionOption
open in new window . See here for more information on this topic.
If you would like to set your activity upon startup, you can use the ClientOptions object to set the appropriate Presence data.
Mentions in embeds may resolve correctly in embed titles, descriptions and field values but will never notify the user. Other areas do not support mentions at all.
Controlling which mentions will send a ping is done via the allowedMentions option, which replaces disableMentions .
This can be set as a default in ClientOptions , and controlled per-message sent by your bot.
Even more control can be achieved by listing specific users or roles to be mentioned by ID, e.g.:
If you want to learn more about this syntax or other types of collectors, check out this dedicated guide page for collectors!
You do not need to have a constant local variable like blockedUsers above. If you have a database system that you use to store IDs of blocked users, you can query the database instead:
Note that this is just a showcase of how you could do such a check.
If you want to learn more about reactions, check out this dedicated guide on reactions!
process.exit() will only kill your Node process, but when using PM2
open in new window , it will restart the process whenever it gets killed. You can read our guide on PM2 here.
A User represents a global Discord user, and a GuildMember represents a Discord user on a specific server. That means only GuildMembers can have permissions, roles, and nicknames, for example, because all of these things are server-bound information that could be different on each server that the user is in.
This only works correctly if you have the GUILD_PRESENCES intent enabled for your application and client. If you want to learn more about intents, check out this dedicated guide on intents!
There are two common measurements for bot pings. The first, websocket heartbeat, is the average interval of a regularly sent signal indicating the healthy operation of the websocket connection the library receives events over:
If you're using sharding, a specific shard's heartbeat can be found on the WebSocketShard instance, accessible at client.ws.shards.ping .
The second, Roundtrip Latency, describes the amount of time a full API roundtrip (from the creation of the command message to the creation of the response message) takes. You then edit the response to the respective value to avoid needing to send yet another message:
For this to work, you need to have ytdl-core and @discordjs/voice installed.
Читайте также: