improve function to resolve member

not actually used yet, waiting for upstream to implement it
This commit is contained in:
MathMan05 2024-08-10 15:44:08 -05:00
parent 82cbf40a22
commit 4e77b7c66c
4 changed files with 111 additions and 20 deletions

View file

@ -197,6 +197,9 @@ class Localuser {
this.unload();
document.getElementById("loading").classList.remove("doneloading");
document.getElementById("loading").classList.add("loading");
this.fetchingmembers = new Map();
this.noncemap = new Map();
this.noncebuild = new Map();
if (((event.code > 1000 && event.code < 1016) || wsCodesRetry.has(event.code))) {
if (this.connectionSucceed !== 0 && Date.now() > this.connectionSucceed + 20000)
this.errorBackoff = 0;
@ -304,6 +307,9 @@ class Localuser {
message.takeReaction(temp.d.emoji, temp.d.user_id);
}
break;
case "GUILD_MEMBERS_CHUNK":
this.gotChunk(temp.d);
break;
}
}
else if (temp.op === 10) {
@ -1029,8 +1035,8 @@ class Localuser {
//---------- resolving members code -----------
waitingmembers = new Map();
async resolvemember(id, guildid) {
console.warn("this function is currently non-functional due to it not being implemented in the server");
throw new Error("Not implemented on the server side and not fully implemented, do not use");
console.warn("this function may or may not work on any instance, use at your own risk");
//throw new Error("Not implemented on the server side and not fully implemented, do not use");
if (!this.waitingmembers.has(guildid)) {
this.waitingmembers.set(guildid, new Map());
}
@ -1043,6 +1049,25 @@ class Localuser {
return await promise;
}
fetchingmembers = new Map();
noncemap = new Map();
noncebuild = new Map();
async gotChunk(chunk) {
console.log(chunk);
chunk.members ??= [];
const arr = this.noncebuild.get(chunk.nonce);
arr[0] = arr[0].concat(chunk.members);
if (chunk.not_found) {
arr[1] = chunk.not_found;
}
arr[2].push(chunk.chunk_index);
if (arr[2].length === chunk.chunk_count) {
console.log("got through");
this.noncebuild.delete(chunk.nonce);
const func = this.noncemap.get(chunk.nonce);
func([arr[0], arr[1]]);
this.noncemap.delete(chunk.nonce);
}
}
async getmembers() {
if (this.ws) {
this.waitingmembers.forEach(async (value, guildid) => {
@ -1053,28 +1078,50 @@ class Localuser {
const build = [];
for (const key of keys) {
build.push(key);
if (build.length === 100) {
break;
}
}
;
if (!build.length) {
this.waitingmembers.delete(guildid);
return;
}
;
let res;
const promise = new Promise((r) => {
res = r;
});
const nonce = "" + Math.floor(Math.random() * 100000000000);
this.noncemap.set(nonce, res);
this.noncebuild.set(nonce, [[], [], []]);
this.ws.send(JSON.stringify({
op: 8,
d: {
query: "",
user_ids: build,
guild_id: guildid,
limit: 100,
nonce: "" + Math.floor(Math.random() * 100000000)
nonce,
//presences:true
}
}));
this.fetchingmembers.set(guildid, res);
const data = await promise;
this.fetchingmembers.set(guildid, true);
const prom = await promise;
;
const data = prom[0];
for (const thing of data) {
value.get(thing.id)(thing);
value.delete(thing.id);
if (value.has(thing.id)) {
value.get(thing.id)(thing);
value.delete(thing.id);
}
}
for (const thing of prom[1]) {
if (value.has(thing)) {
value.get(thing)(undefined);
value.delete(thing);
}
}
this.fetchingmembers.delete(guildid);
this.getmembers();
});
}