I'm trying to make a discord bot that will create an admin role with administrator permissions with an admin command.
I've tried this code complied from other sources, but I can't get it to work. Any help is appreciated.
let member = await server.fetch_member(message.author.id);
await message.guild.createRole({name:"admin", color: "00FFFF", permissions: "ADMINISTRATOR",});
await member.add_roles(role="admin", reason="role added");
await message.channel.send('success');
CodePudding user response:
I'm not sure which sources you looked at. Check the Discord.JS Documentation.
I used RoleManager.create() to supply a name
aswell as a Administrator
permission via PermissionFlagBits
. This will return a promise. I will await the promise and assign the role.
Lastly, I will use GuildMemberRoleManager.add() to add the new role.
I wrapped my code in a try/catch block to catch any Discord API errors.
import { PermissionFlagsBits } from "discord.js";
// or const { PermissionFlagsBits } = require("discord.js");
// depending on your environment
// ...
try {
const newAdminRole = await message.guild.roles.create({
name: "Admin",
permissions: [PermissionFlagsBits.Administrator]
});
await message.member.roles.add(newAdminRole);
} catch (err) {
console.error(err);
}
CodePudding user response:
I did play it for a bit. No outcome but I will provide at least my progress you can continue from.
message.guild.roles.create({
name: 'admin',
color: '#00FFFF',
// Whatever permissions you want
permissions: ["ADMINISTRATOR"],
})
const guildMember = message.guild?.members.fetch(message.author.id)
// Throws Property 'addRole' does not exist on type 'GuildMemberManager' even tho documentation says otherwise
message.guild.members.addRole(guildMember)
// Part of documentation says this is for adding roles, but I am 99% certain this is for adding user via. Oauth2
message.guild.members.add(message.author)
There is documentation for that method. https://discord.js.org/#/docs/discord.js/main/class/GuildMemberManager?scrollTo=addRole
If you find anything please answer your question so if someone encounters the same problem the solution will be here for them.