diff --git a/StarsiegeBot/BotSettings.cs b/StarsiegeBot/BotSettings.cs index 92e36eb..6800701 100644 --- a/StarsiegeBot/BotSettings.cs +++ b/StarsiegeBot/BotSettings.cs @@ -1,14 +1,14 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.Entities; -using System.Text.RegularExpressions; -using System.Collections.Generic; using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; using System.Text; -using DSharpPlus; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; namespace StarsiegeBot { @@ -18,6 +18,8 @@ namespace StarsiegeBot class BotSettings : BaseCommandModule { public static Dictionary GuildSettings; + private readonly string fileName = "guildSettings.json"; + private CancellationToken cancelToken; public BotSettings() { Console.WriteLine("Bot Setting Commands Loaded"); @@ -28,7 +30,7 @@ namespace StarsiegeBot return; } // Check for guild settings file. If it doesnt exist, create it. - if (!File.Exists("guildSettings.json")) + if (!File.Exists(fileName)) { // Since we need to creat it, we're going to set some default stuff. Console.Write("Guild Settings Config File Not Found. Creating One..."); @@ -55,7 +57,7 @@ namespace StarsiegeBot // Now that we have the initial stuff in memory, write it to file. string output = JsonConvert.SerializeObject(GuildSettings); - File.WriteAllTextAsync("guildSettings.json", output); + File.WriteAllTextAsync(fileName, output); // in form the console reader that we're done making the initial config. Console.WriteLine(" Done!"); } @@ -63,11 +65,12 @@ namespace StarsiegeBot { // load the config file to memory. var json = ""; - using (var fs = File.OpenRead("guildSettings.json")) + using (var fs = File.OpenRead(fileName)) using (var sr = new StreamReader(fs, new UTF8Encoding(false))) json = sr.ReadToEnd(); GuildSettings = JsonConvert.DeserializeObject>(json); } + _ = StartTimer(cancelToken); } [GroupCommand] [Description("Work in progress. Does nothing yet.")] @@ -75,6 +78,26 @@ namespace StarsiegeBot { await ctx.TriggerTypingAsync(); } + + + private async Task StartTimer(CancellationToken cancellationToken) + { + await Task.Run(async () => + { + while (true) + { + string output = JsonConvert.SerializeObject(BotSettings.GuildSettings); + await File.WriteAllTextAsync("guildsettings.json", output); + await Task.Delay(TimeSpan.FromSeconds(120), cancellationToken); + if (cancellationToken.IsCancellationRequested) + { + Console.WriteLine("Token Cancelled."); + break; + } + } + }); + } + } public class GuildSettings @@ -86,8 +109,8 @@ namespace StarsiegeBot public bool AllowRolesPurchase { get; set; } public bool UseAutoRoles { get; set; } public List Prefixes { get; set; } - public Dictionary> SelfRoles { get; set; } - public Dictionary> LevelRoles { get; set; } + public Dictionary> SelfRoles { get; set; } + public Dictionary> LevelRoles { get; set; } public List HiddenRolesList { get; set; } public DiscordChannel WelcomeChannel { get; set; } public bool UseWelcome { get; set; } diff --git a/StarsiegeBot/Code Organization.txt b/StarsiegeBot/Code Organization.txt new file mode 100644 index 0000000..51225a8 --- /dev/null +++ b/StarsiegeBot/Code Organization.txt @@ -0,0 +1,21 @@ +Public const fields. +Non-public const fields. +Public static properties. +Public static fields. +Non-public static properties. +Non-public static fields. +Public properties. +Public fields. +Private properties. +Private fields. +Static constructor. +Public constructors. +Non-public constructors. +Public methods (with the exception of methods overriden from System.Object). +Non-public methods. +Methods overriden from System.Object. +Public static methods. +Non-public static methods. +Operator overloads. +Public events. +Non-public events. \ No newline at end of file diff --git a/StarsiegeBot/EventTriggers.cs b/StarsiegeBot/EventTriggers.cs index 8deef48..043d621 100644 --- a/StarsiegeBot/EventTriggers.cs +++ b/StarsiegeBot/EventTriggers.cs @@ -1,20 +1,13 @@ #pragma warning disable IDE0060 // Remove unused parameter -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; using DSharpPlus.Entities; using DSharpPlus.EventArgs; -using DSharpPlus.Interactivity; -using DSharpPlus.Interactivity.Enums; -using DSharpPlus.Interactivity.Extensions; -using DSharpPlus.VoiceNext; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace StarsiegeBot { @@ -225,7 +218,7 @@ namespace StarsiegeBot { d.Logger.LogDebug(BotEventId, "GuildDownloadCompleted."); //if(e.Guilds[376937422010974209].Members.ContainsKey(139548200099905536)) - //notify = e.Guilds[376937422010974209].Members[139548200099905536]; + //notify = e.Guilds[376937422010974209].Members[139548200099905536]; return Task.CompletedTask; } @@ -255,7 +248,7 @@ namespace StarsiegeBot string msg = WelcomeMessage.WelcomeMessageProcessing(BotSettings.GuildSettings[gId].WelcomeMessage, e.Member, e.Guild); BotSettings.GuildSettings[gId].WelcomeChannel.SendMessageAsync(msg); } - + return Task.CompletedTask; } public Task GuildMemberRemoved(DiscordClient d, GuildMemberRemoveEventArgs e) diff --git a/StarsiegeBot/ExperiemntalCommands.cs b/StarsiegeBot/ExperiemntalCommands.cs new file mode 100644 index 0000000..fca794e --- /dev/null +++ b/StarsiegeBot/ExperiemntalCommands.cs @@ -0,0 +1,35 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity.Extensions; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace StarsiegeBot +{ + [Group("ex")] + [Hidden] + + [Description("Experimental commands. May break the bot.")] + class ExperiemntalCommands : BaseCommandModule + { + [Command("exit"), RequireOwner] + public async Task ExitProgram (CommandContext ctx) + { + await ctx.RespondAsync("Attempting a shutdown..."); + System.Environment.Exit(0); + } + + [Command("Report")] + public async Task Report (CommandContext ctx) + { + await ctx.RespondAsync("Report not generated"); + } + } +} diff --git a/StarsiegeBot/Commands.cs b/StarsiegeBot/GameCommands.cs similarity index 83% rename from StarsiegeBot/Commands.cs rename to StarsiegeBot/GameCommands.cs index 264585c..2424e8a 100644 --- a/StarsiegeBot/Commands.cs +++ b/StarsiegeBot/GameCommands.cs @@ -1,16 +1,16 @@ #pragma warning disable IDE0060 // Remove unused parameter -using System; -using System.Threading.Tasks; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.Entities; -using System.Text.RegularExpressions; -using System.Linq; +using System; using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; namespace StarsiegeBot { - class Commands : BaseCommandModule + class GameCommands : BaseCommandModule { // Master insult list. private readonly string[] InsultMaster = { @@ -22,12 +22,13 @@ namespace StarsiegeBot }; private List InsultCopy; - public Commands() + public GameCommands() { // Let the console know we've loaded basic commands. Console.WriteLine("Basic Commands Loaded"); InsultCopy = InsultMaster.ToList(); } + [Command("8ball"), Aliases(new String[] { "ball", "8" })] [Description("The Magic 8-Ball is a fortune-telling or seeking advice")] public async Task Eightball(CommandContext ctx, [RemainingText, Description("Your question to the 8 ball")] String remainingText = "") @@ -36,12 +37,7 @@ namespace StarsiegeBot // Load all the traditional 8-Ball answers into an array. string[] results = new string[] { "It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no", "Outlook not so good.", "Very doubtful." }; // give the end user the randomly choosen result. - DiscordEmbedBuilder embed = new DiscordEmbedBuilder - { - Description = results[Program.rnd.Next(0, results.Length)], - Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)] - }; - await ctx.RespondAsync(embed); + await ctx.RespondAsync(StartEmbed(results[Program.rnd.Next(0, results.Length)])); } [Command("about")] [Description("Gives some basic info about the bot")] @@ -81,12 +77,7 @@ namespace StarsiegeBot // We're going to randomly select heads or tails and then give it to the user. await ctx.TriggerTypingAsync(); string[] choices = { "heads", "tails" }; - DiscordEmbedBuilder embed = new DiscordEmbedBuilder - { - Description = "The coin landed on... " + choices[Program.rnd.Next(0, choices.Length)], - Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)] - }; - await ctx.RespondAsync(embed); + await ctx.RespondAsync(StartEmbed($"The coin landed on... {choices[Program.rnd.Next(0, choices.Length)]}")); } [Command("credits")] [Description("Shows all those that have helped with my bot(s) over the years!")] @@ -174,11 +165,11 @@ namespace StarsiegeBot else await ctx.RespondAsync(overallResults); } - [Command("guilds"),Hidden,RequireOwner] - public async Task GimmeServerNames (CommandContext ctx) + [Command("guilds"), Hidden, RequireOwner] + public async Task GimmeServerNames(CommandContext ctx) { string output = ""; - foreach (KeyValuePair item in ctx.Client.Guilds) + foreach (KeyValuePair item in ctx.Client.Guilds) { output += item.Value.Name + "\r\n"; } @@ -196,24 +187,14 @@ namespace StarsiegeBot int i = Math.Min(min, max); int a = Math.Max(min, max) + 1; // output the results - DiscordEmbedBuilder embed = new DiscordEmbedBuilder - { - Description = $"🎲 Your random number is: {Program.rnd.Next(i, a)}", - Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)] - }; - await ctx.RespondAsync(embed); + await ctx.RespondAsync(StartEmbed($"🎲 Your random number is: {Program.rnd.Next(i, a)}")); } [Command("random")] public async Task Random(CommandContext ctx) { await ctx.TriggerTypingAsync(); // some one didn't read the HELP file on Random. Explain it to them. - DiscordEmbedBuilder embed = new DiscordEmbedBuilder - { - Description = $"Gives you a random number between