mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
feat: add member cache; handle all role changes with RoleManagerPlugin; exit gracefully
This commit is contained in:
parent
fd60a09947
commit
fa50110766
48 changed files with 755 additions and 264 deletions
|
@ -1,10 +1,9 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { RoleQueueItem } from "../../../data/entities/RoleQueueItem";
|
||||
import { logger } from "../../../logger";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { RoleManagerPluginType } from "../types";
|
||||
|
||||
const ROLE_ASSIGNMENTS_PER_BATCH = 20;
|
||||
const ROLE_ASSIGNMENTS_PER_BATCH = 10;
|
||||
|
||||
export async function runRoleAssignmentLoop(pluginData: GuildPluginData<RoleManagerPluginType>) {
|
||||
if (pluginData.state.roleAssignmentLoopRunning || pluginData.state.abortRoleAssignmentLoop) {
|
||||
|
@ -30,40 +29,25 @@ export async function runRoleAssignmentLoop(pluginData: GuildPluginData<RoleMana
|
|||
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;
|
||||
const member = await pluginData.guild.members.fetch(assignment.user_id).catch(() => null);
|
||||
if (!member) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
|
||||
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}\`)`,
|
||||
});
|
||||
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}\`)`,
|
||||
});
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
})());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue