Reputation: 13
I'm trying to make a discord bot, which can add a specific role to an user.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', (message) => {
let allowedRole = message.guild.channels.find(channel => channel.name === "Streamer");
let gRole = message.guild.channels.find(channel => channel.name === "Stream 1");
mention = message.mentions.users.first();
let member = message.mentions.users.first();
if (message.content === "addRole") {
members.get(message.mentions.user.id).addRole(gRole);
}
});
client.login('mytoken');
I expect that the bot can add role to a specific person
Upvotes: 1
Views: 2091
Reputation: 2980
First of all, your first mistake was, that you searched for channel names and not role names in your variable gRole
and allowedRole
. I changed this 2 variables, so they search for roles instead of channels.
You defined twice the same, once as mention and once as member = message.mentions.users.first()
. Because of this, I removed your variable named mention
and used the variable member
. I found that the code for this variable didn't match the name of the variable because your code returned the user Object and I changed it to the member Object (because your variable is named member).
Lastly, I used your predefined variable member
and assigned the role gRole
to it, so you don't have to get the member another time.
Try to use the following code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
const allowedRole = message.guild.roles.find(role => role.name === 'Streamer'); // isn't used
const gRole = message.guild.roles.find(role => role.name === 'Stream 1');
const member = message.mentions.members.first();
if (message.content === 'addRole') {
member.addRole(gRole);
}
});
client.login('mytoken');
Upvotes: 2