Discord.js removing all member roles - discord

I need the bot removes all member roles when I type a command
and here is my code
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
console.log(args);
const command = args.shift().toLowerCase();
if (command == 'مخالف') {
const member = message.mentions.members.first();
if (!member) return message.channel.send("لم يتم لعثور على العضو");
let drole = member.roles;
member.roles.remove(drole);
}
});

You can set the roles to an empty array, removing any roles they had.
member.roles.set([]);
GuildMemberRoleManager#set()

Related

discord.js add 3 roles with 1 command

I want add roles to a user when a admin typed activate (user) the bot give the user 3 roles and here is my code
if (message.content === 'activate') {
const guild = message.guild;
const role = message.guild.roles.cache.get('842121303616126977');
const member = message.mentions.members.first
member.roles.add(role);
}
});
GuildMemberRoleManager.add accepts an array of RoleResolvable, which can either be an instance of Role or a Snowflake.
if (message.content === "activate") {
const member = message.mentions.members.first(); // Note that first is a method, not a property.
if (!member) return message.channel.send("Member not found.");
member.roles.add(["RoleID1", "RoleID2", "RoleID3"]);
}
this worked
this code worked for me finally `client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
console.log(args);
const command = args.shift().toLowerCase();
if (command == 'activate') {
const member = message.mentions.members.first(); // Note that first is a method, not a property.
if (!member) return message.channel.send("member not found");
const drole = message.guild.roles.cache.get('842121300532920362');
const prole = message.guild.roles.cache.get('842121301777973298');
const lrole = message.guild.roles.cache.get('842121302708977694');
const frole = message.guild.roles.cache.get('842121305854836826');
member.roles.add(drole);
member.roles.add(lrole);
member.roles.add(prole);
member.roles.remove(frole);
message.channel.send('activated' + ' <' + '#' + member + '>');
}
});`

How to make a say embed command in Discord.js

I'm new to coding and am making a Discord bot for a friend. They are requesting a say command that could act as a confession command where it would look like this. An embed with a set Title, set color, and completely anonymous but with an editable description that would fill in with what they want to confess. As i'm new to coding I don't know how to do this. If anyone can help that would be really appreciated! Thank you!
(Edit) I realise that i wasn't through enough about what the code was so im making an edit with my main.js code.
const client = new Discord.Client();
const prefix = 'wtf ';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
bot.once('ready', () => {
console.log('Tomoko is online!');
});
bot.on('message', async msg =>{
if(!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(msg, args, Discord);
} else if (command === 'creator'){
client.commands.get('creator').execute(msg, args, Discord);
} else if (command === 'help'){
client.commands.get('help').execute(msg, args, Discord);
} else if (command === 'kick'){
client.commands.get('kick').execute(msg, args, Discord);
} else if (command === 'ban'){
client.commands.get('ban').execute(msg, args, Discord);
} else if (command === 'mute'){
client.commands.get('mute').execute(msg, args, Discord);
} else if (command === 'unmute'){
client.commands.get('unmute').execute(msg, args, Discord);
} else if (command === 'warn'){
client.commands.get('warn').execute(msg, args)
} else if (command === 'deletewarns'){
client.commands.get('deletewarns').execute(msg, args);
} else if (command === 'warnings'){
client.commands.get('warnings').execute(msg, args);
} if (args[0].toLowerCase() === 'confess') {
const description = args.splice(1).join(" ");
const embed = new MessageEmbed().setTitle('✦┊‧๑ ꒰<a:ccc:862524564457390150><a:ooo:862524674185101322><a:nnn:862524667368833024><a:fff:862524592202973244><a:eee:862524583802568714><a:sss:862524709782683648><a:sss:862524709782683648>꒱ ‧๑┊✧').setColor('ffaaaa').setDescription(description);
await msg.delete().catch(e => console.log(e));
msg.channel.send(embed);
} else if (command === "unban"){
client.commands.get('unban').execute(msg, args, Discord);
;}
});
client.login('DAMN YOU WISH I WOULD SHOW YOU');
So if possible can anyone give me the advanced command handler say embed command. Thank you!!
That depends on how you handle your commands. But in general: generate a new embed, and set the description to the contents of your message.
Check here on how to create an embed.
A simple bot to do your job
// importing dependencies
const { MessageEmbed, Client } = require('discord.js');
const prefix = '!';
const bot = new Client(); // init discord client
bot.on('ready', () => console.log('yee im on'));
// listening for messages
bot.on('message', async msg => {
if (!msg.content.startsWith(prefix)) return // dont run if the prefix is not used
const args = msg.content.substring(prefix.length).split(" "); // creating array of the message contents
if (args[0].toLowerCase() === 'say') { // a simple command handler
const description = args.splice(1).join(" ");
const embed = new MessageEmbed().setDescription(description); // setTitle and stuff according to your preference
await msg.delete().catch(e => console.log(e)); // deleting the user message since it should be anonymous
msg.channel.send(embed);
}
});
bot.login('yourtokenhere');
Make sure to replace token and prefix with your token and prefix
How to run the command :
!say ooh this is a confession

client.commands.get('kickembed').execute //cannot read property 'execute' is undefined//

I can actually run the bot, it will let mi interact with the other 3 commands, but when trying to do the "kickembed" it will fail and give me the error "client.commands.get('kickembed').execute(message,args,Discord)"
^
Cannot read property of 'execute' of undefined
tbh, i tried everithing, my little brain cant work this out, tysm for your time!
const client = new Discord.Client();
const prefix = ('^');
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () =>{
console.log('Cataclysm is online, beep bop')
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} else if (command === 'welcome'){
client.commands.get('welcome').execute(message, args);
} else if (command === 'ban'){
client.commands.get('Ban').execute(message, args);
} else if (command === 'kick'){
client.commands.get('kick').execute(message, args);
} else if (command === 'kickembed'){
client.commands.get('kickembed').execute(message, args, Discord);
}
}); ~~~
and this is "kickembed"(where i have the problem i think)
~~~ const name = 'kick';
const description = "This command kicks a member!";
function execute(message, args, Discord) {
const banembed = new Discord.MessageEmbed()
.setColor('#ff3838')
.setTitle('A user has been kicked')
.setDescription('player (fix this plis) has been kicked by etc');
message.channel.send(banembed);~~~
your kickembed files have wrong name it is kick
So it should be like:
const name = 'kickembed';
const description = "This command kicks a member!";
function execute(message, args, Discord) {
const banembed = new Discord.MessageEmbed()
.setColor('#ff3838')
.setTitle('A user has been kicked')
.setDescription('player (fix this plis) has been kicked by etc');
message.channel.send(banembed);

Discord.JS - Find if a user has a role

I'm doing the most basic mute command ever, and I need to find if the user has a role called 'Muted'
I've added the code below, but I keep getting an error.
if (command === "mute") {
const { member, mentions } = message
const mutee = message.mentions.users.first();
const muter = message.author
console.log(mutee)
if(member.hasPermission('ADMINISTRATOR')) {
let muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (message.guild.members.get(mutee.id).roles.cache.has(muteRole.id)) {
message.channel.send(new MessageEmbed() .setTitle(`Uh Oh!`) .setDescription(`${mutee.user} already has been Muted!`))
}
else {
}
}
}
First, I recommend you to get the mentioned member directly instead of the user with const mutee = message.mentions.members.first();. (If you are using the command under a Guild Channel)
Then, you can simply check if he has the role with:
if (mutee.roles.cache.has(muteRole.id))

Discord bot role command stopped working for unknown reasons

I have a command which worked, but at some point stopped, returning to the chat message that the role does not exist. An error "(node:12228) DeprecationWarning: Collection#find: pass a function instead" is sent to the console every time I use a command, but I always had it
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission("MANAGE_MEMBERS")) return message.reply();
let rMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
if(!rMember) return message.reply("nope.");
let role = args.join(" ").slice(22);
if(!role) return message.reply("nope!");
let gRole = message.guild.roles.find(`name`, role);
if(!gRole) return message.reply("role does not exist.");
const allowed = ['some id'];
if (!allowed.includes(gRole.id)) return;
if(rMember.roles.has(gRole.id)) return message.reply("nope.");
await(rMember.removeRoles(['some id']));
await(rMember.addRole(gRole.id));
if(gRole.id == 'id') rMember.addRole('id') && rMember.removeRoles(['some id']);;
try{
await rMember.send(`you got ${gRole.name}!`)
}catch(e){
}
}
module.exports.help = {
name: "role"
}
So I need the command to work.
In order of appearance, I see these mistakes in your code...
You're not catching any rejected promises.
MANAGE_MEMBERS is not a valid permission flag.
You should pass a function into Collection.find().
No idea what you're trying to do with the declaration of role.
Your use of the && logical operator in your if statement is incorrect. Use a block statement instead.
Your catch block in the try...catch statement has been left empty.
Combining the answers provided to your other questions about this code, this is a correct, much cleaner rewrite...
const getMention = require('discord-mentions'); // Don't forget to install.
module.exports.run = async (bot, message, args) => {
if (!message.guild) return;
try {
if (!message.member.hasPermission('MANAGE_ROLES')) return;
if (!args[1]) return await message.reply('Please provide a user.');
if (!args[2]) return await message.reply('Please provide a role.');
if (args[3]) return await message.reply('Too many arguments provided.');
let member;
if (getMention(args[1])) member = getMention(args[1], message.guild).member;
else member = message.guild.members.find(m => m.user.tag === args[1] || m.id === args[1]);
if (!member) return await message.reply('Invalid user provided.');
let role;
if (getMention(args[2])) role = getMention(args[2], message.guild).role;
else role = message.guild.roles.find(r => r.name === args[2] || r.id === args[2]);
if (!role) return await message.reply('Invalid role provided.');
const allowed = ['role1ID', 'role2ID', 'role3ID'];
if (!allowed.includes(role.id)) return await message.reply('That role is not allowed.');
if (member.roles.has(role.id)) return await message.reply('That user already has that role.');
await member.removeRoles(['someID', 'someOtherID']);
await member.addRole(role.id);
if (role.id === 'anAlternateRoleID') {
await member.removeRoles(['someID', 'someOtherID']);
await member.addRole('otherRoleID');
}
await member.send(`You got the ${role.name} role.`)
.catch(() => console.log(`Couldn't send message to ${member.user.tag}.`));
} catch(err) {
console.error(err);
}
};
module.exports.help = {
name: 'role'
};
Keep in mind, if you're removing just one role from a member, you should use the singular version of the method, GuildMember.removeRole().

Resources