diff --git a/src/discord.ts b/src/discord.ts index e1dd97e5..d47d08e0 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -28,6 +28,7 @@ import { PinPrefixEvent } from './events/pin-prefix' import { VCSpeechLogMessageUrlEvent } from './events/vc-speech-log-url' import { OriginCommand } from './commands/origin' import { JoinedNotifierEvent } from './events/joined-notifier' +import { LeavedNotififerEvent } from './events/leaved-notifier' import { TweetEmbedEvent } from './events/tweet-embed' export class Discord { @@ -76,6 +77,7 @@ export class Discord { new PinPrefixEvent(this), new VCSpeechLogMessageUrlEvent(this), new JoinedNotifierEvent(this), + new LeavedNotififerEvent(this), new TweetEmbedEvent(this), ] for (const event of events) { diff --git a/src/events/leaved-notifier.ts b/src/events/leaved-notifier.ts new file mode 100644 index 00000000..7064bd46 --- /dev/null +++ b/src/events/leaved-notifier.ts @@ -0,0 +1,32 @@ +import { ChannelType, GuildMember } from 'discord.js' +import { BaseDiscordEvent } from '.' +import { Configuration } from '@/config' + +/** + * ユーザーがサーバから退出した際、以下の処理を行う + * - #general で退出したことを通知する + */ +export class LeavedNotififerEvent extends BaseDiscordEvent<'guildMemberRemove'> { + get eventName(): 'guildMemberRemove' { + return 'guildMemberRemove' + } + + async execute(member: GuildMember): Promise { + const config: Configuration = this.discord.getConfig() + const generalChannelId = + config.get('discord').channel?.general || '1138605147287728150' + + const generalChannel = + await this.discord.client.channels.fetch(generalChannelId) + if (!generalChannel || generalChannel.type !== ChannelType.GuildText) { + throw new Error('generalChannel is not found') + } + + // general チャンネルがあるサーバ以外は無視 + if (generalChannel.guildId !== member.guild.id) return + + // #general で参加したことを通知する + const generalMessageContent = `:wave:<@${member.id}>さんが jao Gamers Club から退出しました。` + await generalChannel.send(generalMessageContent) + } +}