zappyzep/src/plugins/WelcomeMessage.ts

81 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-05-03 23:56:38 +03:00
import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { decorators as d, IPluginOptions } from "knub";
import { Member, TextChannel } from "eris";
import { renderTemplate } from "../templateFormatter";
import { createChunkedMessage, stripObjectToScalars, tNullable } from "../utils";
import { LogType } from "../data/LogType";
import { GuildLogs } from "../data/GuildLogs";
import * as t from "io-ts";
2019-05-03 23:56:38 +03:00
const ConfigSchema = t.type({
send_dm: t.boolean,
send_to_channel: tNullable(t.string),
message: t.string,
});
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
2019-05-03 23:56:38 +03:00
export class WelcomeMessagePlugin extends ZeppelinPlugin<TConfigSchema> {
2019-05-03 23:56:38 +03:00
public static pluginName = "welcome_message";
2019-08-22 02:58:32 +03:00
public static configSchema = ConfigSchema;
public static pluginInfo = {
prettyName: "Welcome message",
};
2019-05-03 23:56:38 +03:00
protected logs: GuildLogs;
2019-08-22 02:58:32 +03:00
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
2019-05-03 23:56:38 +03:00
return {
config: {
send_dm: false,
send_to_channel: null,
message: null,
},
};
}
protected onLoad() {
this.logs = new GuildLogs(this.guildId);
}
2019-05-03 23:56:38 +03:00
@d.event("guildMemberAdd")
async onGuildMemberAdd(_, member: Member) {
const config = this.getConfig();
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 {
2019-10-11 22:39:49 +03:00
await createChunkedMessage(dmChannel, formatted);
} catch (e) {
this.logs.log(LogType.BOT_ALERT, {
body: `Failed send a welcome DM to {userMention(member)}`,
member: stripObjectToScalars(member),
});
}
2019-05-03 23:56:38 +03:00
}
if (config.send_to_channel) {
const channel = this.guild.channels.get(config.send_to_channel);
if (!channel || !(channel instanceof TextChannel)) return;
try {
2019-10-11 22:39:49 +03:00
await createChunkedMessage(channel, formatted);
} catch (e) {
this.logs.log(LogType.BOT_ALERT, {
body: `Failed send a welcome message for {userMention(member)} to {channelMention(channel)}`,
member: stripObjectToScalars(member),
channel: stripObjectToScalars(channel),
});
}
2019-05-03 23:56:38 +03:00
}
}
}