Merge branch 'knub30' into k30_reminders
This commit is contained in:
commit
bc0bf118c0
10 changed files with 172 additions and 1 deletions
|
@ -13,7 +13,7 @@ export const FollowCmd = locateUserCommand({
|
||||||
|
|
||||||
signature: {
|
signature: {
|
||||||
member: ct.resolvedMember(),
|
member: ct.resolvedMember(),
|
||||||
reminder: ct.string({ required: false, rest: true }),
|
reminder: ct.string({ required: false, catchAll: true }),
|
||||||
|
|
||||||
duration: ct.delay({ option: true, shortcut: "d" }),
|
duration: ct.delay({ option: true, shortcut: "d" }),
|
||||||
active: ct.bool({ option: true, shortcut: "a" }),
|
active: ct.bool({ option: true, shortcut: "a" }),
|
||||||
|
|
21
backend/src/plugins/UsernameSaver/UsernameSaverPlugin.ts
Normal file
21
backend/src/plugins/UsernameSaver/UsernameSaverPlugin.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||||
|
import { UsernameHistory } from "src/data/UsernameHistory";
|
||||||
|
import { Queue } from "src/Queue";
|
||||||
|
import { UsernameSaverPluginType } from "./types";
|
||||||
|
import { MessageCreateEvt } from "./events/MessageCreateEvt";
|
||||||
|
import { VoiceChannelJoinEvt } from "./events/VoiceChannelJoinEvt";
|
||||||
|
|
||||||
|
export const UsernameSaverPlugin = zeppelinPlugin<UsernameSaverPluginType>()("username_saver", {
|
||||||
|
// prettier-ignore
|
||||||
|
events: [
|
||||||
|
MessageCreateEvt,
|
||||||
|
VoiceChannelJoinEvt,
|
||||||
|
],
|
||||||
|
|
||||||
|
onLoad(pluginData) {
|
||||||
|
const { state, guild } = pluginData;
|
||||||
|
|
||||||
|
state.usernameHistory = new UsernameHistory();
|
||||||
|
state.updateQueue = new Queue();
|
||||||
|
},
|
||||||
|
});
|
11
backend/src/plugins/UsernameSaver/events/MessageCreateEvt.ts
Normal file
11
backend/src/plugins/UsernameSaver/events/MessageCreateEvt.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { usernameEvent } from "../types";
|
||||||
|
import { updateUsername } from "../updateUsername";
|
||||||
|
|
||||||
|
export const MessageCreateEvt = usernameEvent({
|
||||||
|
event: "messageCreate",
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
if (meta.args.message.author.bot) return;
|
||||||
|
meta.pluginData.state.updateQueue.add(() => updateUsername(meta.pluginData, meta.args.message.author));
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { usernameEvent } from "../types";
|
||||||
|
import { updateUsername } from "../updateUsername";
|
||||||
|
|
||||||
|
export const VoiceChannelJoinEvt = usernameEvent({
|
||||||
|
event: "voiceChannelJoin",
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
if (meta.args.member.bot) return;
|
||||||
|
meta.pluginData.state.updateQueue.add(() => updateUsername(meta.pluginData, meta.args.member.user));
|
||||||
|
},
|
||||||
|
});
|
12
backend/src/plugins/UsernameSaver/types.ts
Normal file
12
backend/src/plugins/UsernameSaver/types.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { BasePluginType, eventListener } from "knub";
|
||||||
|
import { UsernameHistory } from "src/data/UsernameHistory";
|
||||||
|
import { Queue } from "src/Queue";
|
||||||
|
|
||||||
|
export interface UsernameSaverPluginType extends BasePluginType {
|
||||||
|
state: {
|
||||||
|
usernameHistory: UsernameHistory;
|
||||||
|
updateQueue: Queue;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const usernameEvent = eventListener<UsernameSaverPluginType>();
|
12
backend/src/plugins/UsernameSaver/updateUsername.ts
Normal file
12
backend/src/plugins/UsernameSaver/updateUsername.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { User } from "eris";
|
||||||
|
import { PluginData } from "knub";
|
||||||
|
import { UsernameSaverPluginType } from "./types";
|
||||||
|
|
||||||
|
export async function updateUsername(pluginData: PluginData<UsernameSaverPluginType>, user: User) {
|
||||||
|
if (!user) return;
|
||||||
|
const newUsername = `${user.username}#${user.discriminator}`;
|
||||||
|
const latestEntry = await pluginData.state.usernameHistory.getLastEntry(user.id);
|
||||||
|
if (!latestEntry || newUsername !== latestEntry.username) {
|
||||||
|
await pluginData.state.usernameHistory.addEntry(user.id, newUsername);
|
||||||
|
}
|
||||||
|
}
|
29
backend/src/plugins/WelcomeMessage/WelcomeMessagePlugin.ts
Normal file
29
backend/src/plugins/WelcomeMessage/WelcomeMessagePlugin.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||||
|
import { PluginOptions } from "knub";
|
||||||
|
import { WelcomeMessagePluginType, ConfigSchema } from "./types";
|
||||||
|
import { GuildLogs } from "src/data/GuildLogs";
|
||||||
|
import { GuildMemberAddEvt } from "./events/GuildMemberAddEvt";
|
||||||
|
|
||||||
|
const defaultOptions: PluginOptions<WelcomeMessagePluginType> = {
|
||||||
|
config: {
|
||||||
|
send_dm: false,
|
||||||
|
send_to_channel: null,
|
||||||
|
message: "",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const WelcomeMessagePlugin = zeppelinPlugin<WelcomeMessagePluginType>()("welcome_message", {
|
||||||
|
configSchema: ConfigSchema,
|
||||||
|
defaultOptions,
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
events: [
|
||||||
|
GuildMemberAddEvt,
|
||||||
|
],
|
||||||
|
|
||||||
|
onLoad(pluginData) {
|
||||||
|
const { state, guild } = pluginData;
|
||||||
|
|
||||||
|
state.logs = new GuildLogs(guild.id);
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,51 @@
|
||||||
|
import { welcomeEvent } from "../types";
|
||||||
|
import { renderTemplate } from "src/templateFormatter";
|
||||||
|
import { stripObjectToScalars, createChunkedMessage } from "src/utils";
|
||||||
|
import { LogType } from "src/data/LogType";
|
||||||
|
import { TextChannel } from "eris";
|
||||||
|
|
||||||
|
export const GuildMemberAddEvt = welcomeEvent({
|
||||||
|
event: "guildMemberAdd",
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
const pluginData = meta.pluginData;
|
||||||
|
const member = meta.args.member;
|
||||||
|
|
||||||
|
const config = pluginData.config.get();
|
||||||
|
if (!config.message) return;
|
||||||
|
if (!config.send_dm && !config.send_to_channel) return;
|
||||||
|
|
||||||
|
const formatted = await renderTemplate(config.message, {
|
||||||
|
member: stripObjectToScalars(member, ["user"]),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config.send_dm) {
|
||||||
|
const dmChannel = await member.user.getDMChannel();
|
||||||
|
if (!dmChannel) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await createChunkedMessage(dmChannel, formatted);
|
||||||
|
} catch (e) {
|
||||||
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
|
body: `Failed send a welcome DM to {userMention(member)}`,
|
||||||
|
member: stripObjectToScalars(member),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.send_to_channel) {
|
||||||
|
const channel = meta.args.guild.channels.get(config.send_to_channel);
|
||||||
|
if (!channel || !(channel instanceof TextChannel)) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await createChunkedMessage(channel, formatted);
|
||||||
|
} catch (e) {
|
||||||
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
|
body: `Failed send a welcome message for {userMention(member)} to {channelMention(channel)}`,
|
||||||
|
member: stripObjectToScalars(member),
|
||||||
|
channel: stripObjectToScalars(channel),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
20
backend/src/plugins/WelcomeMessage/types.ts
Normal file
20
backend/src/plugins/WelcomeMessage/types.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import * as t from "io-ts";
|
||||||
|
import { BasePluginType, eventListener } from "knub";
|
||||||
|
import { tNullable } from "src/utils";
|
||||||
|
import { GuildLogs } from "src/data/GuildLogs";
|
||||||
|
|
||||||
|
export const ConfigSchema = t.type({
|
||||||
|
send_dm: t.boolean,
|
||||||
|
send_to_channel: tNullable(t.string),
|
||||||
|
message: t.string,
|
||||||
|
});
|
||||||
|
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||||
|
|
||||||
|
export interface WelcomeMessagePluginType extends BasePluginType {
|
||||||
|
config: TConfigSchema;
|
||||||
|
state: {
|
||||||
|
logs: GuildLogs;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const welcomeEvent = eventListener<WelcomeMessagePluginType>();
|
|
@ -2,12 +2,16 @@ import { UtilityPlugin } from "./Utility/UtilityPlugin";
|
||||||
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
||||||
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
||||||
import { RemindersPlugin } from "./Reminders/RemindersPlugin";
|
import { RemindersPlugin } from "./Reminders/RemindersPlugin";
|
||||||
|
import { UsernameSaverPlugin } from "./UsernameSaver/UsernameSaverPlugin";
|
||||||
|
import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin";
|
||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||||
LocateUserPlugin,
|
LocateUserPlugin,
|
||||||
RemindersPlugin,
|
RemindersPlugin,
|
||||||
|
UsernameSaverPlugin,
|
||||||
UtilityPlugin,
|
UtilityPlugin,
|
||||||
|
WelcomeMessagePlugin,
|
||||||
];
|
];
|
||||||
|
|
||||||
export const globalPlugins = [];
|
export const globalPlugins = [];
|
||||||
|
|
Loading…
Add table
Reference in a new issue