From 87113f031b20e8de6274ab3c21e7de38aed8296d Mon Sep 17 00:00:00 2001 From: Voltstro Date: Mon, 29 Jul 2019 19:15:46 +1000 Subject: [PATCH] Move all Structs to the Structs folder --- Pootis-Bot/Core/Bot.cs | 9 ++++--- Pootis-Bot/Core/Global.cs | 2 +- Pootis-Bot/Entities/GlobalConfigFile.cs | 9 +------ Pootis-Bot/Entities/GlobalServerList.cs | 13 +--------- Pootis-Bot/Modules/Audio/VoiceChannels.cs | 3 ++- Pootis-Bot/Modules/Fun/GiphySearch.cs | 20 ++++++++++++--- Pootis-Bot/Services/Fun/GiphyService.cs | 25 +++++++++++++------ Pootis-Bot/Structs/ConfigApis.cs | 10 ++++++++ Pootis-Bot/{Entities => Structs}/GiphyData.cs | 4 +-- Pootis-Bot/Structs/GiphySearchResults.cs | 11 ++++++++ Pootis-Bot/Structs/VoiceChannel.cs | 14 +++++++++++ 11 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 Pootis-Bot/Structs/ConfigApis.cs rename Pootis-Bot/{Entities => Structs}/GiphyData.cs (70%) create mode 100644 Pootis-Bot/Structs/GiphySearchResults.cs create mode 100644 Pootis-Bot/Structs/VoiceChannel.cs diff --git a/Pootis-Bot/Core/Bot.cs b/Pootis-Bot/Core/Bot.cs index 7581c658..666ba050 100644 --- a/Pootis-Bot/Core/Bot.cs +++ b/Pootis-Bot/Core/Bot.cs @@ -2,12 +2,13 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using System.Diagnostics; using Discord; using Discord.WebSocket; using Discord.Rest; using Pootis_Bot.Entities; using Pootis_Bot.Services; -using System.Diagnostics; +using Pootis_Bot.Structs; namespace Pootis_Bot.Core { @@ -60,7 +61,7 @@ public async Task StartBot() private Task ChannelDestroyed(SocketChannel channel) { GlobalServerList serverList = ServerLists.GetServer((channel as SocketGuildChannel).Guild); - GlobalServerList.VoiceChannel voiceChannel = serverList.GetVoiceChannel(channel.Id); + var voiceChannel = serverList.GetVoiceChannel(channel.Id); //If the channel deleted was an auto voice channel, remove it from the list. if(voiceChannel.Name != null) @@ -79,7 +80,7 @@ private async Task UserVoiceStateUpdated(SocketUser user, SocketVoiceState befor //If we are adding an auto voice channel if (after.VoiceChannel != null) { - GlobalServerList.VoiceChannel voiceChannel = server.GetVoiceChannel(after.VoiceChannel.Id); + var voiceChannel = server.GetVoiceChannel(after.VoiceChannel.Id); if (voiceChannel.Name != null) { RestVoiceChannel createdChannel = await after.VoiceChannel.Guild.CreateVoiceChannelAsync($"New {voiceChannel.Name} chat"); @@ -232,7 +233,7 @@ await ownerDM.SendMessageAsync($"{guild.Owner.Mention}, your server **{guild.Nam } //Check to see if all the auto voice channels are there - List deleteAutoChannels = new List(); + List deleteAutoChannels = new List(); foreach(var autoChannel in server.VoiceChannels) { if (_client.GetChannel(autoChannel.ID) == null) diff --git a/Pootis-Bot/Core/Global.cs b/Pootis-Bot/Core/Global.cs index 249687e1..722a77f0 100644 --- a/Pootis-Bot/Core/Global.cs +++ b/Pootis-Bot/Core/Global.cs @@ -21,7 +21,7 @@ internal static class Global // Main Server - Development Server internal static readonly string[] discordServers = { "https://discord.creepysin.com", "https://discord.gg/m4YcsUa" }; - internal static readonly string version = "0.2.8"; + internal static readonly string version = "0.2.9"; internal static readonly string aboutMessage = $"Pootis Bot --- | --- {version}\n" + $"Created by Creepysin licensed under the MIT license. Vist {githubPage}/blob/master/LICENSE.md for more info.\n\n" + $"Pootis Robot icon by Valve\n" + diff --git a/Pootis-Bot/Entities/GlobalConfigFile.cs b/Pootis-Bot/Entities/GlobalConfigFile.cs index 8f0c013c..455bcbb5 100644 --- a/Pootis-Bot/Entities/GlobalConfigFile.cs +++ b/Pootis-Bot/Entities/GlobalConfigFile.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Pootis_Bot.Structs; namespace Pootis_Bot.Entities { @@ -24,14 +25,6 @@ public class GlobalConfigFile //Help modules public List helpModules = new List(); - public struct ConfigApis - { - public string apiGiphyKey; - public string apiYoutubeKey; - public string apiGoogleSearchKey; - public string googleSearchEngineID; - } - public class HelpModules { public string group; diff --git a/Pootis-Bot/Entities/GlobalServerList.cs b/Pootis-Bot/Entities/GlobalServerList.cs index 384812bb..bbb1df12 100644 --- a/Pootis-Bot/Entities/GlobalServerList.cs +++ b/Pootis-Bot/Entities/GlobalServerList.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Pootis_Bot.Structs; namespace Pootis_Bot.Entities { @@ -25,18 +26,6 @@ public class GlobalServerList public List ActiveAutoVoiceChannels = new List(); - public struct VoiceChannel - { - public ulong ID { get; set; } - public string Name { get; set;} - - public VoiceChannel(ulong id, string name) - { - ID = id; - Name = name; - } - } - public class CommandInfo { public string Command { get; set; } diff --git a/Pootis-Bot/Modules/Audio/VoiceChannels.cs b/Pootis-Bot/Modules/Audio/VoiceChannels.cs index 0386cfd2..6d776d05 100644 --- a/Pootis-Bot/Modules/Audio/VoiceChannels.cs +++ b/Pootis-Bot/Modules/Audio/VoiceChannels.cs @@ -6,6 +6,7 @@ using Pootis_Bot.Core; using Pootis_Bot.Entities; using Pootis_Bot.Preconditions; +using Pootis_Bot.Structs; namespace Pootis_Bot.Modules.Audio { @@ -22,7 +23,7 @@ public async Task AddVoiceChannel(string baseName) await Context.Channel.SendMessageAsync($"Added {baseName} as an auto voice channel."); - GlobalServerList.VoiceChannel voiceChannel = new GlobalServerList.VoiceChannel(channel.Id, baseName); + VoiceChannel voiceChannel = new VoiceChannel(channel.Id, baseName); ServerLists.GetServer((SocketGuild)Context.Guild).VoiceChannels.Add(voiceChannel); ServerLists.SaveServerList(); diff --git a/Pootis-Bot/Modules/Fun/GiphySearch.cs b/Pootis-Bot/Modules/Fun/GiphySearch.cs index 3c7f7d37..1727cb7d 100644 --- a/Pootis-Bot/Modules/Fun/GiphySearch.cs +++ b/Pootis-Bot/Modules/Fun/GiphySearch.cs @@ -3,6 +3,7 @@ using Discord.Commands; using Pootis_Bot.Core; using Pootis_Bot.Entities; +using Pootis_Bot.Structs; using Pootis_Bot.Services.Fun; namespace Pootis_Bot.Modules.Fun @@ -27,12 +28,23 @@ public async Task CmdGiphySearch([Remainder] string search = "") return; } - GiphyData results = GiphyService.Search(search); + var results = GiphyService.Search(search); + if (!results.IsSuccessfull) + { + //This should never happen, since we check it at the start! + if(results.ErrorReason == ErrorReason.NoAPIKey) + return; + if(results.ErrorReason == ErrorReason.Error) + { + await Context.Channel.SendMessageAsync("Sorry, but an error occured while searching Giphy, please try again in a moment!"); + return; + } + } EmbedBuilder embed = new EmbedBuilder(); - embed.WithTitle("Giphy Search: " + Global.Title(results.gifTitle)); - embed.WithDescription($"**By**: {results.gifAuthor}\n**URL**: {results.GifLink}"); - embed.WithImageUrl(results.gifUrl); + embed.WithTitle("Giphy Search: " + Global.Title(results.Data.gifTitle)); + embed.WithDescription($"**By**: {results.Data.gifAuthor}\n**URL**: {results.Data.GifLink}"); + embed.WithImageUrl(results.Data.gifUrl); embed.WithFooter($"Search by {Context.User} @ ", Context.User.GetAvatarUrl()); embed.WithCurrentTimestamp(); embed.WithColor(FunCmdsConfig.giphyColor); diff --git a/Pootis-Bot/Services/Fun/GiphyService.cs b/Pootis-Bot/Services/Fun/GiphyService.cs index 337ab2cd..cdb2b778 100644 --- a/Pootis-Bot/Services/Fun/GiphyService.cs +++ b/Pootis-Bot/Services/Fun/GiphyService.cs @@ -1,14 +1,16 @@ using System.Net; using Newtonsoft.Json; using Pootis_Bot.Core; -using Pootis_Bot.Entities; +using Pootis_Bot.Structs; namespace Pootis_Bot.Services.Fun { public static class GiphyService { - public static GiphyData Search(string search) + public static GiphySearchResult Search(string search) { + GiphySearchResult searchResult = new GiphySearchResult(); + try { //Check to see if the token is null or white space @@ -34,16 +36,23 @@ public static GiphyData Search(string search) GifLink = dataObject.data[choose].bitly_gif_url.ToString() }; - return item; + searchResult.IsSuccessfull = true; + searchResult.Data = item; + return searchResult; } - else - return null; + else + { + searchResult.IsSuccessfull = false; + searchResult.ErrorReason = ErrorReason.NoAPIKey; + return searchResult; + } } catch { - return null; - } - + searchResult.IsSuccessfull = false; + searchResult.ErrorReason = ErrorReason.Error; + return searchResult; + } } } } diff --git a/Pootis-Bot/Structs/ConfigApis.cs b/Pootis-Bot/Structs/ConfigApis.cs new file mode 100644 index 00000000..34b992a1 --- /dev/null +++ b/Pootis-Bot/Structs/ConfigApis.cs @@ -0,0 +1,10 @@ +namespace Pootis_Bot.Structs +{ + public struct ConfigApis + { + public string apiGiphyKey; + public string apiYoutubeKey; + public string apiGoogleSearchKey; + public string googleSearchEngineID; + } +} diff --git a/Pootis-Bot/Entities/GiphyData.cs b/Pootis-Bot/Structs/GiphyData.cs similarity index 70% rename from Pootis-Bot/Entities/GiphyData.cs rename to Pootis-Bot/Structs/GiphyData.cs index 2e6cfcc5..aefd3dbc 100644 --- a/Pootis-Bot/Entities/GiphyData.cs +++ b/Pootis-Bot/Structs/GiphyData.cs @@ -1,6 +1,6 @@ -namespace Pootis_Bot.Entities +namespace Pootis_Bot.Structs { - public class GiphyData + public struct GiphyData { public string gifUrl; public string gifTitle; diff --git a/Pootis-Bot/Structs/GiphySearchResults.cs b/Pootis-Bot/Structs/GiphySearchResults.cs new file mode 100644 index 00000000..6ad0aa53 --- /dev/null +++ b/Pootis-Bot/Structs/GiphySearchResults.cs @@ -0,0 +1,11 @@ +namespace Pootis_Bot.Structs +{ + public enum ErrorReason { NoAPIKey, HTTPError, Error } + + public struct GiphySearchResult + { + public bool IsSuccessfull { get; set; } + public ErrorReason ErrorReason { get; set; } + public GiphyData Data { get; set; } + } +} diff --git a/Pootis-Bot/Structs/VoiceChannel.cs b/Pootis-Bot/Structs/VoiceChannel.cs new file mode 100644 index 00000000..54bdfa94 --- /dev/null +++ b/Pootis-Bot/Structs/VoiceChannel.cs @@ -0,0 +1,14 @@ +namespace Pootis_Bot.Structs +{ + public struct VoiceChannel + { + public ulong ID { get; set; } + public string Name { get; set; } + + public VoiceChannel(ulong id, string name) + { + ID = id; + Name = name; + } + } +}