From ebd27c16ab325f5b69290468a6d58948951646a5 Mon Sep 17 00:00:00 2001 From: versx Date: Fri, 6 Nov 2020 10:26:10 -0800 Subject: [PATCH] Allow for custom alerts file for subscriptions (#60) --- config.example.json | 6 ++++-- src/Bot.cs | 3 +-- src/Configuration/DiscordServerConfig.cs | 21 +++++++++++++++++++++ src/Net/Models/GymDetailsData.cs | 3 ++- src/Net/Models/PokemonData.cs | 5 ++--- src/Net/Models/PokestopData.cs | 3 ++- src/Net/Models/QuestData.cs | 3 ++- src/Net/Models/RaidData.cs | 4 ++-- src/Net/Models/WeatherData.cs | 4 ++-- 9 files changed, 38 insertions(+), 14 deletions(-) diff --git a/config.example.json b/config.example.json index ab419559..8a95d381 100644 --- a/config.example.json +++ b/config.example.json @@ -31,7 +31,8 @@ }, "iconStyle": "Default", "botChannelIds": [], - "status": null + "status": null, + "dmAlertsFile": "default.json" }, "000000000000000002": { "commandPrefix": ".", @@ -59,7 +60,8 @@ }, "iconStyle": "Default", "botChannelIds": [], - "status": "Test #2" + "status": "Test #2", + "dmAlertsFile": "default.json" } }, "database": { diff --git a/src/Bot.cs b/src/Bot.cs index 32b4477e..487ad871 100644 --- a/src/Bot.cs +++ b/src/Bot.cs @@ -32,8 +32,6 @@ // TODO: Check nests again // TODO: IV wildcards // TODO: Egg subscriptions (maybe) - // TODO: Osm nominatim reverse geocoding - // TODO: Fix race condition between incoming messages and when emojis list is initialized public class Bot { @@ -99,6 +97,7 @@ public Bot(WhConfig whConfig) { var guildId = keys[i]; var server = _whConfig.Servers[guildId]; + server.LoadDmAlerts(); var client = new DiscordClient(new DiscordConfiguration { AutomaticGuildSync = true, diff --git a/src/Configuration/DiscordServerConfig.cs b/src/Configuration/DiscordServerConfig.cs index 8bce562a..572608b1 100644 --- a/src/Configuration/DiscordServerConfig.cs +++ b/src/Configuration/DiscordServerConfig.cs @@ -1,8 +1,14 @@ namespace WhMgr.Configuration { + using System; using System.Collections.Generic; + using System.IO; + using Newtonsoft.Json; + using WhMgr.Alarms.Alerts; + using WhMgr.Data; + /// /// Discord server configuration class /// @@ -125,6 +131,12 @@ public class DiscordServerConfig [JsonProperty("status")] public string Status { get; set; } + [JsonProperty("dmAlertsFile")] + public string DmAlertsFile { get; set; } + + [JsonIgnore] + public AlertMessage DmAlerts { get; set; } + /// /// Instantiate a new class /// @@ -137,6 +149,15 @@ public DiscordServerConfig() QuestChannelIds = new List(); ShinyStats = new ShinyStatsConfig(); NestsMinimumPerHour = 1; + DmAlertsFile = "default.json"; + + LoadDmAlerts(); + } + + public void LoadDmAlerts() + { + var path = Path.Combine(Strings.AlertsFolder, DmAlertsFile); + DmAlerts = MasterFile.LoadInit(path); } } } \ No newline at end of file diff --git a/src/Net/Models/GymDetailsData.cs b/src/Net/Models/GymDetailsData.cs index 6afa5002..b75f15b0 100644 --- a/src/Net/Models/GymDetailsData.cs +++ b/src/Net/Models/GymDetailsData.cs @@ -88,8 +88,9 @@ public sealed class GymDetailsData public DiscordEmbedNotification GenerateGymMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, GymDetailsData oldGym, string city) { + var server = whConfig.Servers[guildId]; var alertType = AlertMessageType.Gyms; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var properties = GetProperties(client.Guilds[guildId], whConfig, city, oldGym); var eb = new DiscordEmbedBuilder { diff --git a/src/Net/Models/PokemonData.cs b/src/Net/Models/PokemonData.cs index d6302b69..e3775a5e 100644 --- a/src/Net/Models/PokemonData.cs +++ b/src/Net/Models/PokemonData.cs @@ -8,7 +8,6 @@ using DSharpPlus; using DSharpPlus.Entities; - using Newtonsoft.Json; using ServiceStack.DataAnnotations; @@ -402,9 +401,9 @@ public void SetDespawnTime() public async Task GeneratePokemonMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city) { // If IV has value then use alarmText if not null otherwise use default. If no stats use default missing stats alarmText - var alertType = IsMissingStats ? AlertMessageType.PokemonMissingStats : AlertMessageType.Pokemon; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; var server = whConfig.Servers[guildId]; + var alertType = IsMissingStats ? AlertMessageType.PokemonMissingStats : AlertMessageType.Pokemon; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var pokemonImageUrl = IconFetcher.Instance.GetPokemonIcon(server.IconStyle, Id, FormId, 0, Gender, Costume, false); var properties = await GetProperties(client.Guilds[guildId], whConfig, city, pokemonImageUrl); var eb = new DiscordEmbedBuilder diff --git a/src/Net/Models/PokestopData.cs b/src/Net/Models/PokestopData.cs index ab85ca05..d22aa530 100644 --- a/src/Net/Models/PokestopData.cs +++ b/src/Net/Models/PokestopData.cs @@ -108,8 +108,9 @@ public void SetTimes() public DiscordEmbedNotification GeneratePokestopMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city) { + var server = whConfig.Servers[guildId]; var alertType = HasInvasion ? AlertMessageType.Invasions : HasLure ? AlertMessageType.Lures : AlertMessageType.Pokestops; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var properties = GetProperties(client.Guilds[guildId], whConfig, city); var eb = new DiscordEmbedBuilder { diff --git a/src/Net/Models/QuestData.cs b/src/Net/Models/QuestData.cs index 8043c36d..eed233fb 100644 --- a/src/Net/Models/QuestData.cs +++ b/src/Net/Models/QuestData.cs @@ -81,8 +81,9 @@ public QuestData() public DiscordEmbedNotification GenerateQuestMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city) { + var server = whConfig.Servers[guildId]; var alertType = AlertMessageType.Quests; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var properties = GetProperties(client.Guilds[guildId], whConfig, city, IconFetcher.Instance.GetQuestIcon(whConfig.Servers[guildId].IconStyle, this)); var eb = new DiscordEmbedBuilder { diff --git a/src/Net/Models/RaidData.cs b/src/Net/Models/RaidData.cs index 9fa4cef3..e83e31a4 100644 --- a/src/Net/Models/RaidData.cs +++ b/src/Net/Models/RaidData.cs @@ -150,9 +150,9 @@ public void SetTimes() /// DiscordEmbedNotification object to send public DiscordEmbedNotification GenerateRaidMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city) { - var alertType = PokemonId > 0 ? AlertMessageType.Raids : AlertMessageType.Eggs; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; var server = whConfig.Servers[guildId]; + var alertType = PokemonId > 0 ? AlertMessageType.Raids : AlertMessageType.Eggs; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var raidImageUrl = IsEgg ? IconFetcher.Instance.GetRaidEggIcon(server.IconStyle, Convert.ToInt32(Level), false, IsExEligible) : IconFetcher.Instance.GetPokemonIcon(server.IconStyle, PokemonId, Form, Evolution, Gender, Costume, false); diff --git a/src/Net/Models/WeatherData.cs b/src/Net/Models/WeatherData.cs index 469c7e17..4519daf5 100644 --- a/src/Net/Models/WeatherData.cs +++ b/src/Net/Models/WeatherData.cs @@ -109,9 +109,9 @@ public void SetTimes() public DiscordEmbedNotification GenerateWeatherMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city) { - var alertType = AlertMessageType.Weather; - var alert = alarm?.Alerts[alertType] ?? AlertMessage.Defaults[alertType]; var server = whConfig.Servers[guildId]; + var alertType = AlertMessageType.Weather; + var alert = alarm?.Alerts[alertType] ?? server.DmAlerts?[alertType] ?? AlertMessage.Defaults[alertType]; var weatherImageUrl = IconFetcher.Instance.GetWeatherIcon(server.IconStyle, GameplayCondition); var properties = GetProperties(client.Guilds[guildId], whConfig, city, weatherImageUrl); var eb = new DiscordEmbedBuilder