Skip to content

Commit fc3d187

Browse files
Improve experimental member fetching
Co-Authored-By: MathMan05 <[email protected]>
1 parent 81257b5 commit fc3d187

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

webpage/localuser.js

+64-11
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class LocalUser {
168168
document.getElementById("loading").classList.remove("doneloading")
169169
document.getElementById("loading").classList.add("loading")
170170

171+
this.fetchingmembers = new Map()
172+
this.noncemap = new Map()
173+
this.noncebuild = new Map()
174+
171175
if (((event.code > 1000 && event.code < 1016) || wsCodesRetry.has(event.code))) {
172176
if (connectionSucceed != 0 && Date.now() > connectionSucceed + 20000) errorBackoff = 0
173177
else errorBackoff = Math.min(errorBackoff + 1, 40)
@@ -261,6 +265,9 @@ class LocalUser {
261265
document.getElementById("servers").insertBefore(guildy.generateGuildIcon(), document.getElementById("bottomseparator"))
262266
break
263267
}
268+
case "GUILD_MEMBERS_CHUNK":
269+
this.gotChunk(json.d)
270+
break
264271
}
265272
} else if (json.op == 1) this.ws.send(JSON.stringify({ op: 1, d: this.lastSequence }))
266273
else if (json.op == 10) {
@@ -983,7 +990,7 @@ class LocalUser {
983990
"",
984991
"Reset token",
985992
async () => {
986-
if (!confirm("Are you sure you want to reset the bot token? Your bot will stop working until you update it.")) return
993+
if (!confirm("Are you sure you want to reset the token? Your bot will stop working until you update it.")) return
987994

988995
const updateRes = await fetch(instance.api + "/applications/" + appId + "/bot/reset", {
989996
method: "POST",
@@ -1013,10 +1020,8 @@ class LocalUser {
10131020

10141021
waitingmembers = new Map()
10151022
async resolvemember(id, guildid) {
1016-
console.warn("this function is currently non-functional, either due to a bug in the client or the server, it's currently unclear, use at your own risk")
1017-
if (!this.waitingmembers.has(guildid)) {
1018-
this.waitingmembers.set(guildid, new Map())
1019-
}
1023+
if (!this.waitingmembers.has(guildid)) this.waitingmembers.set(guildid, new Map())
1024+
10201025
let res
10211026
const promise = new Promise(r => {
10221027
res = r
@@ -1026,7 +1031,31 @@ class LocalUser {
10261031
return await promise
10271032
}
10281033
fetchingmembers = new Map()
1034+
noncemap = new Map()
1035+
noncebuild = new Map()
1036+
async gotChunk(chunk) {
1037+
chunk.members ??= []
1038+
const arr = this.noncebuild.get(chunk.nonce)
1039+
arr[0] = arr[0].concat(chunk.members)
1040+
if (chunk.not_found) {
1041+
arr[1] = chunk.not_found
1042+
}
1043+
arr[2].push(chunk.chunk_index)
1044+
if (arr[2].length == chunk.chunk_count) {
1045+
this.noncebuild.delete(chunk.nonce)
1046+
const func = this.noncemap.get(chunk.nonce)
1047+
func([arr[0], arr[1]])
1048+
this.noncemap.delete(chunk.nonce)
1049+
}
1050+
}
10291051
async getmembers() {
1052+
let res
1053+
const promise = new Promise(r => {
1054+
res = r
1055+
})
1056+
setTimeout(res, 10)
1057+
await promise //allow for more to be sent at once
1058+
10301059
if (this.ws) {
10311060
this.waitingmembers.forEach(async (value, guildid) => {
10321061
const keys = value.keys()
@@ -1035,27 +1064,51 @@ class LocalUser {
10351064
const build = []
10361065
for (const key of keys) {
10371066
build.push(key)
1067+
if (build.length == 100) break
10381068
}
10391069

1040-
let res
1041-
const promise = new Promise(r => {
1042-
res = r
1070+
if (!build.length) {
1071+
this.waitingmembers.delete(guildid)
1072+
return
1073+
}
1074+
1075+
let res2
1076+
const promise2 = new Promise(r => {
1077+
res2 = r
10431078
})
1079+
1080+
const nonce = "" + Math.floor(Math.random() * 100000000000)
1081+
this.noncemap.set(nonce, res2)
1082+
this.noncebuild.set(nonce, [[], [], []])
10441083
this.ws.send(JSON.stringify({
10451084
op: 8,
10461085
d: {
10471086
user_ids: build,
10481087
guild_id: guildid,
10491088
limit: 100,
1050-
nonce: "" + Math.floor(Math.random() * 100000000)
1089+
//presences: true,
1090+
nonce
10511091
}
10521092
}))
1053-
this.fetchingmembers.set(guildid, res)
1054-
const data = await promise
1093+
this.fetchingmembers.set(guildid, true)
1094+
const prom = await promise2
1095+
1096+
const data = prom[0]
10551097
for (const thing of data) {
10561098
value.get(thing.id)(thing)
10571099
value.delete(thing.id)
1100+
if (value.has(thing.id)) {
1101+
value.get(thing.id)(thing)
1102+
value.delete(thing.id)
1103+
}
1104+
}
1105+
for (const thing of prom[1]) {
1106+
if (value.has(thing)) {
1107+
value.get(thing)()
1108+
value.delete(thing)
1109+
}
10581110
}
1111+
this.fetchingmembers.delete(guildid)
10591112
this.getmembers()
10601113
})
10611114
}

webpage/member.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class Member {
8080
return memb
8181
}
8282

83+
guild.localuser.resolvemember(id.id, guild.id).then(console.log)
8384
const fetchPromise = fetch(instance.api + "/users/" + id + "/profile?with_mutual_guilds=true&with_mutual_friends_count=true" + (guild.id == "@me" ? "" : "&guild_id=" + guild.id), {
8485
headers: guild.headers
8586
})
8687
fetchPromise.catch(console.warn)
87-
guild.localuser.resolvemember(id?.id, guild.id)
8888
const promise = fetchPromise.then(res => res.json()).then(json => {
8989
const memb = new Member(json, guild)
9090
Member.already[guild.id][id] = memb

0 commit comments

Comments
 (0)