mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +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
33
backend/src/plugins/Persist/PersistPlugin.ts
Normal file
33
backend/src/plugins/Persist/PersistPlugin.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { PluginOptions } from "knub";
|
||||
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { PersistPluginType, ConfigSchema } from "./types";
|
||||
import { GuildPersistedData } from "src/data/GuildPersistedData";
|
||||
import { GuildLogs } from "src/data/GuildLogs";
|
||||
import { StoreDataEvt } from "./events/StoreDataEvt";
|
||||
import { LoadDataEvt } from "./events/LoadDataEvt";
|
||||
|
||||
const defaultOptions: PluginOptions<PersistPluginType> = {
|
||||
config: {
|
||||
persisted_roles: [],
|
||||
persist_nicknames: false,
|
||||
persist_voice_mutes: false,
|
||||
},
|
||||
};
|
||||
|
||||
export const PersistPlugin = zeppelinPlugin<PersistPluginType>()("persist", {
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
// prettier-ignore
|
||||
events: [
|
||||
StoreDataEvt,
|
||||
LoadDataEvt,
|
||||
],
|
||||
|
||||
onLoad(pluginData) {
|
||||
const { state, guild } = pluginData;
|
||||
|
||||
state.persistedData = GuildPersistedData.getGuildInstance(guild.id);
|
||||
state.logs = new GuildLogs(guild.id);
|
||||
},
|
||||
});
|
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();
|
||||
},
|
||||
});
|
35
backend/src/plugins/Persist/events/StoreDataEvt.ts
Normal file
35
backend/src/plugins/Persist/events/StoreDataEvt.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { persistEvent } from "../types";
|
||||
import { IPartialPersistData } from "src/data/GuildPersistedData";
|
||||
import { Member } from "eris";
|
||||
import intersection from "lodash.intersection";
|
||||
|
||||
export const StoreDataEvt = persistEvent({
|
||||
event: "guildMemberRemove",
|
||||
|
||||
async listener(meta) {
|
||||
const member = meta.args.member as Member;
|
||||
const pluginData = meta.pluginData;
|
||||
|
||||
let persist = false;
|
||||
const persistData: IPartialPersistData = {};
|
||||
const config = pluginData.config.getForUser(member.user);
|
||||
|
||||
const persistedRoles = config.persisted_roles;
|
||||
if (persistedRoles.length && member.roles) {
|
||||
const rolesToPersist = intersection(persistedRoles, member.roles);
|
||||
if (rolesToPersist.length) {
|
||||
persist = true;
|
||||
persistData.roles = rolesToPersist;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.persist_nicknames && member.nick) {
|
||||
persist = true;
|
||||
persistData.nickname = member.nick;
|
||||
}
|
||||
|
||||
if (persist) {
|
||||
pluginData.state.persistedData.set(member.id, persistData);
|
||||
}
|
||||
},
|
||||
});
|
22
backend/src/plugins/Persist/types.ts
Normal file
22
backend/src/plugins/Persist/types.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import * as t from "io-ts";
|
||||
import { BasePluginType, eventListener } from "knub";
|
||||
import { GuildPersistedData } from "src/data/GuildPersistedData";
|
||||
import { GuildLogs } from "src/data/GuildLogs";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
persisted_roles: t.array(t.string),
|
||||
persist_nicknames: t.boolean,
|
||||
persist_voice_mutes: t.boolean, // Deprecated, here to not break old configs
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export interface PersistPluginType extends BasePluginType {
|
||||
config: TConfigSchema;
|
||||
|
||||
state: {
|
||||
persistedData: GuildPersistedData;
|
||||
logs: GuildLogs;
|
||||
};
|
||||
}
|
||||
|
||||
export const persistEvent = eventListener<PersistPluginType>();
|
|
@ -1,10 +1,12 @@
|
|||
import { UtilityPlugin } from "./Utility/UtilityPlugin";
|
||||
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
||||
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
||||
import { PersistPlugin } from "./Persist/PersistPlugin";
|
||||
|
||||
// prettier-ignore
|
||||
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||
LocateUserPlugin,
|
||||
PersistPlugin,
|
||||
UtilityPlugin,
|
||||
];
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue