core/utils/DiscordBot.ts
Byungchul Kim e8075ee7d5
feat: support webhook (#514)
* feat: support webhook

* feat: bot webhook

* types: add enums for webhook

* feat: update webhook status by param

* feat: send webhook of server count change

* feat: send webhook of vote

* chore: add desc of faulty webhook

* chore: set initial value

* feat: add collection of clients

* chore: simplify

* feat: set webhook status dynamically

* feat: webhook for discord

* refactor: rename WebhookStatus.Paused

* refactor: make webhookClients to one object

* feat: webhook with fetch

* feat: add warning prop to input component

* feat: display red when warning

* feat: check server count properly

* feat: handle status codes

* refactor: remove double fetch

* chore: typo

* feat: send failed message

* fix: missing id on query

* feat: limit response body

* feat: use severlist bot to dm

* feat: webhook for servers

* feat: use env for ids

* refactor: remove variables

* fix: send discord log

* fix: message

* feat: include koreanbots in footer

* fix: typo

* refactor: export function as non default

* feat: add verification

* feat: add columns

* feat: verify bot webhook

* feat: verify server webhook

* chore: rename key to secret

* fix: stringify

* chore: remove webhook related columns

* refactor: use separate object for webhook

* type: add webhook prop to bot / server

* fix: implement webhook status

* refactor: rename webhook to webhookURL

* feat: select webhook props

* feat: remove bot's private props

* feat: remove server private fields

* chore: use makeURLs

* type: fix faildSince is type as string

* refactor: rename to updateWebhook

* chore: make props optional

* feat: failedSince

* feat: remove failedSince when success

* fix: missing import

* fix: typo

* fix: convert missing prop

* fix: typo

* chore: remove unnecessary select

* fix: missing systax

* feat: sort docs

* feat: use relay

* fix: check status properly

* chore: handle relay server error

* remove awaits

* fix: add base url

* fix: typo

* chore: remove red highlights

* chore: change emoji

---------

Co-authored-by: SKINMAKER <skinmaker@SKINMAKERs-iMac.local>
Co-authored-by: skinmaker1345 <me@skinmaker.dev>
2023-04-03 12:03:41 +09:00

42 lines
2.1 KiB
TypeScript

import * as Discord from 'discord.js'
export const DiscordBot = new Discord.Client({
intents: Number(process.env.DISCORD_CLIENT_INTENTS ?? 32767)
})
export const ServerListDiscordBot = new Discord.Client({
intents: []
})
export const webhookClients = {
bot: new Discord.Collection<string, Discord.WebhookClient>(),
server: new Discord.Collection<string, Discord.WebhookClient>()
}
DiscordBot.on('ready', async () => {
console.log('I\'m Ready')
await getMainGuild().members.fetch()
console.log(`Fetched ${getMainGuild().members.cache.size} Members`)
})
DiscordBot.login(process.env.DISCORD_TOKEN)
ServerListDiscordBot.login(process.env.DISCORD_SERVERLIST_TOKEN)
export const getMainGuild = () => DiscordBot.guilds.cache.get(process.env.GUILD_ID)
export const getReviewGuild = () => DiscordBot.guilds.cache.get(process.env.REVIEW_GUILD_ID)
export const getReportChannel = (): Discord.TextChannel => getMainGuild().channels.cache.get(process.env.REPORT_CHANNEL_ID) as Discord.TextChannel
export const getLoggingChannel = (): Discord.TextChannel => getMainGuild().channels.cache.get(process.env.LOGGING_CHANNEL_ID) as Discord.TextChannel
export const getStatsLoggingChannel = (): Discord.TextChannel => getMainGuild().channels.cache.get(process.env.STATS_LOGGING_CHANNEL_ID) as Discord.TextChannel
export const getBotReviewLogChannel = (): Discord.TextChannel => getReviewGuild().channels.cache.get(process.env.REVIEW_LOG_CHANNEL_ID) as Discord.TextChannel
export const getOpenBotReviewLogChannel = (): Discord.TextChannel => getMainGuild().channels.cache.get(process.env.OPEN_REVIEW_LOG_CHANNEL_ID) as Discord.TextChannel
export const discordLog = async (type: string, issuerID: string, embed?: Discord.EmbedBuilder, attachment?: { content: string, format: string}, content?: string): Promise<void> => {
getLoggingChannel().send({
content: `[${type}] <@${issuerID}> (${issuerID})\n${content || ''}`,
embeds: [embed && embed.setTitle(type).setTimestamp(new Date())],
...(attachment && { files: [
new Discord.AttachmentBuilder(Buffer.from(attachment.content), {name: `${type.toLowerCase().replace(/\//g, '-')}-${issuerID}-${Date.now()}.${attachment.format}`
})]})
})
}