How do I find a role with a specific permission? - javascript

So I'm trying to create a discord bot and one of the commands for the bot needs me to filter roles by the permissions they're allowed (or, at least this would be the easiest way), and I can't seem to figure out how to do that. I've tried way too many things to even count, and at this point, I'm looking for a different way. But if anyone has the answer to this, that would be amazing.
Basically I wanna do this:
let staff = message.channel.guild.roles.cache.filter(s => s has this permission)
(Pseudo-code obviously)
But as I said, I just can't seem to figure it out.
case 'staff':
let staff = chan.guild.roles.cache.filter(m => m.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES));
//console.log(staff)
message.guild.channels.create(Channel, 'text').then (
function() {
let ch = message.guild.channels.cache.find(ch=>ch.name === Channel)
ch.updateOverwrite(message.guild.id, { VIEW_CHANNEL : false });
ch.updateOverwrite(staff, { VIEW_CHANNEL : true })
}
)
type.stop()
break;
Also, 'chan' is a shortened version of 'message.channel'

Is this what you're looking for?
let rolesWithManageMessagesPermission =
message.channel.guild.roles.cache.filter(r => r.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)

Related

I want to set when someone mention me bot will respond

I'm trying to make my discord.js bot send messages when someone pings me. How can I do that??
I was trying with that code:
if(message.content === "<#723821826291138611>") {
message.channel.send("Hello, sup? ")
}
but that doesn't work. how can I do that?
The best - and most conventional - way to do this is to check the MessageMentions#(users|members) collection.
const {
mentions:{
users,
repliedUser
}
} = message;
if (users.has("723821826291138611") && !repliedUser) {
// Your code
}
This will return true if the mentioned is found in any order not only first, it's possible for the API to emit the mentions in different orders if multiple mentions were given. I wouldn't recommend searching the message.content string either.
You have to use if(message.content.includes("<#723821826291138611>")) instead of if(message.content === "<#723821826291138611>") to make it work!
But also you can use this code to do it:
let mentioned = message.mentions.members.first();
if(mentioned && mentioned.id == "723821826291138611") {
if(message.mentions.repliedUser) return;
message.channel.send("YOUR_TEXT")
}

Why is guild.members.fetch timing out

Hello so I am writing a discord bot and all I want is an ordered list of members ordered by their ids. To achieve this I am running following code, however it just console.logs "Couldn't fetch members", with no further errors. :(
Upon further inspection, I discovered that it is a timeout error. Everything else works fine and I think that the const guild isn't the problem, but other than that hope that you people can help, Cheers!
const IDs = new Map();
var repeat = new Boolean(false);
var randomInt = new Number(0);
client.on('ready', () => {
console.log('Ready!');
const guild = client.guilds.cache.get("xxxxxxxxxxxxxxxxxx");
guild.members.fetch().then(members => {
console.log("Found the members");
IDs = members.map(user => user.id);
const OnlineMembers = members.filter(member => member.presence.status == "online");
}).catch(e => console.log("Couldn't fetch members."));
});
P.S. This is running on a Raspberry Pi over Nodemon --inspect, in case that changes anything
I found the solution, sorry if I bothered anybody, there is this really small setting in the Discord Dev Hub. That enables or disables this feature, obviously I was to dumb to turn it on from the start, thank you to anybody who took some time for this, Cheers!

i keep getting an error about .position in discord.js

So i tried making a kick command in dicors.js(creating a bot). I did a little bit of research and I ended up with copying a code from stack overflow. The code was working fine but i wanned to make it so if "x" that has a lower rank tries to kick "y" which has higher rank, the bot would then send a message to "x" that he can't kick a higher rank person. (both x and y are users)
The code that i copied also has this function included and i keep getting this error
let authorHighestRole = msg.member.highestRole.position;
^
TypeError: Cannot read property 'position' of undefined
I searched online: nothing.
I tried some stupid things like adding () to .position
I searched in the Discord.js docs and still nothing.
I came to the conclusion that the code is old.
Here is my code
case 'kick' :
if(msg.channel.type === 'DM') {
msg.channel.send('This command can use only in guide');
return;
};
if(!msg.member.hasPermission('KICK_MEMBERS')) {
msg.channel.send('You have no permissions to do that');
return;
};
let mentionMember = msg.mentions.members.first();
if(!mentionMember) {
msg.channel.send('Please specify the person you want to kick!');
return;
}
if(!mentionMember.kickable) {
msg.channel.send('I can\'t kick this user!');
return
};
//Get the highest role of user for compare
let authorHighestRole = msg.member.highestRole.position;
let mentionHighestRole = mentionMember.highestRole.position;
//If mention user have same or higher role, so show this error msg
if(mentionHighestRole >= authorHighestRole) {
msg.channel.send('You can`t kick members with equal or higher position');
return;
};
mentionMember.kick()
msg.channel.send(`${mentionMember.displayName} was kicked`)
break;
Any help would be appreciated.
member doesn't contain a property called highestRole, I believe the property you're looking for is: member.roles.heighest.
see docs: https://discord.js.org/#/docs/main/stable/class/GuildMemberRoleManager?scrollTo=highest
msg.member.roles.highest.position

I want to make a Discord.js reaction collector, counter, timer and role dm

So I made a suggestion command which sends an embed with the suggestor name and the suggestion to a specific channel. It then reacts with 👍 and 👎.
How can I make a collector, that counts the reactions and after e.g 1 week it stops it and DMs everyone in a specific role (e.g Staff) with the results and edits the embed in the channel saying Suggestion accepted/denied.
Also, how can I make a collector that runs a command with permissions (roles e.g
Staff)
I know I am asking for a lot, but I looked at discord.js.org's documentation and I couldn't understand it.
Thanks for taking off your time to help me!
Here is my code so far:
if (command === 'suggest') {
if (!args.length) {
const suggestHelp = new Discord.MessageEmbed()
.setTitle('Suggestions')
.setDescription('Help')
.addField('Usage', 'The easiest way for community to rate suggestions!')
.addField(
'Commands',
'`/suggest` Displays info about the suggestions \n `/suggest <suggestion>` Make a suggestion, which can be rated by the community'
)
.setFooter('Powered by Monebot')
.setColor(0x2e86c1);
message.channel.send(suggestHelp);
return;
} else if (args.length) {
let suggestor = message.author.username;
let suggestionText = args.slice(0).join(' ');
const suggestEmbed = new Discord.MessageEmbed()
.setDescription(suggestionText)
.setColor(0xf5b041)
.setAuthor(`Suggestion from ${suggestor}`);
//.setThumbnail('https://i.imgur.com/wSTFkRM.png')
client.channels.cache
.get('ID')
.send(suggestEmbed)
.then((messageReaction) => {
//Setup 1 Channel ID for suggestions
messageReaction.react('👍');
messageReaction.react('👎');
});
}
}
You are not specific with what you want us to help you with.
You're asking us too much to do in one time and you litterally gave us you code and asked us to change it ^^'
But i get what you are trying to do so i suggest you to search in a discord.js guide wich is extremelt helpful in this case
If i helped you can approuve this suggestion, if not please edit yours so that we can help you.
Markdown would be helful
And i have the same question as ortogonal
What are you having issues on? I would suggest you try to make everything you listed before coming to here, do you have an error?

connection.playStream is not a function

I know that there are a lot of question on this topic, but I browsed all of them but none of the answers seemed to fix my issue. I'm just creating a Discord bot for fun, I have almost never programmed in JavaScript but I wanted to try it.
My Code looks like this:
var url = "https://www.youtube.com/watch?v=HHv-s2OqYpw"
if(!message.member.voice.channel){
message.channel.send("Join a voice channel.");
return
} else {
vc = message.member.voice.channel;
connection = await vc.join();
isValid = ytdl.validateURL(url);
if(!isValid){
message.channel.send("The url you gave doesn't exist");
} else {
const stream = ytdl(url, {filter: "audioonly"});
const dispatcher = connection.playStream(stream)
dispatcher.on("end", function() {
vc.leave()
message.channel.send("Done playing the only music I can play lol")
})
I could do that it could play more songs but I only want it to play this one and in the terminal i get this error:
connection.playStream is not a function
I do have ytdl-core and opusscript installed and they both have the newest version. I tried doing this in lots of different ways, and it really annoys me that I can't figure out what the problem is. Sorry for bad english and thank those who help me. Have a nice day!
https://discord.js.org/#/docs/main/stable/class/VoiceConnection?scrollTo=play
As you can see, there's a VoiceConnection#play() method, and since you're most likely on v12, this is the method you're looking for.
const stream = ytdl(url, options);
const dispatcher = connection.play(stream);

Categories

Resources