What is the easiest way to edit multiple permissionOverwrites?

So far i have tried <TextChannel>.edit, but this overrides all current permissions. So i've spread those into an array and then pushed the new overwrites into them, but that brings me to point 2: this is for a close ticket command, thus the permissions i edit are for the member that created the ticket, and its setting the SendMessages permission to false Now, if i spread the current permissions, and then add the updated ones, this means the array includes both the current permissions (in which the SendMessages permission is set to true), and the new one, in which it is set to false. Which one of these will override? I assume since the edited ones are the last to be pushed into the array, they will override but i'm unsure Or am i making things overcomplicated (again) and is there a really easy way to do this that i'm looking over?
25 Replies
d.js toolkit
d.js toolkit•17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
treble/luna
treble/lunaOP•17mo ago
to visualise: this is how the array would look like
[
{
id: m,
allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, and the other perms that are currently set],
type: OverwriteType.Member
},
{
id: m,
deny: [PermissionsBitField.Flags.SendMessages],
type: OverwriteType.Member
}
]
[
{
id: m,
allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, and the other perms that are currently set],
type: OverwriteType.Member
},
{
id: m,
deny: [PermissionsBitField.Flags.SendMessages],
type: OverwriteType.Member
}
]
Unknown User
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaOP•17mo ago
yeah that was what i was thinkin too but just wanna make sure there isnt an easier way that i'm overlooking
Unknown User
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs•17mo ago
method PermissionOverwrites#edit() Edits this Permission Overwrite.
treble/luna
treble/lunaOP•17mo ago
yeah but thats for a single one, this is for a ticket, which can have multiple members added to them
Unknown User
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaOP•17mo ago
yeah that could be a better idea too. but there's also a set of support roles which are added (and are configurable per guild) which dont need their permission edited. i'll probably just splice or well, the test ive done so far seem to confirm my thought of the last element in the array overriding but i'm not too keen on actually doing it that way
Unknown User
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaOP•17mo ago
yeah they're all for the members i could do it that way indeed, thanks for the advice!
Булочка | pasha_boez
this is for a close ticket command, thus the permissions i edit are for the member that created the ticket, and its setting the SendMessages permission to false Now, if i spread the current permissions, and then add the updated ones, this means the array includes both the current permissions (in which the SendMessages permission is set to true), and the new one, in which it is set to false. Which one of these will override? I assume since the edited ones are the last to be pushed into the array, they will override but i'm unsure
You can use PermissionOverwrites.resolveOverwriteOptions method to correctly edit your existed overwrites @luna🌈
d.js docs
d.js docs•17mo ago
method (static) PermissionOverwrites.resolveOverwriteOptions() Resolves bitfield permissions overwrites from an object.
Булочка | pasha_boez
I haven't studied the question enough, sorry...
treble/luna
treble/lunaOP•17mo ago
no worries! i currently cant check anything, will do tomorrow!
Булочка | pasha_boez
So i've spread those into an array
Here's an example how you can use this:
// Object with id of member as key and PermissionOverwriteOptions (like in PermissionOverwrites#edit()) as value
const newOverwrites = {
m: {
SendMessages: true,
ViewChannel: true,
},
m: {
SendMessages: false,
},
};

// Here's a my main thought
const newPermissionOverwrites = <TextChannel>.permissionOverwrites.cache.map((permissionOverwrite) => {
if (!newOverwrites[permissionOverwrite.id]) return permissionOverwrite;
return {
id: permissionOverwrite.id,
...PermissionOverwrites.resolveOverwriteOptions(
newOverwrites[permissionOverwrite.id],
permissionOverwrite,
),
type: permissionOverwrite.type,
};
});

<TextChannel>.permissionOverwrites.set(newPermissionOverwrites);
// Object with id of member as key and PermissionOverwriteOptions (like in PermissionOverwrites#edit()) as value
const newOverwrites = {
m: {
SendMessages: true,
ViewChannel: true,
},
m: {
SendMessages: false,
},
};

// Here's a my main thought
const newPermissionOverwrites = <TextChannel>.permissionOverwrites.cache.map((permissionOverwrite) => {
if (!newOverwrites[permissionOverwrite.id]) return permissionOverwrite;
return {
id: permissionOverwrite.id,
...PermissionOverwrites.resolveOverwriteOptions(
newOverwrites[permissionOverwrite.id],
permissionOverwrite,
),
type: permissionOverwrite.type,
};
});

<TextChannel>.permissionOverwrites.set(newPermissionOverwrites);
Unfortunately, now Idk how to use it with your array without workarounds, but maybe you'll think of something better or will review my method to storage new permissions P.s. I doesn't test this, code may have issues, but I want to make up my thought to you
d.js docs
d.js docs•17mo ago
interface PermissionOverwriteOptions An object mapping permission flags to true (enabled), null (unset) or false (disabled). ```js (more...)
Булочка | pasha_boez
I think that's all my things for today
treble/luna
treble/lunaOP•17mo ago
ive tried it but i could not get it to work, i found a solution myself though! thanks for the help!
Булочка | pasha_boez
Oh.. okay. Can you share your solution, please?
treble/luna
treble/lunaOP•17mo ago
let ovrr : Array<OverwriteResolvable>= [...c.permissionOverwrites.cache.values()]


ovrr.map( el => {
data.members.map((m : string) => {
if(el.id == m){

let updated : OverwriteResolvable = {
id: el.id,
allow: [PermissionsBitField.Flags.ViewChannel],
deny: [PermissionsBitField.Flags.SendMessages],
type: OverwriteType.Member
}
let index = ovrr.indexOf(el)
ovrr.splice(index, 1 , updated )
}
})
})
let ovrr : Array<OverwriteResolvable>= [...c.permissionOverwrites.cache.values()]


ovrr.map( el => {
data.members.map((m : string) => {
if(el.id == m){

let updated : OverwriteResolvable = {
id: el.id,
allow: [PermissionsBitField.Flags.ViewChannel],
deny: [PermissionsBitField.Flags.SendMessages],
type: OverwriteType.Member
}
let index = ovrr.indexOf(el)
ovrr.splice(index, 1 , updated )
}
})
})
this is just the core test i did, the only thing i just need to add is to pass in the current allowed perms as el.allow but i have to remove the SendMessages from it first
Булочка | pasha_boez
Looks hard, but thanks. Maybe I use it in future 🙂
treble/luna
treble/lunaOP•17mo ago
its funky but it works
Unknown User
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaOP•17mo ago
oh thats indeed a good way to do it!
Want results from more Discord servers?
Add your server