I want to do a setup command so I can create a role and give it to myself via a command. The role is created but I cannot give myself the role. I always get an error code after the role was created.
My Code:
case 'setup':
if(!message.author.id == '416940852291045376') return message.channel.send('Youre not me')
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.catch(console.error);
let role = message.guild.roles.cache.find(r => r.name === "Bot dev")
let member = message.member
member.roles.add(role)
The error code i'm getting:
C:\Discord Bots\Bot v13\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:110
throw new TypeError('INVALID_TYPE', 'roles', 'Role, Snowflake or Array or Collection of Roles or Snowflakes');
^
TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
RoleManager#create() returns a promise with the newly created role, you must await it or resolve it before searching for the role.
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.then(role => {
const { member } = message;
member.roles.add(role);
})
.catch(console.error);
Related
I'm trying to make a discord bot (with js) that can assign roles to users.
Discord version: 14.3.0
Nodejs version: 16.17.0
Im using this function to assign role.
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages
]
});
....
var role = message.guild.roles.cache.find(role => role.name == "Role_name")
guildMember.roles.add(role) // this line is causing the error
This is the exact error im getting
/node_modules/#discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:293
throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code :
data.error, status, method, url, requestData);
^
DiscordAPIError[50013]: Missing Permissions
at SequentialHandler.runRequest ( /media/pranav/Storage/ie/programming/nodejs/project-l isabey/node_modules/#discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:293:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async SequentialHandler.queueRequest ( /media/pranav/Storage/ie/programming/nodejs/project-l isabey/node_modules/#discordjs/rest/dist/lib/handlers/SequentialHandler.cjs:99:14)
at async REST.request (/media/pranav/Storage/ie/programming/nodejs/project-l isabey/node_modules/#discordjs/rest/dist/lib/REST.cjs:52:22)
at async GuildMemberRoleManager.add ( /media/pranav/Storage/ie/programming/nodejs/project-l isabey/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:129:7) {
rawError: { message: 'Missing Permissions', code: 50013 },
code: 50013,
status: 403,
method: 'PUT',
url: ' https://discord.com/api/v10/guilds/1011546412011507823/members/1021733308457041970/roles/1 021714244846223360',
requestBody: { files: undefined, json: undefined }
}
I tried giving the bot Mod permissions in the server
Invited bot to server with "discord.com/api/oauth2/authorize?client_id=xxxxxxxxxx&permissions=8&scope=bot%20applications.commands"
In developer portal all 3 "privileged gateway intents" are checked.
Still i'm getting the same error. How can I fix this ?
Thanks!
Make sure that the role you're trying to assign is lower to your highest bot role.
In server settings, literally drag the bots role above the roles it will be assigning.
(the code)
const Command = require("../Structures/Command.js");
module.exports = new Command({
name: "give-role",
description: "gives you the role",
permission: "ADMINISTRATOR",
async run(message, arguments, client) {
const targetUser = message.mentions.users.first();
if (!targetUser) {
message.reply("Tag the user that you want to give the role to");
return;
}
arguments.shift();
const roleName = arguments[1];
const { guild } = message;
const role = guild.roles.cache.find((role) => {
return role.name === roleName;
});
if (!role) {
message.reply(`There is no role called: ${roleName}`);
return;
}
const member = guild.members.cache.get(targetUser.id);
member.roles.add(role);
message.reply(`That user now has the role : ${roleName}`);
}
});
(error message)
/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Permissions
at RequestHandler.execute (/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
at async GuildMemberRoleManager.add (/home/runner/Discord-manager/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:124:7) {
method: 'put',
path: '/guilds/911548130917498901/members/854133415364263976/roles/924275494810165259',
code: 50013,
httpStatus: 403,
requestData: { json: undefined, files: [] }
}
The error is DiscordAPIError: 50013, the error is getting because the prompt says the bot do not have permissions but I added administrator but it still says it doesn't have permissions and I do not know why. Please help me (sorry if my english is bad :() )
The bot may be trying to give a role higher than the one your bot has. To fix this, drag the bot's highest positioned role over the role you are trying to give.
Credit: Reddit Comment
You need to check the position of the role before trying to add it to the user, for example:
if (message.guild.me.roles.highest.position <= role.position) {
message.reply(`${role.name} is higher than or equal to my highest role.`);
return;
}
I try to add a role to a user by ID, but I always receive this error
user.roles.add(role)
^
TypeError: Cannot read properties of undefined (reading 'add')
Here is my code
let role = Guild.roles.cache.find((r) => r.name === dataobj.Guild.name);
client.users.fetch(useritem).then((user) => {
user.roles.add(role)
.catch(error => { })
});
dataobj.Guild.name is the name of the role.
useritem is the ID of the user.
var role is correct and output the right role.
Var user is correct and output the right user.
I tried many things, but nothing works.
Thank for your help.
You should fetch guild.members instead of client.users and to add role by its ID you should use role.id instead of role
I am trying to make a command were it creates a role but gives me the error saying that guild is not defined:
guild.roles.create({
^
ReferenceError: guild is not defined
while this is my code:
guild.roles.create({
data: {
name: 'Boots',
color: 'BLUE',
},
reason: 'very cool',
})
.then(console.log)
.catch(console.error);
let role = message.guild.roles.find(r => r.name === "Boots");
member.roles.add(role)
message.channel.bulkDelete(1);
message.channel.send('Mhm, very cool');
You have to use
message.guild.roles [...]
Because if you type
guild.roles [...]
the program wants to access the roles property of the variable guild
Did you define guild? If you want to use guild, just write something like const guild = message.guild;
I’ve tried to add code to allow people to customise which role people get when they join a server which can be set on a per server basis however I cannot seem set get it working. The greeting channel, the greeting and the DM are all working. It is only the role adding. If you could point me in the right direction then that would be very helpful.
client.settings = new Enmap({
name: "settings",
fetchAll: false,
autoFetch: true,
cloneLevel: 'deep'
});
// Just setting up a default configuration object here, to have somethign to insert.
const defaultSettings = {
prefix: "!",
modLogChannel: "mod-log",
modRole: "Moderator",
adminRole: "Administrator",
welcomeChannel: "chat",
welcomeMessage: "Welcome to the server, {{user}} 😉",
welcomeDMMessage: "text",
rolesOnJoin: "Basic"
}
client.on("guildDelete", guild => {
// When the bot leaves or is kicked, delete settings to prevent stale entries.
client.settings.delete(guild.id);
});
client.on("guildMemberAdd", member => {
client.settings.ensure(member.guild.id, defaultSettings);
let roleAdd = client.settings.get(member.guild.id, "rolesOnJoin");
let welcomeMessage = client.settings.get(member.guild.id, "welcomeMessage");
let welcomeDMMessage = client.settings.get(member.guild.id, "welcomeDMMessage");
let role = member.guild.roles.find(role => role.name == roleAdd);
welcomeMessage = welcomeMessage.replace("{{user}}", member.user.tag)
member.guild.channels
.find("name", client.settings.get(member.guild.id, "welcomeChannel"))
.send(welcomeMessage, {files: ["https://cdn.glitch.com/ecc1aef4-3247-42a1-9361-cfc56e9c5ba1%2F75AC6C9B-3E71-4F25-B8CF-47050B4B8F21.jpeg"]})
.catch(console.error);
member.send(welcomeDMMessage);
member.addRole(role);
});
addRole() needs the role ID (or the Role as an object), you can still find it via it's name with
let role = guild.roles.find(role => role.name == roleAdd)
member.addRole(role);
Presumably the problem is that addRole() expects either a Role object or a role ID:
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=addRole
https://discord.js.org/#/docs/main/stable/typedef/RoleResolvable
"Basic" is neither.
If all you have is the role name, you need to find it by that name in the guild.roles Collection.