From d5ec964a7d2917f62b0610be0fccaf3c7e3211d6 Mon Sep 17 00:00:00 2001 From: gngrninja Date: Sat, 24 Jun 2017 17:51:10 -0700 Subject: [PATCH] Adding configuration class to store API and bot data --- CommandHandler.cs | 2 +- Modules/Admin/Admin.cs | 2 +- Modules/Config.cs | 211 +++++++++++++++++++++++++++++++++ Modules/Fun/FunCommands.cs | 18 +-- Modules/Fun/OxfordApi.cs | 4 +- Modules/Giphy/GiphyApi.cs | 2 +- Modules/Giphy/GiphyCommands.cs | 10 +- Modules/Steam/SteamApi.cs | 2 +- Modules/Weather/WeatherApi.cs | 4 +- Modules/Wow/WarcraftLogs.cs | 2 +- Modules/Wow/WowApi.cs | 2 +- Modules/Wow/WowCommands.cs | 52 ++++---- Modules/YouTube/YouTubeApi.cs | 4 +- NinjaBot.cs | 197 +----------------------------- 14 files changed, 268 insertions(+), 244 deletions(-) create mode 100644 Modules/Config.cs diff --git a/CommandHandler.cs b/CommandHandler.cs index 47502e9..a5cff4b 100644 --- a/CommandHandler.cs +++ b/CommandHandler.cs @@ -39,7 +39,7 @@ public async Task HandleCommand(SocketMessage parameterMessage) // Mark where the prefix ends and the command begins int argPos = 0; // Determine if the message has a valid prefix, adjust argPos - if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasCharPrefix(NinjaBot.Prefix, ref argPos))) return; + if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasCharPrefix(Config.Prefix, ref argPos))) return; // Create a Command Context var context = new SocketCommandContext(_client, message); diff --git a/Modules/Admin/Admin.cs b/Modules/Admin/Admin.cs index 27397a2..b81688e 100644 --- a/Modules/Admin/Admin.cs +++ b/Modules/Admin/Admin.cs @@ -455,7 +455,7 @@ private async Task GetNoteInfo(ICommandContext Context) var note = db.Notes.FirstOrDefault(n => n.ServerId == (long)Context.Guild.Id); if (note == null) { - sb.AppendLine($"Unable to find a note for server [{Context.Guild.Name}], perhaps try adding one by using {NinjaBot.Prefix}set-note \"Note goes here!\""); + sb.AppendLine($"Unable to find a note for server [{Context.Guild.Name}], perhaps try adding one by using {Config.Prefix}set-note \"Note goes here!\""); } else { diff --git a/Modules/Config.cs b/Modules/Config.cs new file mode 100644 index 0000000..8f05587 --- /dev/null +++ b/Modules/Config.cs @@ -0,0 +1,211 @@ +using System; +using System.IO; +using System.Collections.Generic; +using NinjaBotCore.Models; +using Newtonsoft.Json; + +namespace NinjaBotCore +{ + public class Config + { + private static string _token; + private static char _prefix; + private static string _wowApi; + private static string _giphyApi; + private static string _googleMapsApi; + private static string _youTubeApi; + private static string _darkSkyApi; + private static string _steamApi; + private static string _faceApi; + private static string _imageApi; + private static string _oxfordDictionaryApi; + private static string _donateUrl; + private static string _warcraftlogsApi; + + public Config() + { + string configFile = "config.json"; + ConfigData botConfig = GetConfigData(configFile); + if (botConfig == null) + { + throw new InvalidDataException("Config file access error!"); + } + else + { + _token = botConfig.Token; + Prefix = botConfig.Prefix; + WowApi = botConfig.WowApi; + GiphyApi = botConfig.GiphyApi; + GoogleMapsApi = botConfig.GoogleMapsApi; + YouTubeApi = botConfig.YouTubeApi; + DarkSkyApi = botConfig.DarkSkyApi; + SteamApi = botConfig.SteamApi; + FaceApi = botConfig.FaceApi; + ImageApi = botConfig.ImageApi; + OxfordDictionaryApi = botConfig.OxfordDictionaryApi; + WarcraftLogsApi = botConfig.WarcraftlogsApi; + DonateUrl = botConfig.DonateUrl; + } + } + public static string Token + { + get + { + return _token; + } + set + { + _token = value; + } + } + public static string WarcraftLogsApi + { + get + { + return _warcraftlogsApi; + } + set + { + _warcraftlogsApi = value; + } + } + public static string DonateUrl + { + get + { + return _donateUrl; + } + set + { + _donateUrl = value; + } + } + public static string OxfordDictionaryApi + { + get + { + return _oxfordDictionaryApi; + } + private set + { + _oxfordDictionaryApi = value; + } + } + public static string FaceApi + { + get + { + return _faceApi; + } + private set + { + _faceApi = value; + } + } + public static char Prefix + { + get + { + return _prefix; + } + private set + { + _prefix = value; + } + } + public static string WowApi + { + get + { + return _wowApi; + } + private set + { + _wowApi = value; + } + } + public static string GiphyApi + { + get + { + return _giphyApi; + } + private set + { + _giphyApi = value; + } + } + public static string GoogleMapsApi + { + get + { + return _googleMapsApi; + } + private set + { + _googleMapsApi = value; + } + } + public static string YouTubeApi + { + get + { + return _youTubeApi; + } + private set + { + _youTubeApi = value; + } + } + public static string DarkSkyApi + { + get + { + return _darkSkyApi; + } + private set + { + _darkSkyApi = value; + } + } + public static string SteamApi + { + get + { + return _steamApi; + } + private set + { + _steamApi = value; + } + } + public static string ImageApi + { + get + { + return _imageApi; + } + private set + { + _imageApi = value; + } + } + ConfigData GetConfigData(string configFile) + { + ConfigData configData = null; + try + { + if (File.Exists(configFile)) + { + configData = JsonConvert.DeserializeObject(File.ReadAllText(configFile)); + } + return configData; + } + catch (Exception ex) + { + Console.WriteLine($"Error getting configuration information -> {ex.Message}"); + return configData; + } + } + } +} \ No newline at end of file diff --git a/Modules/Fun/FunCommands.cs b/Modules/Fun/FunCommands.cs index 6937ea6..a23fcbf 100644 --- a/Modules/Fun/FunCommands.cs +++ b/Modules/Fun/FunCommands.cs @@ -63,7 +63,7 @@ public async Task Donate() sb.AppendLine(); sb.AppendLine($"Every little bit counts!"); sb.AppendLine(); - sb.AppendLine($"[Donate To Support NinjaBot!]({NinjaBot.DonateUrl}/5) :thumbsup:"); + sb.AppendLine($"[Donate To Support NinjaBot!]({Config.DonateUrl}/5) :thumbsup:"); embed.ThumbnailUrl = "https://static1.squarespace.com/static/5644323de4b07810c0b6db7b/t/5931c57f46c3c47b464d717a/1496434047310/FdxsNNRt.jpg"; embed.WithColor(new Color(0, 255, 0)); @@ -209,14 +209,14 @@ public async Task Help() embed.ThumbnailUrl = Context.User.GetAvatarUrl(); embed.WithColor(new Color(0, 0, 255)); sb.AppendLine("Here are a few commands to try:"); - sb.AppendLine($"\t :black_small_square: WoW Commands: {NinjaBot.Prefix}wow **|** {NinjaBot.Prefix}armory characterName **|** {NinjaBot.Prefix}logs **|** {NinjaBot.Prefix}set-guild **|** {NinjaBot.Prefix}get-guild"); - sb.AppendLine($"\t :black_small_square: Rocket League Stats: {NinjaBot.Prefix}rlstats"); - sb.AppendLine($"\t :black_small_square: Server Note Commands: {NinjaBot.Prefix}note **|** {NinjaBot.Prefix}set-note note goes here"); - sb.AppendLine($"\t :black_small_square: Away System Commands: {NinjaBot.Prefix}away (reason) **|** {NinjaBot.Prefix}back"); - sb.AppendLine($"\t :black_small_square: Search YouTube: {NinjaBot.Prefix}ysearch search term"); - sb.AppendLine($"\t :black_small_square: Define a Word: {NinjaBot.Prefix}define word"); - sb.AppendLine($"\t :black_small_square: Ask Ninja 8-ball a question: {NinjaBot.Prefix}8ball question"); - sb.AppendLine($"\t :black_small_square: Greeting Commands: {NinjaBot.Prefix}toggle-greetings **|** {NinjaBot.Prefix}set-join-message User join greeting **|** {NinjaBot.Prefix}set-part-message User left message"); + sb.AppendLine($"\t :black_small_square: WoW Commands: {Config.Prefix}wow **|** {Config.Prefix}armory characterName **|** {Config.Prefix}logs **|** {Config.Prefix}set-guild **|** {Config.Prefix}get-guild"); + sb.AppendLine($"\t :black_small_square: Rocket League Stats: {Config.Prefix}rlstats"); + sb.AppendLine($"\t :black_small_square: Server Note Commands: {Config.Prefix}note **|** {Config.Prefix}set-note note goes here"); + sb.AppendLine($"\t :black_small_square: Away System Commands: {Config.Prefix}away (reason) **|** {Config.Prefix}back"); + sb.AppendLine($"\t :black_small_square: Search YouTube: {Config.Prefix}ysearch search term"); + sb.AppendLine($"\t :black_small_square: Define a Word: {Config.Prefix}define word"); + sb.AppendLine($"\t :black_small_square: Ask Ninja 8-ball a question: {Config.Prefix}8ball question"); + sb.AppendLine($"\t :black_small_square: Greeting Commands: {Config.Prefix}toggle-greetings **|** {Config.Prefix}set-join-message User join greeting **|** {Config.Prefix}set-part-message User left message"); sb.AppendLine(); sb.AppendLine($"**For a more detailed command list, please visit:** http://gngr.ninja/bot"); embed.Description = sb.ToString(); diff --git a/Modules/Fun/OxfordApi.cs b/Modules/Fun/OxfordApi.cs index 3e47cfa..dd32eca 100644 --- a/Modules/Fun/OxfordApi.cs +++ b/Modules/Fun/OxfordApi.cs @@ -20,8 +20,8 @@ public class OxfordApi public OxfordApi() { _baseUrl = "https://od-api.oxforddictionaries.com/api/v1"; - _apiKey = NinjaBot.OxfordDictionaryApi.Split(',')[0]; - _appId = NinjaBot.OxfordDictionaryApi.Split(',')[1]; + _apiKey = Config.OxfordDictionaryApi.Split(',')[0]; + _appId = Config.OxfordDictionaryApi.Split(',')[1]; } private string getAPIRequest(string url, string sourceLang = "en") diff --git a/Modules/Giphy/GiphyApi.cs b/Modules/Giphy/GiphyApi.cs index 1aa2c53..99b8248 100644 --- a/Modules/Giphy/GiphyApi.cs +++ b/Modules/Giphy/GiphyApi.cs @@ -16,7 +16,7 @@ class GiphyApi public string ApiRequest(string url, string appendUrl = null) { string response = string.Empty; ; - string apiKey = $"{NinjaBot.GiphyApi}"; + string apiKey = $"{Config.GiphyApi}"; if (string.IsNullOrEmpty(appendUrl)) { url = $"http://api.giphy.com/v1{url}?api_key={apiKey}"; diff --git a/Modules/Giphy/GiphyCommands.cs b/Modules/Giphy/GiphyCommands.cs index 9cc29fb..b2de00e 100644 --- a/Modules/Giphy/GiphyCommands.cs +++ b/Modules/Giphy/GiphyCommands.cs @@ -61,7 +61,7 @@ public async Task Giphy([Remainder] string args = "") else { embed.Title = $"Sorry, Giphy is disabled here :(\n"; - embed.Description = $"Use {NinjaBot.Prefix}giphy-toggle to enable it"; + embed.Description = $"Use {Config.Prefix}giphy-toggle to enable it"; await _cc.Reply(Context, embed); } } @@ -105,10 +105,10 @@ public async Task ToggleGiphy() embed.Title = $"Giphy enabled for [**{serverName}**]"; sb.AppendLine($":question:__How to use **!giphy**__:question:"); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}giphy**"); + sb.AppendLine($"**{Config.Prefix}giphy**"); sb.AppendLine($"The above command would get a random image"); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}giphy Rocket League**"); + sb.AppendLine($"**{Config.Prefix}giphy Rocket League**"); sb.AppendLine($"The above command would get a random image related to Rocket League"); } else if ((bool)giphySettings.GiphyEnabled) @@ -124,10 +124,10 @@ public async Task ToggleGiphy() embed.Title = $"Giphy enabled for [**{serverName}**]"; sb.AppendLine($":question:__How to use **!giphy**__:question:"); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}giphy**"); + sb.AppendLine($"**{Config.Prefix}giphy**"); sb.AppendLine($"The above command would get a random image"); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}giphy Rocket League**"); + sb.AppendLine($"**{Config.Prefix}giphy Rocket League**"); sb.AppendLine($"The above command would get a random image related to Rocket League"); } db.SaveChanges(); diff --git a/Modules/Steam/SteamApi.cs b/Modules/Steam/SteamApi.cs index eabe6a7..beda7cc 100644 --- a/Modules/Steam/SteamApi.cs +++ b/Modules/Steam/SteamApi.cs @@ -13,7 +13,7 @@ namespace NinjaBotCore.Modules.Steam { public class Steam { - private string _key = $"?key={NinjaBot.SteamApi}"; + private string _key = $"?key={Config.SteamApi}"; private string getAPIRequest(string url) { diff --git a/Modules/Weather/WeatherApi.cs b/Modules/Weather/WeatherApi.cs index 2a1dbd3..35b68e4 100644 --- a/Modules/Weather/WeatherApi.cs +++ b/Modules/Weather/WeatherApi.cs @@ -14,8 +14,8 @@ namespace NinjaBotCore.Modules.Weather { class WeatherApi { - private readonly string _dsApiKey = NinjaBot.DarkSkyApi; - private readonly string _gMapsApiKey = NinjaBot.GoogleMapsApi; + private readonly string _dsApiKey = Config.DarkSkyApi; + private readonly string _gMapsApiKey = Config.GoogleMapsApi; public string getAPIRequest(string url) { diff --git a/Modules/Wow/WarcraftLogs.cs b/Modules/Wow/WarcraftLogs.cs index 2f8408f..907a029 100644 --- a/Modules/Wow/WarcraftLogs.cs +++ b/Modules/Wow/WarcraftLogs.cs @@ -49,7 +49,7 @@ private set public string logsApiRequest(string url) { string response = string.Empty; - string wowLogsKey = $"api_key={NinjaBot.WarcraftLogsApi}"; + string wowLogsKey = $"api_key={Config.WarcraftLogsApi}"; string baseURL = "https://www.warcraftlogs.com:443/v1"; url = $"{baseURL}{url}{wowLogsKey}"; diff --git a/Modules/Wow/WowApi.cs b/Modules/Wow/WowApi.cs index 15e63c4..45d4e7d 100644 --- a/Modules/Wow/WowApi.cs +++ b/Modules/Wow/WowApi.cs @@ -104,7 +104,7 @@ public string GetAPIRequest(string url) string prefix; prefix = "https://us.api.battle.net/wow"; - key = $"&apikey={NinjaBot.WowApi}"; + key = $"&apikey={Config.WowApi}"; url = $"{prefix}{url}{key}"; Console.WriteLine($"Wow API request to {url}"); diff --git a/Modules/Wow/WowCommands.cs b/Modules/Wow/WowCommands.cs index 1f6e2a1..357c650 100644 --- a/Modules/Wow/WowCommands.cs +++ b/Modules/Wow/WowCommands.cs @@ -56,9 +56,9 @@ public async Task GetArmory([Remainder]string args = null) var embedError = new EmbedBuilder(); embedError.Title = $"WoW Armory(NA) Command"; sb.AppendLine($"Usage examples:"); - sb.AppendLine($":black_small_square: **{NinjaBot.Prefix}armory** charactername"); + sb.AppendLine($":black_small_square: **{Config.Prefix}armory** charactername"); sb.AppendLine($"\t:black_small_square: Search armory for *charactername* (first in guild, then in the rest of WoW(NA))"); - sb.AppendLine($":black_small_square: **{NinjaBot.Prefix}armory** charactername realmname"); + sb.AppendLine($":black_small_square: **{Config.Prefix}armory** charactername realmname"); sb.AppendLine($"\t:black_small_square: Search armory for *charactername* on *realmname*"); embedError.Description = sb.ToString(); await _cc.Reply(Context, embedError); @@ -142,7 +142,7 @@ public async Task GetArmory([Remainder]string args = null) { charName = chars[0].charName; realmName = chars[0].realmName; - await _cc.Reply(Context, $"If **{charName}** on **{realmName}** is not who were looking for, try {NinjaBot.Prefix}wow search {charName} to locate a different character."); + await _cc.Reply(Context, $"If **{charName}** on **{realmName}** is not who were looking for, try {Config.Prefix}wow search {charName} to locate a different character."); } else { @@ -342,7 +342,7 @@ public async Task GetLogs([Remainder] string args = "") else { sb.AppendLine("Please specify a guild and realm name!"); - sb.AppendLine($"Example: {NinjaBot.Prefix}logs Thunderlord, UR KEY UR CARRY"); + sb.AppendLine($"Example: {Config.Prefix}logs Thunderlord, UR KEY UR CARRY"); await _cc.Reply(Context, sb.ToString()); return; } @@ -350,7 +350,7 @@ public async Task GetLogs([Remainder] string args = "") if (string.IsNullOrEmpty(guildName) || string.IsNullOrEmpty(realmName)) { sb.AppendLine("Please specify a guild and realm name!"); - sb.AppendLine($"Example: {NinjaBot.Prefix}logs Thunderlord, UR KEY UR CARRY"); + sb.AppendLine($"Example: {Config.Prefix}logs Thunderlord, UR KEY UR CARRY"); await _cc.Reply(Context, sb.ToString()); return; } @@ -426,7 +426,7 @@ public async Task SetGuild([Remainder]string args = "") { StringBuilder sb = new StringBuilder(); sb.AppendLine("Please specify a realm and guild name!"); - sb.AppendLine($"Example: {NinjaBot.Prefix}set-guild Thunderlord, UR KEY UR CARRY"); + sb.AppendLine($"Example: {Config.Prefix}set-guild Thunderlord, UR KEY UR CARRY"); await _cc.Reply(Context, sb.ToString()); return; } @@ -598,14 +598,14 @@ public async Task GetRanking([Remainder] string args = null) sb.Clear(); sb.AppendLine($":black_medium_small_square: **rankings**"); sb.AppendLine($"\t :black_small_square: Get rankings for the guild associated with this Discord server from http://www.wowprogress.com"); - sb.AppendLine($"\t :black_small_square: Alternatively, you can provide a guild and realm in this format: {NinjaBot.Prefix}wow rankings *realmname*, *guildname*"); + sb.AppendLine($"\t :black_small_square: Alternatively, you can provide a guild and realm in this format: {Config.Prefix}wow rankings *realmname*, *guildname*"); sb.AppendLine($":black_medium_small_square: **auctions**"); sb.AppendLine($"\t :black_small_square: Get a list of the current auctions (most used raiding/crafting items) for the realm associated with this Discord server"); - sb.AppendLine($"\t :black_small_square: Alternatively, you can provide a realm in this format: {NinjaBot.Prefix}wow auctions *realmname*"); + sb.AppendLine($"\t :black_small_square: Alternatively, you can provide a realm in this format: {Config.Prefix}wow auctions *realmname*"); sb.AppendLine($":black_medium_small_square: **search**"); sb.AppendLine($"\t :black_small_square: Search the WoW website for a character name to find out the associated level and realm"); var embed = new EmbedBuilder(); - embed.Title = $"{NinjaBot.Prefix}wow Command Usage"; + embed.Title = $"{Config.Prefix}wow Command Usage"; embed.Description = sb.ToString(); await _cc.Reply(Context, embed); } @@ -645,26 +645,26 @@ public async Task GetTop10([Remainder] string args = null) //Argument logic if (args == null || args.Split(',')[0] == "help") { - sb.AppendLine($"**{NinjaBot.Prefix}top10** fightName(or ID from {NinjaBot.Prefix}top10 list) guild(type guild to get guild only results, all for all guilds) metric(dps(default), or hps) difficulty(lfr, flex, normal, heroic(default), or mythic) "); + sb.AppendLine($"**{Config.Prefix}top10** fightName(or ID from {Config.Prefix}top10 list) guild(type guild to get guild only results, all for all guilds) metric(dps(default), or hps) difficulty(lfr, flex, normal, heroic(default), or mythic) "); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** list"); + sb.AppendLine($"**{Config.Prefix}top10** list"); sb.AppendLine($"Get a list of all encounters and shortcut IDs"); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** 1"); + sb.AppendLine($"**{Config.Prefix}top10** 1"); sb.AppendLine($"The above command would get all top 10 **dps** results for **Skorpyron** on **{realmName}**."); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** 1 guild"); + sb.AppendLine($"**{Config.Prefix}top10** 1 guild"); sb.AppendLine($"The above command would get the top 10 **dps** results for **Skorpyron** on **{realmName}** for **{guildName}**."); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** 1 guild hps"); + sb.AppendLine($"**{Config.Prefix}top10** 1 guild hps"); sb.AppendLine($"The above command would get the top 10 **hps** results for **Skorpyron** on **{realmName}** for **{guildName}**."); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** 1 all hps"); + sb.AppendLine($"**{Config.Prefix}top10** 1 all hps"); sb.AppendLine($"The above command would get all top 10 **hps** results for **Skorpyron** on **{realmName}**."); sb.AppendLine(); - sb.AppendLine($"**{NinjaBot.Prefix}top10** 1 guild dps mythic"); + sb.AppendLine($"**{Config.Prefix}top10** 1 guild dps mythic"); sb.AppendLine($"The above command would get the top 10 **dps** results for **Skorpyron** on **{realmName}** for **{guildName}** on **mythic** difficulty."); - embed.Title = $"{Context.User.Username}, here are some examples for **{NinjaBot.Prefix}top10**"; + embed.Title = $"{Context.User.Username}, here are some examples for **{Config.Prefix}top10**"; embed.Description = sb.ToString(); await _cc.Reply(Context, embed); return; @@ -1005,7 +1005,7 @@ private async Task GetAuctionItems(string args) } if (string.IsNullOrEmpty(realmName)) { - await _cc.Reply(Context, $"Unable to find a guild/realm association!\nTry {NinjaBot.Prefix}wow auctions realmName guildName"); + await _cc.Reply(Context, $"Unable to find a guild/realm association!\nTry {Config.Prefix}wow auctions realmName guildName"); return; } Console.WriteLine($"Looking up auctions for realm {realmName.ToUpper()}"); @@ -1165,9 +1165,9 @@ private async Task GetWowRankings(string args = "") StringBuilder sb = new StringBuilder(); var embed = new EmbedBuilder(); embed.WithColor(new Color(255, 0, 0)); - embed.Title = $"Unable to find a guild/realm association!\nTry {NinjaBot.Prefix}wow rankings Realm Name, Guild Name"; - sb.AppendLine($"Command syntax: {NinjaBot.Prefix}wow rankings realm name, guild name"); - sb.AppendLine($"Command example: {NinjaBot.Prefix}wow rankings azgalor, carebears"); + embed.Title = $"Unable to find a guild/realm association!\nTry {Config.Prefix}wow rankings Realm Name, Guild Name"; + sb.AppendLine($"Command syntax: {Config.Prefix}wow rankings realm name, guild name"); + sb.AppendLine($"Command example: {Config.Prefix}wow rankings azgalor, carebears"); embed.Description = sb.ToString(); await _cc.Reply(Context, embed); return; @@ -1178,9 +1178,9 @@ private async Task GetWowRankings(string args = "") StringBuilder sb = new StringBuilder(); var embed = new EmbedBuilder(); embed.WithColor(new Color(255, 0, 0)); - embed.Title = $"Unable to find a guild/realm association!\nTry {NinjaBot.Prefix}wow rankings Realm Name, Guild Name"; - sb.AppendLine($"Command syntax: {NinjaBot.Prefix}wow rankings realm name, guild name"); - sb.AppendLine($"Command example: {NinjaBot.Prefix}wow rankings azgalor, carebears"); + embed.Title = $"Unable to find a guild/realm association!\nTry {Config.Prefix}wow rankings Realm Name, Guild Name"; + sb.AppendLine($"Command syntax: {Config.Prefix}wow rankings realm name, guild name"); + sb.AppendLine($"Command example: {Config.Prefix}wow rankings azgalor, carebears"); embed.Description = sb.ToString(); await _cc.Reply(Context, embed); return; @@ -1234,8 +1234,8 @@ private async Task GetWowRankings(string args = "") var embed = new EmbedBuilder(); embed.WithColor(new Color(255, 0, 0)); embed.Title = $":frowning: Sorry, {Context.User.Username}, something went wrong! Perhaps check the guild's home realm.:frowning: "; - sb.AppendLine($"Command syntax: {NinjaBot.Prefix}wow rankings realm name, guild name"); - sb.AppendLine($"Command example: {NinjaBot.Prefix}wow rankings azgalor, carebears"); + sb.AppendLine($"Command syntax: {Config.Prefix}wow rankings realm name, guild name"); + sb.AppendLine($"Command example: {Config.Prefix}wow rankings azgalor, carebears"); embed.Description = sb.ToString(); await _cc.Reply(Context, embed); } diff --git a/Modules/YouTube/YouTubeApi.cs b/Modules/YouTube/YouTubeApi.cs index bb47345..7760214 100644 --- a/Modules/YouTube/YouTubeApi.cs +++ b/Modules/YouTube/YouTubeApi.cs @@ -18,7 +18,7 @@ namespace NinjaBotCore.Modules.YouTube { public class YouTubeApi { - private string key = NinjaBot.YouTubeApi; + private string key = Config.YouTubeApi; private string getYouTubeApiRequest(string url) { @@ -65,7 +65,7 @@ public async Task> SearchChannelsAsync(string keyword = "spac { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { - ApiKey = NinjaBot.YouTubeApi, + ApiKey = Config.YouTubeApi, ApplicationName = this.GetType().ToString() }); diff --git a/NinjaBot.cs b/NinjaBot.cs index 52361b9..a66fca6 100644 --- a/NinjaBot.cs +++ b/NinjaBot.cs @@ -21,185 +21,16 @@ namespace NinjaBotCore public class NinjaBot { private DiscordSocketClient _client; - private CommandHandler _handler; - private string _token; - string configFile; - private static char _prefix; - private static string _wowApi; - private static string _giphyApi; - private static string _googleMapsApi; - private static string _youTubeApi; - private static string _darkSkyApi; - private static string _steamApi; - private static string _faceApi; - private static string _imageApi; - private static string _oxfordDictionaryApi; - private static string _donateUrl; - private static string _warcraftlogsApi; - public static List OutputChannels { get; private set; } - - public static string WarcraftLogsApi - { - get - { - return _warcraftlogsApi; - } - set - { - _warcraftlogsApi = value; - } - } - public static string DonateUrl - { - get - { - return _donateUrl; - } - set - { - _donateUrl = value; - } - } - public static string OxfordDictionaryApi - { - get - { - return _oxfordDictionaryApi; - } - private set - { - _oxfordDictionaryApi = value; - } - } - public static string FaceApi - { - get - { - return _faceApi; - } - private set - { - _faceApi = value; - } - } - public static char Prefix - { - get - { - return _prefix; - } - private set - { - _prefix = value; - } - } - public static string WowApi - { - get - { - return _wowApi; - } - private set - { - _wowApi = value; - } - } - public static string GiphyApi - { - get - { - return _giphyApi; - } - private set - { - _giphyApi = value; - } - } - public static string GoogleMapsApi - { - get - { - return _googleMapsApi; - } - private set - { - _googleMapsApi = value; - } - } - public static string YouTubeApi - { - get - { - return _youTubeApi; - } - private set - { - _youTubeApi = value; - } - } - public static string DarkSkyApi - { - get - { - return _darkSkyApi; - } - private set - { - _darkSkyApi = value; - } - } - public static string SteamApi - { - get - { - return _steamApi; - } - private set - { - _steamApi = value; - } - } - public static string ImageApi - { - get - { - return _imageApi; - } - private set - { - _imageApi = value; - } - } - + private CommandHandler _handler; + public NinjaBot() { - //Make sure we have a db file (if not, create it) + //Make sure we have a db file (if not, create one) using (var db = new NinjaBotEntities()) { db.Database.EnsureCreated(); } - configFile = "config.json"; - ConfigData botConfig = GetConfigData(configFile); - if (botConfig == null) - { - throw new InvalidDataException("Config file access error!"); - } - else - { - _token = botConfig.Token; - Prefix = botConfig.Prefix; - WowApi = botConfig.WowApi; - GiphyApi = botConfig.GiphyApi; - GoogleMapsApi = botConfig.GoogleMapsApi; - YouTubeApi = botConfig.YouTubeApi; - DarkSkyApi = botConfig.DarkSkyApi; - SteamApi = botConfig.SteamApi; - FaceApi = botConfig.FaceApi; - ImageApi = botConfig.ImageApi; - OxfordDictionaryApi = botConfig.OxfordDictionaryApi; - WarcraftLogsApi = botConfig.WarcraftlogsApi; - DonateUrl = botConfig.DonateUrl; - } + new Config(); } public async Task Start() @@ -215,7 +46,7 @@ public async Task Start() // Login and connect to Discord. _client.Log += Log; var serviceProvider = ConfigureServices(); - await _client.LoginAsync(TokenType.Bot, _token); + await _client.LoginAsync(TokenType.Bot, Config.Token); await _client.StartAsync(); _handler = new CommandHandler(serviceProvider); await _handler.ConfigureAsync(); @@ -225,24 +56,6 @@ public async Task Start() // Block this program until it is closed. await Task.Delay(-1); } - - ConfigData GetConfigData(string configFile) - { - ConfigData configData = null; - try - { - if (File.Exists(configFile)) - { - configData = JsonConvert.DeserializeObject(File.ReadAllText(configFile)); - } - return configData; - } - catch (Exception ex) - { - Console.WriteLine($"Error getting configuration information -> {ex.Message}"); - return configData; - } - } private IServiceProvider ConfigureServices() { var services = new ServiceCollection()