From 96b05cacf58c1108ffcf01a57fc39fb4299a1dbe Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 9 Apr 2022 17:02:21 +0100 Subject: [PATCH] Add setup command for lobby --- data/lobbyConfig.txt | 8 +++ src/commands/501231711271780357/lobby.ts | 88 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 data/lobbyConfig.txt diff --git a/data/lobbyConfig.txt b/data/lobbyConfig.txt new file mode 100644 index 0000000..ed65f72 --- /dev/null +++ b/data/lobbyConfig.txt @@ -0,0 +1,8 @@ +USAGE: config [cooldown] [Game Name] + +===[ EXAMPLE ]=== +To add a channel: +- config add game-name role-name 30 Game Name + +To remove a channel: +- config remove game-name \ No newline at end of file diff --git a/src/commands/501231711271780357/lobby.ts b/src/commands/501231711271780357/lobby.ts index 5e6d687..e002338 100644 --- a/src/commands/501231711271780357/lobby.ts +++ b/src/commands/501231711271780357/lobby.ts @@ -2,6 +2,11 @@ import { TextChannel } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; import { Command } from "../../type/command"; import { default as eLobby } from "../../entity/501231711271780357/Lobby"; +import SettingsHelper from "../../helpers/SettingsHelper"; +import PublicEmbed from "../../helpers/embeds/PublicEmbed"; +import { readFileSync } from "fs"; +import ErrorEmbed from "../../helpers/embeds/ErrorEmbed"; +import BaseEntity from "../../contracts/BaseEntity"; export default class Lobby extends Command { constructor() { @@ -13,6 +18,20 @@ export default class Lobby extends Command { public override async execute(context: ICommandContext) { if (!context.message.guild) return; + switch (context.args[0]) { + case "config": + await this.UseConfig(context); + break; + default: + await this.UseDefault(context); + } + } + + // ======= + // Default + // ======= + + private async UseDefault(context: ICommandContext) { const channel = context.message.channel as TextChannel; const channelId = channel.id; @@ -52,4 +71,73 @@ export default class Lobby extends Command { private SendDisabled(context: ICommandContext) { context.message.reply("This channel hasn't been setup for lobbies."); } + + // ====== + // Config + // ====== + private async UseConfig(context: ICommandContext) { + const moderatorRole = await SettingsHelper.GetSetting("role.moderator", context.message.guild!.id); + + if (!context.message.member?.roles.cache.find(x => x.name == moderatorRole)) { + const errorEmbed = new ErrorEmbed(context, "Sorry, you must be a moderator to be able to configure this command"); + errorEmbed.SendToCurrentChannel(); + + return; + } + + switch (context.args[1]) { + case "add": + await this.AddLobbyConfig(context); + break; + case "remove": + await this.RemoveLobbyConfig(context); + break; + case "help": + default: + this.SendConfigHelp(context); + } + } + + private SendConfigHelp(context: ICommandContext) { + const helpText = readFileSync(`${process.cwd()}/data/lobbyConfig.txt`).toString(); + + const embed = new PublicEmbed(context, "Configure Lobby Command", helpText); + embed.SendToCurrentChannel(); + } + + private async AddLobbyConfig(context: ICommandContext) { + const channel = context.message.guild!.channels.cache.find(x => x.name == context.args[2]); + const role = context.message.guild!.roles.cache.find(x => x.name == context.args[3]); + const cooldown = context.args[4] || "30"; + const gameName = context.args.splice(5).join(" "); + + if (!channel || !role) { + this.SendConfigHelp(context); + return; + } + + const entity = new eLobby(channel.id, role.id, Number.parseInt(cooldown), gameName); + await entity.Save(eLobby, entity); + + const embed = new PublicEmbed(context, "", "Added new lobby channel"); + embed.SendToCurrentChannel(); + } + + private async RemoveLobbyConfig(context: ICommandContext) { + const channel = context.message.guild!.channels.cache.find(x => x.name == context.args[2]); + + if (!channel) { + this.SendConfigHelp(context); + return; + } + + const entity = await eLobby.FetchOneByChannelId(channel.id); + + if (entity) { + await BaseEntity.Remove(eLobby, entity); + } + + const embed = new PublicEmbed(context, "", "Removed lobby channel"); + embed.SendToCurrentChannel(); + } } \ No newline at end of file -- 2.43.4