From 081d76668617bfb8d5a9ef74bf1f9fbea409b9bc Mon Sep 17 00:00:00 2001 From: Yuzu Date: Wed, 29 Jun 2022 19:59:53 +0700 Subject: [PATCH] Add support command --- .env.example | 1 + locales/en.json | 5 ++++ locales/jp.json | 5 ++++ locales/th.json | 5 ++++ src/discord/Help.ts | 4 +-- src/discord/Support.ts | 54 ++++++++++++++++++++++++++++++++++++ src/providers/Discord.ts | 2 ++ src/providers/Environment.ts | 2 ++ 8 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/discord/Support.ts diff --git a/.env.example b/.env.example index a73c170..877f3d9 100644 --- a/.env.example +++ b/.env.example @@ -7,6 +7,7 @@ DATABASE_URL=postgresql://user:password@host:port/dbName?schema=public DISCORD_TOKEN=SuperSecretDiscordToken DEVELOPER_IDS=DEV1_ID,DEV2_ID PRIVATE_BOT=true +SUPPORT_URL=https://example.com/support OSU_API_KEY=SuperSecretosu!Key YOUTUBE_COOKIE_BASE64=SuperSecretCookie_EncodeItInBase64_AndThen_RemoveTheLastEqualToo \ No newline at end of file diff --git a/locales/en.json b/locales/en.json index 9b9913e..96ac2b6 100644 --- a/locales/en.json +++ b/locales/en.json @@ -145,5 +145,10 @@ "title_processing": "Measuring network performance ( ◕▿◕ )", "running_on": "Running on {{HOST}}", "development_environment": "Development Environment" + }, + "support": { + "title": "Support", + "info": "[Join {{BOT_NAME}}'s support server]({{{LINK}}})", + "not_available": "{{BOT_NAME}}'s support server is currently unavailable" } } \ No newline at end of file diff --git a/locales/jp.json b/locales/jp.json index 9487362..87c6aef 100644 --- a/locales/jp.json +++ b/locales/jp.json @@ -145,5 +145,10 @@ "title_processing": "ネットワーク性能の測定 ( ◕▿◕ )", "running_on": "{{HOST}}で実行中", "development_environment": "開発環境" + }, + "support": { + "title": "サポート", + "info": "[{{BOT_NAME}}のサポートサーバーに参加する]({{{LINK}}})", + "not_available": "{{BOT_NAME}}のサポートサーバーは現在利用できません" } } \ No newline at end of file diff --git a/locales/th.json b/locales/th.json index da987f6..797c412 100644 --- a/locales/th.json +++ b/locales/th.json @@ -145,5 +145,10 @@ "title_processing": "กำลังวัดประสิทธิภาพของเครือข่าย ( ◕▿◕ )", "running_on": "ทำงานอยู่บน {{HOST}}", "development_environment": "โหมดกำลังพัฒนา" + }, + "support": { + "title": "ติดต่อฝ่ายสนับสนุน", + "info": "[เข้าร่วมเซิร์ฟเวอร์สนับสนุนของ {{BOT_NAME}}]({{{LINK}}})", + "not_available": "เซิร์ฟเวอร์สนับสนุนของ {{BOT_NAME}} ไม่พร้อมใช้งานในขณะนี้" } } \ No newline at end of file diff --git a/src/discord/Help.ts b/src/discord/Help.ts index 17f1835..fb10f2e 100644 --- a/src/discord/Help.ts +++ b/src/discord/Help.ts @@ -1,4 +1,4 @@ -import { CommandInteraction, Interaction, Message } from 'discord.js'; +import { CommandInteraction, Message } from 'discord.js'; import { I18n } from 'i18n'; import DiscordProvider from '../providers/Discord'; @@ -24,7 +24,7 @@ const EMBEDS = { fields: [ { name: `☕ ${locale.__('help.general')}`, - value: `help, ping, invite, userinfo, stats`, + value: `help, ping, invite, userinfo, stats, support`, inline: true }, { diff --git a/src/discord/Support.ts b/src/discord/Support.ts new file mode 100644 index 0000000..75816b9 --- /dev/null +++ b/src/discord/Support.ts @@ -0,0 +1,54 @@ +import { Message, CommandInteraction } from 'discord.js'; +import { I18n } from 'i18n'; + +import Environment from '../providers/Environment'; +import DiscordProvider from '../providers/Discord'; +import Locale from '../services/Locale'; + +import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule'; +import { sendHybridInteractionMessageResponse, makeInfoEmbed } from '../utils/DiscordMessage'; + +const EMBEDS = { + SUPPORT_INFO: (data: HybridInteractionMessage, locale: I18n) => { + const user = data.getUser(); + let message; + if (Environment.get().SUPPORT_URL) + message = locale.__('support.info', { BOT_NAME: DiscordProvider.client.user!.username, LINK: Environment.get().SUPPORT_URL}); + + return makeInfoEmbed({ + title: locale.__('support.title'), + description: message || locale.__('support.not_available', { BOT_NAME: DiscordProvider.client.user!.username}), + user + }); + } +}; + +export default class Support extends DiscordModule { + public id: string = 'Discord_Support'; + public commands = ['support']; + public commandInteractionName = 'support'; + + async GuildOnModuleCommand(args: any, message: Message) { + await this.run(new HybridInteractionMessage(message), args); + } + + async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) { + await this.run(new HybridInteractionMessage(interaction), interaction.options); + } + + async run(data: HybridInteractionMessage, args: any) { + const guild = data.getGuild(); + if (!guild) return; + + const locale = await Locale.getGuildLocale(guild.id); + + await sendHybridInteractionMessageResponse(data, { + embeds: [EMBEDS.SUPPORT_INFO(data, locale)] + }); + + if(Environment.get().SUPPORT_URL) + await sendHybridInteractionMessageResponse(data, { + content: Environment.get().SUPPORT_URL + }); + } +} diff --git a/src/providers/Discord.ts b/src/providers/Discord.ts index 68a9a29..24ad2d6 100644 --- a/src/providers/Discord.ts +++ b/src/providers/Discord.ts @@ -14,6 +14,7 @@ import Discord_MembershipScreening from '../discord/MembershipScreening'; import Discord_osu from '../discord/osu'; import Discord_UserInfo from '../discord/UserInfo'; import Discord_Stats from '../discord/Stats'; +import Discord_Support from '../discord/Support'; import Discord_MusicPlayer_Play from '../discord/MusicPlayer/Play'; import Discord_MusicPlayer_Skip from '../discord/MusicPlayer/Skip'; @@ -64,6 +65,7 @@ class Discord { new Discord_InteractionManager(), new Discord_osu(), new Discord_Settings(), + new Discord_Support(), new Discord_MusicPlayer_Play(), new Discord_MusicPlayer_NowPlaying(), diff --git a/src/providers/Environment.ts b/src/providers/Environment.ts index 19c9a6e..77e17c2 100644 --- a/src/providers/Environment.ts +++ b/src/providers/Environment.ts @@ -29,6 +29,7 @@ class Environment { const DISCORD_TOKEN = process.env.DISCORD_TOKEN; const DEVELOPER_IDS = process.env.DEVELOPER_IDS; const PRIVATE_BOT = process.env.PRIVATE_BOT; + const SUPPORT_URL = process.env.SUPPORT_URL; const OSU_API_KEY = process.env.OSU_API_KEY; const YOUTUBE_COOKIE_BASE64 = process.env.YOUTUBE_COOKIE_BASE64; @@ -39,6 +40,7 @@ class Environment { DISCORD_TOKEN, DEVELOPER_IDS, PRIVATE_BOT, + SUPPORT_URL, OSU_API_KEY, YOUTUBE_COOKIE_BASE64