Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ユーザが退出した際、generalで通知する #205

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions src/events/leaved-notifier.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
}
}