Skip to content

Commit

Permalink
Support to enable/disable notification subscriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed Nov 12, 2018
1 parent 6124f3d commit 6897447
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 17 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Works with [RealDeviceMap](https://github.com/123FLO321/RealDeviceMap)
//Google maps key.
"gmapsKey": "<GOOGLE_MAPS_KEY>",
//Enable discord user subscriptions for custom notifications to Quests/Raids/Pokemon, if enabled database information is required below.
"enableSubscriptions": false,
//MySQL database connection string.
"connectionString": "Uid=user;Password=password;Server=127.0.0.1;Port=3306;Database=brockdb",
Expand Down Expand Up @@ -137,4 +140,10 @@ Works with [RealDeviceMap](https://github.com/123FLO321/RealDeviceMap)
34.01,-117.01
34.02,-117.02
34.03,-117.03
```
```


## TODO:
- Allow Pokemon id and name in Pokemon filter lists.
- Localization
- Wiki
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"token": "<DISCORD_BOT_TOKEN>",
"enabled": true,
"ownerId": 000000000000,
"supporterRoleId": 000000000000,
"moderators": [
Expand All @@ -9,6 +8,7 @@
"guildId": 000000000000,
"webhookPort": 8002,
"gmapsKey": "<GOOGLE_MAPS_KEY>",
"enableSubscriptions": false,
"connectionString": "",
"cityRoles": [
"City1",
Expand Down
26 changes: 20 additions & 6 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

using ServiceStack.OrmLite;

//TODO: Quest subscription notifications.
//TODO: Filter quests by rewards.

public class Bot
{
#region Variables
Expand Down Expand Up @@ -56,8 +53,11 @@ public Bot(WhConfig whConfig)
_whm.RaidAlarmTriggered += OnRaidAlarmTriggered;
_whm.QuestAlarmTriggered += OnQuestAlarmTriggered;
_whm.PokemonSubscriptionTriggered += OnPokemonSubscriptionTriggered;
_whm.RaidSubscriptionTriggered += OnRaidSubscriptionTriggered;
_whm.QuestSubscriptionTriggered += OnQuestSubscriptionTriggered;
if (_whConfig.EnableSubscriptions)
{
_whm.RaidSubscriptionTriggered += OnRaidSubscriptionTriggered;
_whm.QuestSubscriptionTriggered += OnQuestSubscriptionTriggered;
}

_logger.Info("WebHookManager is running...");

Expand All @@ -81,12 +81,17 @@ public Bot(WhConfig whConfig)
_client.ClientErrored += Client_ClientErrored;
_client.DebugLogger.LogMessageReceived += DebugLogger_LogMessageReceived;

if (_whConfig.EnableSubscriptions)
{
_subMgr = new SubscriptionManager();
}

DependencyCollection dep;
using (var d = new DependencyCollectionBuilder())
{
d.AddInstance
(
_dep = new Dependencies(_subMgr = new SubscriptionManager(), _whConfig)
_dep = new Dependencies(_subMgr, _whConfig)
//LobbyManager = new RaidLobbyManager(_client, _config, _logger, notificationProcessor.GeofenceSvc),
//ReminderSvc = new ReminderService(_client, _db, _logger),
//PoGoVersionMonitor = new PokemonGoVersionMonitor(),
Expand Down Expand Up @@ -367,6 +372,9 @@ private void OnQuestSubscriptionTriggered(object sender, QuestData e)

private async Task ProcessPokemonSubscription(PokemonData pkmn)
{
if (!_whConfig.EnableSubscriptions)
return;

var db = Database.Instance;
if (!db.Pokemon.ContainsKey(pkmn.Id))
return;
Expand Down Expand Up @@ -467,6 +475,9 @@ private async Task ProcessPokemonSubscription(PokemonData pkmn)

private async Task ProcessRaidSubscription(RaidData raid)
{
if (!_whConfig.EnableSubscriptions)
return;

var db = Database.Instance;
if (!db.Pokemon.ContainsKey(raid.PokemonId))
return;
Expand Down Expand Up @@ -565,6 +576,9 @@ private async Task ProcessRaidSubscription(RaidData raid)

private async Task ProcessQuestSubscription(QuestData quest)
{
if (!_whConfig.EnableSubscriptions)
return;

var db = Database.Instance;
var reward = quest.Rewards[0].Info;
var rewardKeyword = quest.GetRewardString();
Expand Down
48 changes: 48 additions & 0 deletions src/Commands/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public Notifications(Dependencies dep)
public async Task InfoAsync(CommandContext ctx,
[Description("Discord user mention string.")] string mention = "")
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (string.IsNullOrEmpty(mention))
{
await SendUserSubscriptionSettings(ctx.Client, ctx.User, ctx.User.Id);
Expand Down Expand Up @@ -63,6 +69,12 @@ public async Task InfoAsync(CommandContext ctx,
]
public async Task EnableDisableAsync(CommandContext ctx)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (!_dep.SubscriptionManager.UserExists(ctx.User.Id))
{
await ctx.TriggerTypingAsync();
Expand All @@ -89,6 +101,12 @@ public async Task PokeMeAsync(CommandContext ctx,
[Description("Minimum level to receive notifications for, use 0 to disregard level.")] int lvl = 0,
[Description("Specific gender the Pokemon must be, use * to disregard gender.")] string gender = "*")
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

var isSupporter = ctx.Client.IsSupporterOrHigher(ctx.User.Id, _dep.WhConfig);
if (!isSupporter)
{
Expand Down Expand Up @@ -304,6 +322,12 @@ await ctx.RespondAsync
public async Task PokeMeNotAsync(CommandContext ctx,
[Description("Pokemon name or id to unsubscribe from Pokemon spawn notifications.")] string poke)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (!_dep.SubscriptionManager.UserExists(ctx.User.Id))
{
await ctx.TriggerTypingAsync();
Expand Down Expand Up @@ -389,6 +413,12 @@ public async Task RaidMeAsync(CommandContext ctx,
[Description("Pokemon name or id to subscribe to raid notifications.")] string poke,
[Description("City to send the notification if the raid appears in otherwise if null all will be sent.")] string city = null)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

var isSupporter = ctx.Client.IsSupporterOrHigher(ctx.User.Id, _dep.WhConfig);
if (!isSupporter)
{
Expand Down Expand Up @@ -525,6 +555,12 @@ public async Task RaidMeNotAsync(CommandContext ctx,
[Description("Pokemon name or id to unsubscribe from raid notifications.")] string poke,
[Description("City to remove the quest notifications from otherwise if null all will be sent.")] string city = null)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (string.Compare(city, Strings.All, true) != 0 && !string.IsNullOrEmpty(city))
{
if (_dep.WhConfig.CityRoles.Find(x => string.Compare(x.ToLower(), city.ToLower(), true) == 0) == null)
Expand Down Expand Up @@ -622,6 +658,12 @@ public async Task QuestMeAsync(CommandContext ctx,
[Description("Reward keyword to use to find field research. Example: Spinda, 1200 stardust, candy")] string rewardKeyword,
[Description("City to send the notification if the quest appears in otherwise if null all will be sent.")] string city = null)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (string.Compare(city, Strings.All, true) != 0 && !string.IsNullOrEmpty(city))
{
if (_dep.WhConfig.CityRoles.Find(x => string.Compare(x.ToLower(), city.ToLower(), true) == 0) == null)
Expand Down Expand Up @@ -685,6 +727,12 @@ public async Task QuestMeNotAsync(CommandContext ctx,
[Description("Reward keyword to remove from field research quest subscriptions. Example: Spinda, 1200 stardust, candy")] string rewardKeyword,
[Description("City to remove the quest notifications from otherwise if null all will be sent.")] string city = null)
{
if (!_dep.WhConfig.EnableSubscriptions)
{
await ctx.RespondAsync($"{ctx.User.Mention} Subscriptions are not enabled in the config.");
return;
}

if (string.Compare(city, Strings.All, true) != 0 && !string.IsNullOrEmpty(city))
{
if (_dep.WhConfig.CityRoles.Find(x => string.Compare(x.ToLower(), city.ToLower(), true) == 0) == null)
Expand Down
3 changes: 3 additions & 0 deletions src/Configuration/WhConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class WhConfig
[JsonProperty("mapProviderFork")]
public MapProviderFork MapProviderFork { get; set; }

[JsonProperty("enableSubscriptions")]
public bool EnableSubscriptions { get; set; }

[JsonProperty("connectionString")]
public string ConnectionString { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/Data/DataAccessLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class DataAccessLayer

public static IDbConnection CreateFactory()
{
if (string.IsNullOrEmpty(ConnectionString))
return null;

var factory = new OrmLiteConnectionFactory(ConnectionString, MySqlDialect.Provider);
return factory.Open();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Data/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ private void CreateDefaultTables()

using (var db = DataAccessLayer.CreateFactory())
{
if (db == null)
return;

db.CreateTable<SubscriptionObject>();
db.CreateTable<PokemonSubscription>();
db.CreateTable<RaidSubscription>();
Expand Down
9 changes: 0 additions & 9 deletions src/WhMgr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
Expand All @@ -82,12 +79,6 @@
<HintPath>..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
Expand Down

0 comments on commit 6897447

Please sign in to comment.