mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Migrated Persist to new Plugin structure
This commit is contained in:
parent
b6257b9189
commit
e1323687a7
5 changed files with 144 additions and 0 deletions
52
backend/src/plugins/Persist/events/LoadDataEvt.ts
Normal file
52
backend/src/plugins/Persist/events/LoadDataEvt.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { persistEvent } from "../types";
|
||||
import { MemberOptions } from "eris";
|
||||
import intersection from "lodash.intersection";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { stripObjectToScalars } from "src/utils";
|
||||
|
||||
export const LoadDataEvt = persistEvent({
|
||||
event: "guildMemberAdd",
|
||||
|
||||
async listener(meta) {
|
||||
const member = meta.args.member;
|
||||
const pluginData = meta.pluginData;
|
||||
|
||||
const memberRolesLock = await pluginData.locks.acquire(`member-roles-${member.id}`);
|
||||
|
||||
const persistedData = await pluginData.state.persistedData.find(member.id);
|
||||
if (!persistedData) {
|
||||
memberRolesLock.unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
const toRestore: MemberOptions = {};
|
||||
const config = pluginData.config.getForMember(member);
|
||||
const restoredData = [];
|
||||
|
||||
const persistedRoles = config.persisted_roles;
|
||||
if (persistedRoles.length) {
|
||||
const rolesToRestore = intersection(persistedRoles, persistedData.roles);
|
||||
if (rolesToRestore.length) {
|
||||
restoredData.push("roles");
|
||||
toRestore.roles = Array.from(new Set([...rolesToRestore, ...member.roles]));
|
||||
}
|
||||
}
|
||||
|
||||
if (config.persist_nicknames && persistedData.nickname) {
|
||||
restoredData.push("nickname");
|
||||
toRestore.nick = persistedData.nickname;
|
||||
}
|
||||
|
||||
if (restoredData.length) {
|
||||
await member.edit(toRestore, "Restored upon rejoin");
|
||||
await pluginData.state.persistedData.clear(member.id);
|
||||
|
||||
pluginData.state.logs.log(LogType.MEMBER_RESTORE, {
|
||||
member: stripObjectToScalars(member, ["user", "roles"]),
|
||||
restoredData: restoredData.join(", "),
|
||||
});
|
||||
}
|
||||
|
||||
memberRolesLock.unlock();
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue