discord.js lock a channel for #everyone - discord.js

When I try to remove SEND_MESSAGES permission from #everyone on a channel, it gives me this error: TypeError [INVALID_TYPE]: Supplied overwrites is not an Array or Collection of Permission Overwrites. And I don't know what to do. This is my code:
let channel = message.channel;
let roles = message.guild.roles;
let testRole = roles.fetch('#everyone');
channel.overwritePermissions(
testRole, { 'SEND_MESSAGES': false },
'string'
);
message.channel.send("Successfully locked channel.");

Nevermind, I got a solution:
let role = message.guild.roles.cache.find(r => r.name === "#everyone");
message.channel.overwritePermissions([{
id: role.id,
deny: ["SEND_MESSAGES"]
}]);

Related

Give role in specific message sent

So I want to give a role for sending specific number of message
Like if they manage to send 1k messages they will get a specific role
Can anyone help me on that?
So if you know the role id then
roleID = "100000000000000001";
let member = message.mentions.members.first();
if(memberHasEnoughMessage && !member.roles.get(roleID)){
let role = Guild.roles.cache.get(roleID);
if(member && role){
member.roles.add(role);
}
}
if you don't know the role id and only know the name then
const role = Guild.roles.cache.find(role => role.name === '10k message');
If you want to create a new role (let's say with permission to ping everyone) then
Guild.createRole( {name:"10k", color: "#f6f61", permissions:["MENTION_EVERYONE"] } );
So the final code should be like
roleID = "100000000000000001";
let member = message.mentions.members.first();
if(memberHasEnoughMessage){
/*
If you already know the role ID
let role = Guild.roles.cache.get(roleID);
*/
let role = Guild.roles.cache.get(roleID);
ler role = Guild.roles.cache.find(r=> r.name === '1k');
if(!role){
await Guild.createRole( {name:"Mentioners", color: "#ff0000", permissions:["MENTION_EVERYONE"] } ).then( (newRole) =>
role = rnewRole;
}).catch(console.log);
}
if(member && role){
member.roles.add(role);
}
}

How to know if a user already has a role in an addrole command

I want to send an error message saying that the user already has the role if they do in an addrole command.My current command is:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "addrole",
description: "adds a role to a member",
execute: async (message , args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Invalid Permissions")
let member = message.mentions.members.first();
let role = message.mentions.roles.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!role) return message.reply("please mention a valid role of this server");
const highest = message.member.roles.highest;
if(highest.comparePositionTo(role) <= 0)
return message.channel.send(`You cannot add roles equal/higher to that member's highest role`)
let addroleembed = new MessageEmbed()
.setTitle(`:white_check_mark: ${role.name} has been added to ${member.user.tag}`)
.setFooter(`Action performed by ${message.author.tag}`)
.setColor('77B255')
addroleembed.setTimestamp();
member.roles.add(role);
message.channel.send(addroleembed)
}}
I have tried using
if(!message.member.roles.cache.some(r=>[`${role.name}`].includes(r.name)))
and
if (member.roles.has(role.id) {
But neither of these seem to work, Anyone know the reason behind this? If I can't use either of these methods, Can you suggest me a solution?
You are so close!
All you need is cache when using has()
member.roles.cache.has(<RoleID>);
Learn More Here
You need to combine your two tries.
if(message.member.roles.cache.has("role ID") {
//rest of your code
}

Finding Members with 2 Roles in Discord [Using Discordjs]

I am making a discord bot using discord.js, and what I want to do is to have the bot find members with Roles A and B, and list them in an embedded message. Currently, I have:
else if (message.content === (!'guns') && message.channel.id == '732740415056380044') {
let team = message.guild.roles.find('name', 'WindStar Team');
let awper = message.guild.roles.find('name', 'AWPer');
let rifler = message.guild.roles.find('name', 'Rifler');
const Members1 = awper = awper.filter(val => !team.includes(val)).map(member => member.displayName);
const Members2 = rifler = rifler.filter(val => !team.includes(val)).map(member => member.displayName);
const embedmesage = new Discord.MessageEmbed()
.setTitle('Preferred Guns')
.setColor(0x00AE86)
.addFields(
{ name: 'AWPers', value: `${Members1.join('\n')}` },
{ name: 'Riflers', value: `${Members2.join('\n')}` },
);
message.channel.send(embedmesage);
return console.log('Preferred gun roles command for windstar team executed');
}
Basically, I am trying to cross-check members who have the WindStar Team role and AWPer role, and send the list of all members who have those roles into an embed. I am also trying to cross-check members with the WindStar Team role and the Rifler role, and send the list of those members into an embed.
let awper = message.guild.roles.find('name', 'AWPer');
awper.members //Either .toArray() or .map(m => m.displayName)
I think, I dont have my bot in front of me to test
I think it is maybe too late at this point but I'll give my solution anyway. (using discord.js v12.5.1)
<Message> = your message instance
let roleMembers = <Message>.guild.roles.cache.filter(role => role.name === "AwPer" && role.name === "WindStar").members
// displaying the role members
roleMembers.map(member => member.user.username)
Basically you are getting all the roles in a guild a cache and filtering them to display the roles you want.
This should work ^. Unless you are using a breaking version of discord.js

Bot Crashed When you dont mention

I have this command on my bot where you can mute someone using the command g!mute where the 'user' is mentioned using #. However, if you don't mention e.g GeoGeo instead of #GeoGeo, it causes the bot to crash. I know you need to put .catch(console.error); somewhere, but I'm not sure where. Thanks in advance. The Error is
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
^
TypeError: message.guild.members.get is not a function
Code:
const Discord = require('discord.js');
const ms = require('ms');
module.exports = {
name: 'mute',
description: "this is mute command",
execute(message, args){
if(!message.member.roles.cache.find(r => r.name ==="Staff", "Head Staff", "Owner", "Co-Owner")) return message.channel.send(`YOU DO NOT HAVE PERMISSION TO DO THAT`)
let members = args[0];
if(!members) return message.reply("g!mute <user> <time>")
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("That person is not in the server!");
let mainrole = message.guild.roles.cache.find(role => role.name === "Fans");
let muterole = message.guild.roles.cache.find(role => role.name === "muted");
if(!muterole) return message.reply("That role does not exist");
let time = args[1];
if(!time){
return message.reply("g!mute <user> <time>");
}
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has now been muted for ${ms(ms(time))}`)
.setColor(0x01B8FF)
message.channel.send(embed);
setTimeout(function(){
person.roles.add(mainrole.id)
person.roles.remove(muterole.id)
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has been unmuted`)
.setColor(0x01B8FF)
message.channel.send(embed);
}, ms(time));
}
}
When faced with an error like: TypeError: message.guild.members.get is not a function
The logical thing to do is check the docs to see that message.guild.members really has a function named get. Here's the docs: https://discord.js.org/#docs/main/stable/class/GuildMemberManager
No get. But there is a cache like you use elsewhere in the code. Just by checking over the docs you can tell that your existing code is wrong (it's outdated) and you need to use cache like you do elsewhere in your code:
message.guild.members.cache.get(args[1])

I need a AutoRole command when somebody joins my server

I need a AutoRole command discord.js when somebody join my discord server he gets the Discord Member role.
Ive tryied some code but it doesnt work.
const discord = require("discord.js");
const config = require('../config.json');
module.exports.run = async (bot, message, args) => {
let target = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
let reason = args.slice(1).join(' ');
let logs = message.guild.channels.find('name', config.logsChannel);
if (!message.member.hasPermission('BAN_MEMBERS')) return message.reply('you do not have permissions to use this command!s');
if (!target) return message.reply('please specify a member to ban!');
if (!reason) return message.reply('please specify a reason for this ban!');
if (!logs) return message.reply(`please create a channel called ${config.logsChannel} to log the bans!`);
let embed = new discord.RichEmbed()
.setColor('RANDOM')
.setThumbnail(target.user.avatarURL)
.addField('Banned Member', `${target.user.username} with an ID: ${target.user.id}`)
.addField('Banned By', `${message.author.username} with an ID: ${message.author.id}`)
.addField('Banned Time', message.createdAt)
.addField('Banned At', message.channel)
.addField('Banned Reason', reason)
.setFooter('Banned user information', target.user.displayAvatarURL);
message.channel.send(`${target.user.username} was banned by ${message.author} for ${reason}`);
target.ban(reason);
logs.send(embed);
};
module.exports.help = {
name: 'ban'
};
When they join they get the Discord Member role.
You can use the guildMemberAdd event to do actions on new users.
client.on("guildMemberAdd", (member) => {
member.addRole('ROLE ID HERE')
});

Resources