Merge branch 'knub30' into k30_usernameSaver

This commit is contained in:
Miikka 2020-07-21 18:17:23 +03:00 committed by GitHub
commit 59c60e753b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 0 deletions

View 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);
},
});

View file

@ -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),
});
}
}
},
});

View 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>();

View file

@ -2,12 +2,14 @@ import { UtilityPlugin } from "./Utility/UtilityPlugin";
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
import { UsernameSaverPlugin } from "./UsernameSaver/UsernameSaverPlugin";
import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin";
// prettier-ignore
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
LocateUserPlugin,
UsernameSaverPlugin,
UtilityPlugin,
WelcomeMessagePlugin,
];
export const globalPlugins = [];