From cb75dc1f5ac4d0583ddadbc615f11b5668d76473 Mon Sep 17 00:00:00 2001 From: Christopher Darnell Date: Mon, 19 Apr 2021 16:43:16 -0700 Subject: [PATCH] Added global toggle [partially]. Need to fix removing global prefix when no Guild Prefix is defined. --- StarsiegeBot/BotSettings.cs | 17 +++++++++++--- StarsiegeBot/Program.cs | 45 ++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/StarsiegeBot/BotSettings.cs b/StarsiegeBot/BotSettings.cs index bf8b85d..e03bf9a 100644 --- a/StarsiegeBot/BotSettings.cs +++ b/StarsiegeBot/BotSettings.cs @@ -22,7 +22,7 @@ using System.Threading; namespace StarsiegeBot { - [Group("ex")] + [Group("bot")] class BotSettings : BaseCommandModule { public static Dictionary GuildSettings; @@ -47,8 +47,10 @@ namespace StarsiegeBot item.Prefixes.Add(">"); // this line... is a test line. - GuildSettings = new Dictionary(); - GuildSettings.Add("default", item); + GuildSettings = new Dictionary + { + { "default", item } + }; string output = JsonConvert.SerializeObject(GuildSettings); File.WriteAllTextAsync("guildSettings.json", output); @@ -108,6 +110,15 @@ namespace StarsiegeBot GuildSettings.Remove(prefix); await ctx.RespondAsync($"Removed `{prefix}` as a command prefix."); } + + [Command("global"), Aliases("g")] + public async Task UseGlobalPrefixes(CommandContext ctx, bool isEnabled) + { + await ctx.TriggerTypingAsync(); + GuildSettings[ctx.Guild.Id.ToString()].UseGlobalPrefix = isEnabled; + await ctx.RespondAsync($"Global Prefix usage has been turned **{(isEnabled ? "on":"off")}**"); + + } } } diff --git a/StarsiegeBot/Program.cs b/StarsiegeBot/Program.cs index 964d3ce..f13b890 100644 --- a/StarsiegeBot/Program.cs +++ b/StarsiegeBot/Program.cs @@ -184,7 +184,8 @@ namespace StarsiegeBot //{ this.Commands.RegisterCommands(); this.Commands.RegisterCommands(); - this.Commands.RegisterCommands(); + this.Commands.RegisterCommands(); + this.Commands.RegisterCommands(); //} Task thisTimer = StartTimer(new CancellationToken()); @@ -197,6 +198,7 @@ namespace StarsiegeBot // and this is to prevent premature quitting await Task.Delay(-1); } + private Task Event_MessageCreated(DiscordClient d, MessageCreateEventArgs e) { d.Logger.LogDebug(BotEventId, "Event_MessageCreated."); @@ -276,11 +278,11 @@ namespace StarsiegeBot return Task.CompletedTask; } - private async Task Event_CommandErrored(CommandsNextExtension ext, CommandErrorEventArgs e) + private Task Event_CommandErrored(CommandsNextExtension ext, CommandErrorEventArgs e) { // let's log the error details e.Context.Client.Logger.LogError(BotEventId, $"{e.Context.User.Username} tried executing '{e.Command?.QualifiedName ?? ""}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? ""}", DateTime.Now); - + return Task.CompletedTask; } private Task Event_InteractionCreated(DiscordClient d, InteractionCreateEventArgs e) @@ -343,22 +345,29 @@ namespace StarsiegeBot { string gId = e.Guild.Id.ToString(); // this guild doesnt have settings, make a skel set for them. - if (!BotSettings.GuildSettings.ContainsKey(gId)) + if (BotSettings.GuildSettings is null) { - Console.WriteLine("making config"); - GuildSettings item = new GuildSettings(); - item.UseAutoRoles = false; - item.UseGlobalPrefix = true; - item.UseLevelRoles = false; - item.UseLevels = false; - item.UseSelfRoles = false; - item.AllowRolesPurchase = false; - item.UseAutoRoles = false; - item.LevelRoles = new Dictionary(); - item.Prefixes = new List(); - item.SelfRoles = new Dictionary(); - // this line... is a test line. - BotSettings.GuildSettings.Add(gId, item); + + } + else + { + if (!BotSettings.GuildSettings.ContainsKey(gId)) + { + Console.WriteLine("making config"); + GuildSettings item = new GuildSettings(); + item.UseAutoRoles = false; + item.UseGlobalPrefix = true; + item.UseLevelRoles = false; + item.UseLevels = false; + item.UseSelfRoles = false; + item.AllowRolesPurchase = false; + item.UseAutoRoles = false; + item.LevelRoles = new Dictionary(); + item.Prefixes = new List(); + item.SelfRoles = new Dictionary(); + // this line... is a test line. + BotSettings.GuildSettings.Add(gId, item); + } } // let's log the name of the guild that was just