3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

feat: add internal role manager plugin; add role buttons plugin

This commit is contained in:
Dragory 2022-04-23 16:31:41 +03:00
parent 9314d57645
commit 3fe71b3e27
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
23 changed files with 732 additions and 1 deletions

View file

@ -0,0 +1,69 @@
import { GuildPluginData } from "knub";
import { RoleManagerPluginType } from "../types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { logger } from "../../../logger";
import { RoleQueueItem } from "../../../data/entities/RoleQueueItem";
const ROLE_ASSIGNMENTS_PER_BATCH = 20;
export async function runRoleAssignmentLoop(pluginData: GuildPluginData<RoleManagerPluginType>) {
if (pluginData.state.roleAssignmentLoopRunning || pluginData.state.abortRoleAssignmentLoop) {
return;
}
pluginData.state.roleAssignmentLoopRunning = true;
while (true) {
// Abort on unload
if (pluginData.state.abortRoleAssignmentLoop) {
break;
}
if (!pluginData.state.roleAssignmentLoopRunning) {
break;
}
await (pluginData.state.pendingRoleAssignmentPromise = (async () => {
// Process assignments in batches, stopping once the queue's exhausted
const nextAssignments = await pluginData.state.roleQueue.consumeNextRoleAssignments(ROLE_ASSIGNMENTS_PER_BATCH);
if (nextAssignments.length === 0) {
pluginData.state.roleAssignmentLoopRunning = false;
return;
}
// Remove assignments that cancel each other out (e.g. from spam-clicking a role button)
const validAssignments = new Map<string, RoleQueueItem>();
for (const assignment of nextAssignments) {
const key = `${assignment.should_add ? 1 : 0}|${assignment.user_id}|${assignment.role_id}`;
const oppositeKey = `${assignment.should_add ? 0 : 1}|${assignment.user_id}|${assignment.role_id}`;
if (validAssignments.has(oppositeKey)) {
validAssignments.delete(oppositeKey);
continue;
}
validAssignments.set(key, assignment);
}
// Apply batch in parallel
await Promise.all(
Array.from(validAssignments.values()).map(async (assignment) => {
const member = await pluginData.guild.members.fetch(assignment.user_id).catch(() => null);
if (!member) {
return;
}
const operation = assignment.should_add
? member.roles.add(assignment.role_id)
: member.roles.remove(assignment.role_id);
await operation.catch((err) => {
logger.warn(err);
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Could not ${assignment.should_add ? "assign" : "remove"} role <@&${assignment.role_id}> (\`${
assignment.role_id
}\`) ${assignment.should_add ? "to" : "from"} <@!${assignment.user_id}> (\`${assignment.user_id}\`)`,
});
});
}),
);
})());
}
}