#permissions overwrites

1 messages Β· Page 1 of 1 (latest)

magic basin
#

@hidden elbow lets continue here

thorn tulip
#

Why don’t you use edit

hidden elbow
#

Sounds good πŸ’―

hidden elbow
magic basin
#

can you send actual code?

hidden elbow
#
const { Client, GatewayIntentBits, messageLink, Discord, ConnectionService, IntentsBitField, OverwriteType } = require("discord.js")

// Client Control

const client=new Client({
    restRequestTimeout: 120000,
    intents:[
        GatewayIntentBits.Guilds,//adds server functionality
        GatewayIntentBits.GuildMessages, //gets messages from our bot.
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers
    ]
});

// SQL Init
var db;

// General Variables and Functions

var TOKEN = '~~~'; // Will .env this at some point
var DEBUG = false;

function getEpoch(){
    var DATE = new Date()
    return Math.floor(DATE.getTime() / 1000);
}

// Server functions

async function addUserToChannel(member, guild, channelId){
  var myChannel = await guild.channels.cache.get(channelId);
  await myChannel.permissionOverwrites.edit(member, {
    'ViewChannel': true,
    'SendMessages': true,
  }, OverwriteType.Member);

  var perms = await myChannel.permissionOverwrites.cache.get(member.id);
  console.log("Added to server: " + member.user.tag + " to " + myChannel.name + " with perms: " + perms);
}

// Client Events

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('guildMemberAdd', async member => {
  addUserToChannel(member, member.guild, "1052526652707586068");
});

That should be all the relevant stuff

magic basin
hidden elbow
#

what do you mean?

magic basin
#

I mean 1052526652707586068

hidden elbow
#

yeah, definitely is since it can find it for myChannel

thorn tulip
#

How do you know though

#

Isn’t overwritesOptions an object too

magic basin
#

btw I'm sure it will throw error if don't find anything in cache

magic basin
thorn tulip
#

It just returns undefined if it’s not in the cache

magic basin
#

oh wait really

hidden elbow
#

it logs:

Added to server: Tobys578_#9323 to first-landing with perms: 817414141471752308
thorn tulip
#

Just doesn’t change permissions?

#

What are you using for β€œmember”

turbid boltBOT
magic basin
candid gazelle
#
Permission deconstruction for 817414141471752308:
[βœ–] CreateInstantInvite (1) 1<<0
[βœ–] KickMembers (2) 1<<1
[βœ”] BanMembers (4) 1<<2
[βœ–] Administrator (8) 1<<3
[βœ”] ManageChannels (16) 1<<4
[βœ”] ManageGuild (32) 1<<5
[βœ”] AddReactions (64) 1<<6
[βœ–] ViewAuditLog (128) 1<<7
[βœ–] PrioritySpeaker (256) 1<<8
[βœ–] Stream (512) 1<<9
[βœ–] ViewChannel (1024) 1<<10
[βœ–] SendMessages (2048) 1<<11
[βœ–] SendTTSMessages (4096) 1<<12
[βœ–] ManageMessages (8192) 1<<13
[βœ–] EmbedLinks (16384) 1<<14
[βœ–] AttachFiles (32768) 1<<15
[βœ–] ReadMessageHistory (65536) 1<<16
[βœ”] MentionEveryone (131072) 1<<17
[βœ”] UseExternalEmojis (262144) 1<<18
[βœ”] ViewGuildInsights (524288) 1<<19
[βœ–] Connect (1048576) 1<<20
[βœ–] Speak (2097152) 1<<21
[βœ”] MuteMembers (4194304) 1<<22
[βœ”] DeafenMembers (8388608) 1<<23
[βœ–] MoveMembers (16777216) 1<<24
[βœ–] UseVAD (33554432) 1<<25
[βœ–] ChangeNickname (67108864) 1<<26
[βœ–] ManageNicknames (134217728) 1<<27
[βœ”] ManageRoles (268435456) 1<<28
[βœ”] ManageWebhooks (536870912) 1<<29
[βœ–] ManageEmojisAndStickers (1073741824) 1<<30
[βœ”] UseApplicationCommands (2147483648) 1<<31
[βœ–] RequestToSpeak (4294967296) 1<<32
[βœ–] ManageEvents (8589934592) 1<<33
[βœ”] ManageThreads (17179869184) 1<<34
[βœ–] CreatePublicThreads (34359738368) 1<<35
[βœ”] CreatePrivateThreads (68719476736) 1<<36
[βœ–] UseExternalStickers (137438953472) 1<<37
[βœ”] SendMessagesInThreads (274877906944) 1<<38
[βœ”] UseEmbeddedActivities (549755813888) 1<<39
[βœ”] ModerateMembers (1099511627776) 1<<40
thorn tulip
#

Oh shit didn’t see that

#

Yino!

hidden elbow
magic basin
#

I think they are a bit different

hidden elbow
#

I'm thinking it might be to do with this tbh, should @ everyone be overriding the member's specific perms like that?

candid gazelle
#

nah it shouldn't, everyone aught to be above I would have thought...

#

Strange that

magic basin
#

also it will be changed anyway

#

just with no effect

candid gazelle
hidden elbow
#

yeah I thought so, I mean Toby578 doesn't have a role, but I thought the user's specific perms should overwrite everyone

magic basin
#

was it even changed? do you see changes in ui?

hidden elbow
magic basin
#

try to do as was said a lot messages ago, change options to object

hidden elbow
#

How do you mean? Sorry js isn't my main language πŸ˜‚

magic basin
#

wait

#

I'm confused..

#

Oh I see nvm

hidden elbow
#

join the club XD

thorn tulip
#

{type: <type> }

magic basin
#

as you see above it takes object of type and reason

hidden elbow
#

you mean

ViewChannel: true

instead of

'ViewChannel': true ```
?
magic basin
#

both are optional

hidden elbow
#

thought so

magic basin
#

your last argument in function

#
  await myChannel.permissionOverwrites.edit(member, {
    'ViewChannel': true,
    'SendMessages': true,
  }, OverwriteType.Member);
#

first is member, second is <Object>: {...} and third is OverwriteType

thorn tulip
#

Didn’t I say that lol

hidden elbow
#

oh yeah, I chucked OverwriteType in and it had no effect

magic basin
#

but should be <Object>: {type: <value>}

magic basin
hidden elbow
#

right

thorn tulip
#

Ah

hidden elbow
magic basin
#

Thonk definitely not

hidden elbow
#

πŸ˜‚

mental hatch
magic basin
hidden elbow
#

shall I just remove the last argument? it seems to do nothing for this case...

magic basin
#

{type: <OvewriteType>}

hidden elbow
#

ohhhhhh

#

got it

magic basin
mental hatch
#

It's there so cache check is skipped

#

if not provided, it checks cache and if no role or member is found in cache, it throws an error

magic basin
#

ah I see

#

but user from event should be auto cached

mental hatch
#

Good practice to use that anyways imo

hidden elbow
#

Yeah no change, which I guess with that reason it's not to be expected

mental hatch
#

d.js has to try to set it internally if you don't (by checking cache and seeing what the snowflake represents)

hidden elbow
#

No chance it would have anything to do with the fact I've joined on the same account before?

magic basin
#

if it is so why does it return 817414141471752308 then

mental hatch
#

That would be the user the overwrite is for

hidden elbow
#

817414141471752308

#

yeah that's right

#

just copied the id just now

magic basin
#

but why not exactly overwrite?

mental hatch
#

what?

turbid boltBOT
mental hatch
#

The overwrite id is the id of the user/role

magic basin
#

cache is Collection <Snowflake, PermissionOverwrites>

mental hatch
#

Yeah, the .id is the user or role id

magic basin
#

but myChannel.permissionOverwrites.cache.get(member.id); returns snowflake

mental hatch
#

hmm, where do they say that?

magic basin
#

console.log("Added to server: " + member.user.tag + " to " + myChannel.name + " with perms: " + perms);

Added to server: Tobys578_#9323 to first-landing with perms: 817414141471752308

magic basin
mental hatch
#

They're stringifying it

magic basin
#

ah

#

wait stringfy returns id?

turbid boltBOT
hidden elbow
#
console.log("Added to server: " + member.user.tag + " to " + myChannel.name + " with perms: " + perms.PermissionOverwrites);

Unhandled promise rejection: TypeError: Cannot read properties of undefined (reading 'PermissionOverwrites')
at addUserToChannel (/bin/DiscordBots/merlin/bot.js:57:105)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

perms is undefined?

mental hatch
#

Yes

hidden elbow
#

andddd

magic basin
#

send actual code

hidden elbow
#

it's randomly started working

magic basin
#

HMMM

#

maybe you forgot to save?)

#

it can't just randomly start..

hidden elbow
#

don't think so πŸ˜‚

#

yeah

mental hatch
#

randomly started working
sounds like cache issue still Thinkeng

hidden elbow
mental hatch
#

Then save it

#

Or send the updated code

magic basin
#

if it's really cache issue, it can happen again

hidden elbow
#
async function addUserToChannel(member, guild, channelId){
  var myChannel = await guild.channels.cache.get(channelId);
  await myChannel.permissionOverwrites.edit(member, {
    'ViewChannel': true,
    'SendMessages': true,
  }, { type: OverwriteType.Member, reason: "Things and Stuff" });

  var perms = await myChannel.permissionOverwrites.cache.get(member.id);
  console.log("Added to server: " + member.user.tag + " to " + myChannel.name + " with perms: " + perms);
}

// Client Events

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('guildMemberAdd', async member => {
  addUserToChannel(member, member.guild, "1052526652707586068");
});

Anyway there's the full code if anyone else is searching the server for it

hidden elbow
magic basin
hidden elbow
#

maybe that's what fixed it then

magic basin
#

all other things as I see always cached/caches when you need it

hidden elbow
#

change just didn't save properly

mental hatch
#

So you didn't save, it broke
Now you saved, and it works?

hidden elbow
#

worked after I changed it to perms.PermissionOverrides and then back so yeah, must have missed saving it or my server cached it somehow before that

mental hatch
#

perms.PermissionOverrides?

#

doesn't exist

hidden elbow
#

yeah

#

that was just me messing around when we realised perms was returning the id

magic basin
hidden elbow
#

well, cheers for the help πŸ˜‚

turbid boltBOT
#

mdn Object.prototype.valueOf()
The valueOf() method of Object converts the this value to an object. This method is meant to be overridden by derived objects for custom type conversion logic.

mental hatch
#

Yeah if you go into source and look at Base class (which PermissionOverwrites extends), you'll see valueOf() returns the id