Skip to content

Commit

Permalink
Allow for custom alerts file for subscriptions (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed Nov 6, 2020
1 parent d6dd279 commit ebd27c1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 deletions.
6 changes: 4 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"iconStyle": "Default",
"botChannelIds": [],
"status": null
"status": null,
"dmAlertsFile": "default.json"
},
"000000000000000002": {
"commandPrefix": ".",
Expand Down Expand Up @@ -59,7 +60,8 @@
},
"iconStyle": "Default",
"botChannelIds": [],
"status": "Test #2"
"status": "Test #2",
"dmAlertsFile": "default.json"
}
},
"database": {
Expand Down
3 changes: 1 addition & 2 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/Configuration/DiscordServerConfig.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Discord server configuration class
/// </summary>
Expand Down Expand Up @@ -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; }

/// <summary>
/// Instantiate a new <see cref="DiscordServerConfig"/> class
/// </summary>
Expand All @@ -137,6 +149,15 @@ public DiscordServerConfig()
QuestChannelIds = new List<ulong>();
ShinyStats = new ShinyStatsConfig();
NestsMinimumPerHour = 1;
DmAlertsFile = "default.json";

LoadDmAlerts();
}

public void LoadDmAlerts()
{
var path = Path.Combine(Strings.AlertsFolder, DmAlertsFile);
DmAlerts = MasterFile.LoadInit<AlertMessage>(path);
}
}
}
3 changes: 2 additions & 1 deletion src/Net/Models/GymDetailsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 2 additions & 3 deletions src/Net/Models/PokemonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

using DSharpPlus;
using DSharpPlus.Entities;

using Newtonsoft.Json;
using ServiceStack.DataAnnotations;

Expand Down Expand Up @@ -402,9 +401,9 @@ public void SetDespawnTime()
public async Task<DiscordEmbedNotification> 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
Expand Down
3 changes: 2 additions & 1 deletion src/Net/Models/PokestopData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion src/Net/Models/QuestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/Net/Models/RaidData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public void SetTimes()
/// <returns>DiscordEmbedNotification object to send</returns>
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);
Expand Down
4 changes: 2 additions & 2 deletions src/Net/Models/WeatherData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebd27c1

Please sign in to comment.