Skip to content

Commit

Permalink
Adding configuration class to store API and bot data
Browse files Browse the repository at this point in the history
  • Loading branch information
gngrninja committed Jun 25, 2017
1 parent a3abc71 commit d5ec964
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 244 deletions.
2 changes: 1 addition & 1 deletion CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Modules/Admin/Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ private async Task<string> 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
{
Expand Down
211 changes: 211 additions & 0 deletions Modules/Config.cs
Original file line number Diff line number Diff line change
@@ -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<ConfigData>(File.ReadAllText(configFile));
}
return configData;
}
catch (Exception ex)
{
Console.WriteLine($"Error getting configuration information -> {ex.Message}");
return configData;
}
}
}
}
18 changes: 9 additions & 9 deletions Modules/Fun/FunCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Modules/Fun/OxfordApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion Modules/Giphy/GiphyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down
10 changes: 5 additions & 5 deletions Modules/Giphy/GiphyCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Modules/Steam/SteamApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions Modules/Weather/WeatherApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Wow/WarcraftLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down
2 changes: 1 addition & 1 deletion Modules/Wow/WowApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
Loading

0 comments on commit d5ec964

Please sign in to comment.