I would like to create a channel with specific permissions for #everyone and a created role.
So I defined the role as " rolee " and the #everyone at " everyoneRole ", I would like #everyone not to have permission to see the room, and the rolee created before to have permission to see, talk, log in and talk in the room (category type room, but permission is the same).
My code will surely be more concrete: by the way, discord.js is a bit tricky because it only accepts the code if everything is perfectly aligned, which in itself is not a problem, but is when the script doesn't work because of a single misplaced space.
let everyoneRole = message.guild.roles.cache.find(role => role.name === '#everyone');
let rolee = message.guild.roles.cache.find(role => role.name === rolename);
let crole = (
message.guild.roles.create({
data: {
name: rolename,
color: '3498DB',
},
});
);
let cat = (
message.guild.channels.create(category, {
type: "category",
position: 999,
permissionOverwrites: [
{
id: everyoneRole.id,
deny: [
'VIEW_CHANNEL',
'SEND_MESSAGES',
],
},
]
});
);
And here is the error message.
My problem is that it refuses to add multiple permission to a single role (allow : ['VIEW_CHANNEL', 'SEND_MESSAGES']).
Note: This answer is for Discord.js v13.3.0
The TypeError is occurring because when you're getting the role from the list of roles, #everyone is technically not a role. While it is a role, you can't get it by requesting it from the role cache.
// When you try to get it from this, Discord.js returns null
let everyoneRole = message.guild.roles.cache.find(role => role.name === '#everyone');
This means that you're handing Discord an empty ID which then causes the issue. The proper way to get the role for everyone, as pointed out by Niet the Dark Absol, is to use <Guild>.roles.cache.everyone (Replace <Guild> with whatever your Guild variable is). Here's what that would look like.
let everyoneRole = message.guild.roles.cache.everyone
Related
So I am in the process of making a Discord.Js bot that includes a command that will let me provide information on certain users. For example: I want to add a command that will provide the PlayStation gamer tag of a mentioned user (lets say the specific users id is <#123>). The input message would look something like this :
"!psn #mention" then the bot would output his gamertag which I will manually log as--> message.channel.send('Here is <#1235467890> 's #psnname');
I want to include the gamertag every member in my server so anyone can request it upon mentioning it with the command "psn", I have gone through tons of trial and error with different code but i can not figure out how to specify the message.mention.members.first(); by a specific user id. Please help
module.exports = {
name: 'codtag',
execute(message, args){
let member = message.mentions.members.first();
if(!args.length){
return message.channel.send({embed: {
color: '#da1801',
title: 'Activision Gamertag: Error',
description: 'You need to tag a user dummy.'
}})
}
if (member !== '<#772597378142306354>')return;
else if (member === `772597378142306354`)return
{
(args[0] === member)
return message.channel.send({embed: {
color: '#1243c6',
title: 'Activision Gamertag',
description: 'Here is <#772597378142306354> Activision: \n\n **WalterWhite#2396124**'
}});
}}
}
For anyone that finds this post with the same question, I figured it out. The following code works perfectly
I added:
let guild = message.mentions.members.first();
I also included the condition for args[0] as:
if (message.mentions.members.had('put users id here without the <#>')
module.exports = {
name: 'cod',
execute(message, args){
let guild = message.mentions.members.first();
if(!args.length){
return message.channel.send({embed: {
color: '#da1801',
title: 'Activision Gamertag: Error',
description: 'You need to tag a valid user dummy.'
}})
}
if(message.mentions.members.has('772597378142306354')){
(args[0] == guild)
message.channel.send({embed: {
color: '#1243c6',
title: 'Activision Gamertag',
description: 'Here is <#772597378142306354> Activision: \n\n **WalterWhite#2396124**',
footer: {
text: 'Message #issmayo if your gamertag is not included.'
}
}});
}
I'm using Node.JS to create a discord bot in which a new role and channel are created with the same command. For the sake of simplicity, both the name of the role and the name of the channel are the first argument of the command:
let chanName = args[0];
let roleName = args[0];
Following the name of the role/channel is a list of mentions - these are the users that receive the new role. The role is created just fine, the channel is created just fine, and the members are added to the role just fine, but I'm having trouble setting up permissions where only users with the new role OR the 'administrator' role are able to see the channel.
The channel is created like so:
message.guild.channels.create(chanName, {
type: "text"
})
.then((channel) => {
//Each new channel is created under the same category
const categoryID = '18-digit-category-id';
channel.setParent(categoryID);
})
.catch(console.error);
It appears as though the channel is not added to the guild cache
let chan = message.guild.channels.cache.find(channel => channel.name === chanName);
because this code returns 'undefined' to the console.
Is there any way I can allow the mentioned users to interact (view, send messages, add reactions...) with this new channel?
Permissions are very simple to set up upon creating a channel!
Thanks to the multiple options GuildChannelManager#create() gives us, we're very simply able to add permission overwrites to our desired roles and members.
We could very simply take your code and add a permissionOverwrites section to set the channel's permissions, as follows:
message.guild.channels.create(chanName, {
type: "text",
permissionOverwrites: [
// keep in mind permissionOverwrites work off id's!
{
id: role.id, // "role" obviously resembles the role object you've just created
allow: ['VIEW_CHANNEL']
},
{
id: administratorRole.id, // "administratorRole" obviously resembles your Administrator Role object
allow: ['VIEW_CHANNEL']
},
{
id: message.guild.id,
deny: ['VIEW_CHANNEL']
}
]
})
.then((channel) => {
//Each new channel is created under the same category
const categoryID = '18-digit-category-id';
channel.setParent(categoryID);
})
Well, I'm currently using the emoji :x:, but on my server I have an emoji called :superbotxemoji: I just don't know how I get my bot to use it
My code:
const Discord = require('discord.js');
module.exports = {
name: 'say',
description: 'say',
execute(message, args) {
const { prefix, token } = require('../config.json');
if (!message.member.hasPermission('ADMINISTRATOR'))
return message.channel.send({
embed: {
color: 16777201,
description: `:x: | ${message.author}, You are not allowed to use this command.`,
footer: {
text: ` | Required permission: ADMINISTRATOR`,
},
},
});
if (!args.length)
return message.channel.send({
embed: {
color: 16777201,
description: `:x: | ${message.author}, You need to put a message.`,
footer: {
text: ` | Example: !say hello`,
},
},
});
const sayMessage = args.join(' ');
message.delete({ timeout: 1 });
message.channel.send(sayMessage);
},
};
There is actually a very detailed explanation from the official discord.js guide which you can find here, although I'll try to paraphrase it.
To send a custom emoji, you must get that emoji's unique ID. To find that, you must send the emote in discord with a backslash in front of it; essentially escaping the emoji.
This will result in the emojis unique ID in this format: <:emoji-name:emoji-id>
If you paste this special string into a message, the bot will send the emoji. However, the emoji must be from a guild the bot is part of.
On the other hand, there's another very easy way to get an emoji using the client.emojis.cache collection and the .find() method.
client.emojis.cache.find(emoji => emoji.name === '<name of emoji>')
This method will also make it possible to send custom emojis, however, this time you can find them by name. Be careful, if there are more than one emojis by the given name, it will not work.
A way to bypass this problem would be looking at a guild.emojis.cache collection. This way the amount of possible duplicate emojis would be narrowed down.
When I use a discord bot command, for the purpose of showing the specific person's discord presence, let's say his presence is set on 'Do Not Disturb' a.k.a dnd.
I want this line :
**• Discord Username:** ${user.username},
lets say that specific person is on online, I want it to say 'Online' with a capital 'O', but it shows it as 'online'. Or the same thing for 'dnd', i want it as 'Do Not Disturb'.
I've defined user as : message.mentions.users.first() || message.author
Any ways to help?
const Presence = {
"online": "Online",
"dnd": "Do Not Disturb",
"idle": "Idle",
"offline": "Offline"
}
client.on("message", message => {
if (message.author.bot) return false;
if (message.content.toLowerCase() == "-presence") {
const User = message.mentions.users.first() || message.author;
message.channel.send(Presence[User.presence.status]);
};
});
So, I'm currently working on a "temporary channels" module for my bot. When a user with a certain rank does !newvc, the bot creates a private voice channel that they can use, add people, and when everyone leaves, it gets automatically deleted after some time.
Everything was working fine, but I've noticed a bug that I can't find the reason behind why it happens. Basically, when you first use the command all works fine, the channel is made, you get added and it's moved to the category. But if you use it again, let's say a minute later you don't get added. The channel is made, set as private but you message.member doesn't get added. Then it again does and doesn't, You get the point right?
I honestly can't find a reason why it does it and the only thing I can think of is something to do with Discord's API.
Here's my code
let member = message.member
user = member.user
message.delete()
message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{
id: message.guild.id,
deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}]).then(channel => {
channel.overwritePermissions(member, {
CONNECT: true,
USE_VAD: true,
PRIORITY_SPEAKER: true
})
channel.setParent('567718414454358026')
})
let privatevc = new Discord.RichEmbed()
.setDescription(':white_check_mark: Successfully created a voice channel!')
.setColor(config.green)
message.channel.send({ embed: privatevc }).then(msg => msg.delete(10000))
FYI: My Discord.JS version is 11.4 (didn't had time to update it, due to work)
Firstly, the first 2 lines should be changed to:
let member = message.member,
user = message.author;
// or
const { member, author: user } = message;
as whilst this isn't a problem, in strict mode it will cause an error as you technically do not have a variable keyword in front of user = member.user. You should try and use const if you aren't going to be changing the value of the variables. Note that message.author is the same as message.member.user.
Secondly, using the permissionOverwrites arg in Guild#createChannel is deprecated (see https://discord.js.org/#/docs/main/v11/class/Guild?scrollTo=createChannel). I am aware that Discord.JS has abolished many things despite them saying "deprecated". Try using the typeOrOptions argument to create the channel with the appropriate overrides instead.
Here's my suggested code:
(async () => {
message.delete();
message.guild.createChannel(`⭐ ${message.author.username}'s Room`, {
type: 'voice',
parent: '567718414454358026',
permissionOverwrites: [{
id: message.guild.id, // #everyone has the ID of the guild
deny: ['VIEW_CHANNEL', 'CONNECT'],
}, {
id: message.author.id, // attach the permission overrides for the user directly here
allow: ['VIEW_CHANNEL', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER']
}]
});
const embed = new Discord.RichEmbed()
.setDescription(':white_check_mark: Successfully created a voice channel!')
.setColor(config.green);
const sentMessage = await message.channel.send(embed);
sentMessage.delete(10 * 1000);
})();
I found the issue. Basically, because the user was added after the channel is created, Discord API was losing it (or something, these are only my guess atm).
After changing it to this:
message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{
id: message.guild.id,
deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}, {
id: message.author.id,
allow: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}])
Everything works again. Thank you PiggyPlex.