mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Reorganize project. Add folder for shared code between backend/dashboard. Switch from jest to ava for tests.
This commit is contained in:
parent
80a82fe348
commit
16111bbe84
162 changed files with 11056 additions and 9900 deletions
80
backend/src/plugins/WelcomeMessage.ts
Normal file
80
backend/src/plugins/WelcomeMessage.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
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";
|
||||
|
||||
const ConfigSchema = t.type({
|
||||
send_dm: t.boolean,
|
||||
send_to_channel: tNullable(t.string),
|
||||
message: t.string,
|
||||
});
|
||||
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export class WelcomeMessagePlugin extends ZeppelinPlugin<TConfigSchema> {
|
||||
public static pluginName = "welcome_message";
|
||||
public static configSchema = ConfigSchema;
|
||||
|
||||
public static pluginInfo = {
|
||||
prettyName: "Welcome message",
|
||||
};
|
||||
|
||||
protected logs: GuildLogs;
|
||||
|
||||
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
||||
return {
|
||||
config: {
|
||||
send_dm: false,
|
||||
send_to_channel: null,
|
||||
message: null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
protected onLoad() {
|
||||
this.logs = new GuildLogs(this.guildId);
|
||||
}
|
||||
|
||||
@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 {
|
||||
await createChunkedMessage(dmChannel, formatted);
|
||||
} catch (e) {
|
||||
this.logs.log(LogType.BOT_ALERT, {
|
||||
body: `Failed send a welcome DM to {userMention(member)}`,
|
||||
member: stripObjectToScalars(member),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (config.send_to_channel) {
|
||||
const channel = this.guild.channels.get(config.send_to_channel);
|
||||
if (!channel || !(channel instanceof TextChannel)) return;
|
||||
|
||||
try {
|
||||
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),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue