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 + '>');
}
});`
Related
Here is the Code. I only want to give members xp when they write in a specific discord channel. How can i do that?
client.on('messageCreate', async message => {
if (!message.guild) return;
if (message.author.bot) return;
const prefix = '?'
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const user = await Levels.fetch(message.author.id, message.guild.id);
if (!message.guild) return;
if (message.author.bot) return;
const randomAmountOfXp = Math.floor(Math.random() * 29) + 1; // Min 1, Max 30
const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, randomAmountOfXp);
if (hasLeveledUp) {
const user = await Levels.fetch(message.author.id, message.guild.id);
message.channel.send({ content: `${message.author}, congratulations! You have leveled up to **${user.level}**. :tada:` });
}
if (user.level === 5) {
message.member.roles.add("1022876270754791445");
} else if (user.level === 10) {
message.member.roles.add("1022876505132499054");
}else if (user.level === 10) {
message.member.roles.remove("1022876270754791445");
}
});
You can check message's channel id when event triggered.
if (message.channel.id !== "CHANNEL_ID") return;
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()
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))
I have this script for kicking users. How can I change that script so I can kick user by user id?
Script:
const discord = require('discord.js');
const client = new discord.Client;
const prefix = "$";
client.on('message', function(message) {
if (!message.content.startsWith(prefix)) { return }
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === "kick") {
let member = message.mentions.members.first();
let reason = args.slice(1).join(" ");
if (member) { // add this
member.kick(reason);
client.channels.cache.get('737341022782357566').send("User <#" + member.id +
"> with id " + member.id + " has been kicked by <#" + message.author.id +
"> with reason " + reason)
} else {
message.reply("invalid parameters for $kick")
}
message.delete();
}
})
client.login('token');
You'll have to get the member by ID:
const Member = message.guild.members.cache.get('MemberId')
if (!Member) return console.log(`Invalid member.`);
And then kick them:
Member.kick("Reason").catch(error => {
console.log(`Couldn't kick ${Member.user.tag}.`);
})
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().