Merge pull request #76 from DarkView/k30_nameHistory
[K30] Migrated NameHistory
This commit is contained in:
commit
76b882d355
6 changed files with 151 additions and 0 deletions
46
backend/src/plugins/NameHistory/NameHistoryPlugin.ts
Normal file
46
backend/src/plugins/NameHistory/NameHistoryPlugin.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { PluginOptions } from "knub";
|
||||
import { NameHistoryPluginType, ConfigSchema } from "./types";
|
||||
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { GuildNicknameHistory } from "src/data/GuildNicknameHistory";
|
||||
import { UsernameHistory } from "src/data/UsernameHistory";
|
||||
import { Queue } from "src/Queue";
|
||||
import { NamesCmd } from "./commands/NamesCmd";
|
||||
import { ChannelJoinEvt, MessageCreateEvt } from "./events/UpdateNameEvts";
|
||||
|
||||
const defaultOptions: PluginOptions<NameHistoryPluginType> = {
|
||||
config: {
|
||||
can_view: false,
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
level: ">=50",
|
||||
config: {
|
||||
can_view: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const NameHistoryPlugin = zeppelinPlugin<NameHistoryPluginType>()("name_history", {
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
// prettier-ignore
|
||||
commands: [
|
||||
NamesCmd,
|
||||
],
|
||||
|
||||
// prettier-ignore
|
||||
events: [
|
||||
ChannelJoinEvt,
|
||||
MessageCreateEvt,
|
||||
],
|
||||
|
||||
onLoad(pluginData) {
|
||||
const { state, guild } = pluginData;
|
||||
|
||||
state.nicknameHistory = GuildNicknameHistory.getGuildInstance(guild.id);
|
||||
state.usernameHistory = new UsernameHistory();
|
||||
state.updateQueue = new Queue();
|
||||
},
|
||||
});
|
51
backend/src/plugins/NameHistory/commands/NamesCmd.ts
Normal file
51
backend/src/plugins/NameHistory/commands/NamesCmd.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { nameHistoryCmd } from "../types";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { disableCodeBlocks, createChunkedMessage } from "knub/dist/helpers";
|
||||
import { NICKNAME_RETENTION_PERIOD } from "src/data/cleanup/nicknames";
|
||||
import { DAYS } from "src/utils";
|
||||
import { MAX_NICKNAME_ENTRIES_PER_USER } from "src/data/GuildNicknameHistory";
|
||||
import { MAX_USERNAME_ENTRIES_PER_USER } from "src/data/UsernameHistory";
|
||||
import { sendErrorMessage } from "src/pluginUtils";
|
||||
|
||||
export const NamesCmd = nameHistoryCmd({
|
||||
trigger: "names",
|
||||
permission: "can_view",
|
||||
|
||||
signature: {
|
||||
userId: ct.userId(),
|
||||
},
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const nicknames = await pluginData.state.nicknameHistory.getByUserId(args.userId);
|
||||
const usernames = await pluginData.state.usernameHistory.getByUserId(args.userId);
|
||||
|
||||
if (nicknames.length === 0 && usernames.length === 0) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "No name history found");
|
||||
}
|
||||
|
||||
const nicknameRows = nicknames.map(
|
||||
r => `\`[${r.timestamp}]\` ${r.nickname ? `**${disableCodeBlocks(r.nickname)}**` : "*None*"}`,
|
||||
);
|
||||
const usernameRows = usernames.map(r => `\`[${r.timestamp}]\` **${disableCodeBlocks(r.username)}**`);
|
||||
|
||||
const user = pluginData.client.users.get(args.userId);
|
||||
const currentUsername = user ? `${user.username}#${user.discriminator}` : args.userId;
|
||||
|
||||
const nicknameDays = Math.round(NICKNAME_RETENTION_PERIOD / DAYS);
|
||||
const usernameDays = Math.round(NICKNAME_RETENTION_PERIOD / DAYS);
|
||||
|
||||
let message = `Name history for **${currentUsername}**:`;
|
||||
if (nicknameRows.length) {
|
||||
message += `\n\n__Last ${MAX_NICKNAME_ENTRIES_PER_USER} nicknames within ${nicknameDays} days:__\n${nicknameRows.join(
|
||||
"\n",
|
||||
)}`;
|
||||
}
|
||||
if (usernameRows.length) {
|
||||
message += `\n\n__Last ${MAX_USERNAME_ENTRIES_PER_USER} usernames within ${usernameDays} days:__\n${usernameRows.join(
|
||||
"\n",
|
||||
)}`;
|
||||
}
|
||||
|
||||
createChunkedMessage(msg.channel, message);
|
||||
},
|
||||
});
|
18
backend/src/plugins/NameHistory/events/UpdateNameEvts.ts
Normal file
18
backend/src/plugins/NameHistory/events/UpdateNameEvts.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { nameHistoryEvt } from "../types";
|
||||
import { updateNickname } from "../updateNickname";
|
||||
|
||||
export const ChannelJoinEvt = nameHistoryEvt({
|
||||
event: "voiceChannelJoin",
|
||||
|
||||
async listener(meta) {
|
||||
meta.pluginData.state.updateQueue.add(() => updateNickname(meta.pluginData, meta.args.member));
|
||||
},
|
||||
});
|
||||
|
||||
export const MessageCreateEvt = nameHistoryEvt({
|
||||
event: "messageCreate",
|
||||
|
||||
async listener(meta) {
|
||||
meta.pluginData.state.updateQueue.add(() => updateNickname(meta.pluginData, meta.args.message.member));
|
||||
},
|
||||
});
|
22
backend/src/plugins/NameHistory/types.ts
Normal file
22
backend/src/plugins/NameHistory/types.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import * as t from "io-ts";
|
||||
import { BasePluginType, command, eventListener } from "knub";
|
||||
import { GuildNicknameHistory } from "src/data/GuildNicknameHistory";
|
||||
import { UsernameHistory } from "src/data/UsernameHistory";
|
||||
import { Queue } from "src/Queue";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
can_view: t.boolean,
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export interface NameHistoryPluginType extends BasePluginType {
|
||||
config: TConfigSchema;
|
||||
state: {
|
||||
nicknameHistory: GuildNicknameHistory;
|
||||
usernameHistory: UsernameHistory;
|
||||
updateQueue: Queue;
|
||||
};
|
||||
}
|
||||
|
||||
export const nameHistoryCmd = command<NameHistoryPluginType>();
|
||||
export const nameHistoryEvt = eventListener<NameHistoryPluginType>();
|
12
backend/src/plugins/NameHistory/updateNickname.ts
Normal file
12
backend/src/plugins/NameHistory/updateNickname.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { Member } from "eris";
|
||||
import { PluginData } from "knub";
|
||||
import { NameHistoryPluginType } from "./types";
|
||||
|
||||
export async function updateNickname(pluginData: PluginData<NameHistoryPluginType>, member: Member) {
|
||||
if (!member) return;
|
||||
const latestEntry = await pluginData.state.nicknameHistory.getLastEntry(member.id);
|
||||
if (!latestEntry || latestEntry.nickname !== member.nick) {
|
||||
if (!latestEntry && member.nick == null) return; // No need to save "no nickname" if there's no previous data
|
||||
await pluginData.state.nicknameHistory.addEntry(member.id, member.nick);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { UtilityPlugin } from "./Utility/UtilityPlugin";
|
||||
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
||||
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
||||
import { NameHistoryPlugin } from "./NameHistory/NameHistoryPlugin";
|
||||
import { MessageSaverPlugin } from "./MessageSaver/MessageSaverPlugin";
|
||||
import { AutoReactionsPlugin } from "./AutoReactions/AutoReactionsPlugin";
|
||||
import { RemindersPlugin } from "./Reminders/RemindersPlugin";
|
||||
|
@ -11,6 +12,7 @@ import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin";
|
|||
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||
AutoReactionsPlugin,
|
||||
LocateUserPlugin,
|
||||
NameHistoryPlugin,
|
||||
MessageSaverPlugin,
|
||||
RemindersPlugin,
|
||||
UsernameSaverPlugin,
|
||||
|
|
Loading…
Add table
Reference in a new issue