DiscordAPIError: Missing Access on createOverwrite - discord

Developing a discord moderation bot with Discord.js
Facing error "DiscordAPIError: Missing Access"
My bot user, which has all server permissions except "Administrator", and highest.rawPosition is 18 (highest of all roles) :
user: ClientUser {
id: 'XXXXXXXXX',
bot: true,
username: 'Bot',
discriminator: '1863',
avatar: '2e8af5cccdc5cf15a0f88818dbb044e6',
lastMessageID: null,
lastMessageChannelID: null,
verified: true,
mfaEnabled: true,
_typing: Map {}
},
is trying to add this role (rawPosition is 2) :
Role {
id: 'XXXXXXXXX',
name: 'Mod',
color: 0,
hoist: false,
rawPosition: 2,
permissions: Permissions { bitfield: 37211712 },
managed: false,
mentionable: false,
deleted: false
}
as an overwrite permission to this channel :
CategoryChannel {
type: 'category',
deleted: false,
id: 'XXXXXXXXX',
name: 'Section Job',
rawPosition: 6,
parentID: null,
}
using this code :
await channel.createOverwrite(role, {
VIEW_CHANNEL: true,
READ_MESSAGES: true,
SEND_MESSAGES: true,
CONNECT: true
});
Here is the error I get :
[2020/10/06 00:52:56:427] DiscordAPIError: Missing Access
I performed a search before posting this.
Discord gives many possible explanations about this specific error, and none would fit with my issue : https://discordjs.guide/popular-topics/permissions-extended.html#missing-permissions
Your bot is missing the needed permission to execute this action in it's calculated base or final permissions (requirement changes based on the type of action you are trying to execute).
--> My bot has all permissions except "Administrator"
You provided an invalid permission number while trying to create overwrites. (The calculator on the apps page returns decimal values while the developer documentation lists the flags in hex. Make sure you are not mixing the two and don't use the hex prefix 0x where not applicable)
--> I give the exact same permissions, formatted exactly the same as another group, and it works
It is trying to execute an action on a guild member with a role higher than or equal to your bots highest role.
--> Not executing an action on a user, but on a group
It is trying to modify or assign a role that is higher than or equal to its highest role.
--> Bot highest role is 18, and added role rawPosition is 2
It is trying to add a managed role to a member.
--> As you can see it is not "managed"
It is trying to remove a managed role from a member.
--> Not trying to remove a role
It is trying to execute a forbidden action on the server owner.
--> Not editing a user, but a channel (and server owwner does not have this role)
It is trying to execute an action based on another unfulfilled factor (for example reserved for partnered guilds).
--> I don't understand this one but it couldn't be that
It is trying to execute an action on a voice channel without the VIEW_CHANNEL permission.
--> Not executing an action on a voice channel, but on a Category channel, and it has VIEW_CHANNEL permission
More information :
1/ The same command in the exact same context works with some other groups, like this one :
Role {
id: 'XXXXXXXXX',
name: 'Job',
color: 0,
hoist: false,
rawPosition: 1,
permissions: Permissions { bitfield: 37211712 },
managed: false,
mentionable: false,
deleted: false
}
2/ The same command works with "Administrator" permission assigned to the bot
3/ Of course, adding "Administrator" permission to the bot is not an option
Thanks for any help !

Ok then I found the solution thanks to discord Js community
I forgot to give my bot read permissions on that specific channel, so the bot couldn't give permissions to a channel he didn't have access to...
I just added
permissionOverwrites: [
{
id: guild.me.roles.highest,
allow: ['VIEW_CHANNEL', 'MANAGE_CHANNELS', 'MANAGE_ROLES']
}
]
to my channel creation, and it works.

Related

dmPermission property not working on discord.js v14

I noticed my discord bot commands were showing up in DMs too, which I didn't want. So I added
dmPermission: false,
to all of them. I refreshed the / commands and I can still see and run the commands in my DMs with the bot. How can I fix this?
Here is an example:
{
name: 'setchannel',
description: 'Sets the channel in which certain messages should be sent',
dmPermission: false,
options: [
...
]
}

Discord.js changing perms

Let say I want to make a lock command (making only admins be able to say something in a channel) and I want a specific role to be overwrite (not #everyone). How could I do that?
To simply it. How can I disable SEND_MESSAGE for a specific role? /
You will be able to achieve this via GuildChannel.permissionOverwrites which returns the PermissionOverwriteManager class. Within that class, you will be able to use the .edit() method which edits permission overwrites for a user or role in this channel, or creates an entry if not already present.
In the first parameter of the .edit() method, you can input the role ID as a string, and in the options, you can input the property, or as known as the permission, with a value of true to enable or value false to disable.
An example of this is the following: (code according to the documentation from https://discord.js.org/#/docs/discord.js/stable/class/PermissionOverwriteManager)
// Edit or Create permission overwrites for a message author
message.channel.permissionOverwrites.edit(message.author, {
SEND_MESSAGES: false
})
.then(channel => console.log(channel.permissionOverwrites.cache.get(message.author.id)))
.catch(console.error);
You can use GuildChannel.permissionOverwrites.edit to set permission overwrite.
The code would be:
message.channel.permissionOverwrites.edit('replace with role id', {
SEND_MESSAGE: false
})
Slash command version:
interaction.channel.permissionOverwrites.edit('replace with role id', {
SEND_MESSAGES: false
})

Trouble sending DM to Message to stored user in DB discord.js

So Im making a ticket command and when you say .new it will open a channel and store message.author so it can dm you a transcript later on when you close it. When I log Console.log(message.author) and the stored person in the db, the message.author has User before the brackets and in the db it does not. Also the db has more lines while message.author has less:
MEMBER: {
id: '708457617013342249',
system: false,
locale: null,
flags: 128,
username: 'chuli',
bot: false,
discriminator: '0001',
avatar: 'a_483669232f603ee04c099c0449e8dc6a',
lastMessageChannelID: '829491485866065971',
createdTimestamp: 1588979858402,
defaultAvatarURL: 'https://cdn.discordapp.com/embed/avatars/1.png',
tag: 'chuli#0001',
avatarURL: 'https://cdn.discordapp.com/avatars/708457617013342249/a_483669232f603ee04c099c0449e8dc6a.webp',
displayAvatarURL: 'https://cdn.discordapp.com/avatars/708457617013342249/a_483669232f603ee04c099c0449e8dc6a.webp'
}
MESSAGE.AUTHOR User {
id: '708457617013342249',
system: false,
locale: null,
flags: UserFlags { bitfield: 128 },
username: 'chuli',
bot: false,
discriminator: '0001',
avatar: 'a_483669232f603ee04c099c0449e8dc6a',
lastMessageID: '842016982039134249',
lastMessageChannelID: '841958995890667530'
}
So I get an error when trying to send to the stored db user:
member.send(pembed).catch()
^
TypeError: member.send is not a function
Ive been breaking my head over this so I hope someone has the answer to this out there!
The reason they have different amount of lines is because they are two different things. message.author is a User, but what's stored in the db is a GuildMember. What's the difference?
The reason that message.author has User in front of it is that it's a class. What's in the db was originally a class, and would've been logged with GuildMember in front of it, however you unfortunately cannot keep classes in quick.db. Nor should you want to, as it would take up an insane amount of storage. Methods such as #send are only available on the GuildMember class (as opposed to the GuildMember class turned into an object by quick.db), so you can't access it from the db.
Instead, store the user ID in the db, as it's a unique identifier and you can get the user from it without storing huge objects in a db. You can use client.users.fetch(id) to get the user.
// await db.set('channel_id', 'author_id');
const id = await db.get('channel_id');
const user = await client.users.fetch(id);
user.send(...);
Ok I fixed thanks to #Lioness100
Heres what I did
const memberid = db.fetch(`ticket_${message.channel.id}`)
const user = message.guild.members.cache.find(c=> c.id === memberid)
Works fine! :) Thanks for helping guys.

How to get all guilds where a member is in?

I know how to get all members from a guild but I need to do the opposite operation : Getting all the guilds (IDs) where a specified member is registered in.
When fetching user like this client.users.cache.get(memberID); I don't see anything in the result that can allow me to see all the member's guilds :
User {
id: '706498754712807398',
system: null,
locale: null,
flags: UserFlags { bitfield: 0 },
username: 'johndoe',
bot: false,
discriminator: '1023',
avatar: null,
lastMessageID: null,
lastMessageChannelID: null
}
Any suggestion ?
You can do this by using the following code:
const userID = '3383083830389'; // the ID of the user
const guilds = client.guilds.cache.filter((guild) => guild.members.cache.has(userID));
and guilds is a Collection of guilds where the user is in. This has two limitations :
you can only get the guilds where the bot is in too
this won't work if the member is not cached (this can be resolved by trying to fetch the member in each guild)
You cannot view servers that a user is in that you do not have access to the account. This is a Discord limitation and falls under the privacy category, you don't want to do this anyways as it can be used maliciously.
The only way you can access the guilds a user is in if you are logged in as that account, otherwise without logging into that account, you cannot see what kind of servers they are in.
But if you want to check what kind of servers your bot is in, the code is quiet simple.
const allGuilds = (client.guilds.cache)
console.log(allGuilds)
I have not tested this myself, but it should point you in the right direction.

Creating channel permission overwrites

I am creating a bot command so that when you type !setup it sets up the whole server (creating channels and roles, etc). I have already set it to create the roles and channels but there are some channels that only specific roles can type in, whereas, other roles can only read the channel. I don't know how to set up permission overwrites for certain roles.
I have already looked on the Discord.js documentation and it doesn't help much. I receive an error that supplied parameter was neither a user nor a role, but I don't know how to gain the role ID.
message.guild.createRole({
name: 'Admin',
color: '#2494ad',
permissions: ['ADMINISTRATOR', 'VIEW_AUDIT_LOG', 'MANAGE_GUILD', 'MANAGE_CHANNELS', 'SEND_TTS_MESSAGES', 'CREATE_INSTANT_INVITE', 'KICK_MEMBERS', 'BAN_MEMBERS', 'ADD_REACTIONS', 'PRIORITY_SPEAKER', 'READ_MESSAGES', 'SEND_MESSAGES', 'MANAGE_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES', 'READ_MESSAGE_HISTORY', 'MENTION_EVERYONE', 'USE_EXTERNAL_EMOJIS', 'CONNECT', 'SPEAK', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'USE_VAD', 'CHANGE_NICKNAME', 'MANAGE_NICKNAMES', 'MANAGE_ROLES', 'MANAGE_WEBHOOKS', 'MANAGE_EMOJIS']
});
message.guild.createRole({
name: 'Requesting Role',
color: '#1bb738',
permissions: ['READ_MESSAGES', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY',]
});
//Categories and Channels
message.guild.createChannel('clan-communications', {
type: 'category',
permissionOverwrites: [{
id:'25311096',
deny: ['SEND_MESSAGES']
}]
});
//Permissions
message.channel.overwritePermissions('25311096', {
SEND_MESSAGES: false
});
break;
I would like roles to have base permissions for the whole server. But certain roles have overwrites for different channels. Instead it says the supplied parameter was neither a user nor a role.
First and foremost, welcome to Stack Overflow. Hopefully we can be of help to you.
Let's walk through a solution step by step to achieve your desired result.
When you create your roles, you should declare a variable to store them. That way, you can use what the client just created later on. Since Guild.createRole() returns a promise, we can save the fulfilled result into a variable.
Note that the keyword await must be placed in async context (within an async function).
const vip = await message.guild.createRole('VIP', { ... });
Then, when you need to use that role, you can refer to the variable.
await message.guild.createChannel('VIPs Only', {
type: 'category',
permissionOverwrites: [
{
id: vip.id,
allow: ['READ_MESSAGES']
}, {
id: message.guild.defaultRole.id, // #everyone role
deny: ['READ_MESSAGES']
}
]
});
If the role already exists, you can retrieve it from the Collection of roles returned by Guild.roles.
const testRole = message.guild.roles.get('IDhere');
const adminRole = message.guild.roles.find(r => r.name === 'Admin');
Other simple improvements.
In your code snippet, I don't see any error handling. The methods you're using return promises. If an error occurs in any, the promise state will become rejected. Any rejected promises that aren't caught will throw errors. To fix this, attach catch() methods to each promise (better for individual handling) or wrap async code in a try...catch statement.
When creating your Admin role, know that the ADMINISTRATOR permission automatically grants all other permissions (and allows users to bypass channel-specific permissions). Therefore, there's no need to list any other permission, or change the role's permission overwrites in any channels.
Your IDs in your code don't appear to be valid. Discord IDs are snowflakes, represented by strings of 18 digits. You can access them through Developer Mode in Discord. However, you shouldn't have to hard-code them with this solution, unless they already exist.
In the permissions array for your Requesting Role role, you have an extra comma.

Resources