Skip to content

Commit

Permalink
No-Area Subscriptions (#83)
Browse files Browse the repository at this point in the history
* Allow subscriptions with no cities specified

* trigger rebuild

* Fix merge conflict
  • Loading branch information
versx authored Mar 7, 2021
1 parent 565838f commit 7a0df3c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

// TODO: List all subscriptions with info command
// TODO: Multiple discord bot tokens per server
// TODO: Check nests again
// TODO: IV wildcards
// TODO: Egg subscriptions (maybe)

Expand Down
6 changes: 2 additions & 4 deletions src/Commands/Input/SubscriptionInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ public async Task<List<string>> GetAreasResult(ulong guildId)
var validAreas = server.EnableCities ? server.CityRoles : server.Geofences.Select(g => g.Name).ToList();
var message = (await _context.RespondEmbed($"Enter the areas to get notifications from separated by a comma (i.e. `city1,city2`):\n**Available Areas:**\n{string.Join("\n- ", validAreas)}\n- All", DiscordColor.Blurple)).FirstOrDefault();
var cities = await _context.WaitForUserChoice(true);
await message.DeleteAsync();

// Check if gender is a valid gender provided
// Check if provided areas are valid and only return valid areas
var areas = SubscriptionAreas.GetAreas(server, cities);
if (areas.Count == 0)
{
// No valid areas provided
await _context.RespondEmbed($"Invalid areas provided.");
return new List<string>();
}
await message.DeleteAsync();

return areas;
}
}
Expand Down
78 changes: 59 additions & 19 deletions src/Commands/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public async Task PokeMeAsync(CommandContext ctx,
[Description("Minimum IV to receive notifications for, use 0 to disregard IV. i.e. 100 or 0-15-15")] string iv = "0",
[Description("Minimum level and maximum level to receive notifications for, use 0 to disregard level. Set a maximum value with 15-35.")] string lvl = "0",
[Description("Specific gender the Pokemon must be, use * to disregard gender. (*, m, f)")] string gender = "*",
[Description("City or area to add to the subscription, or leave blank for all cities."), RemainingText] string city = "all")
[Description("City or area to add to the subscription, 'All' for all areas, or leave blank to use set distance."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -381,6 +381,11 @@ public async Task PokeMeAsync(CommandContext ctx,
}

var areas = SubscriptionAreas.GetAreas(server, city);
if (areas.Count == 0 && subscription.DistanceM == 0)
{
await ctx.RespondEmbed($"{ctx.User.Username}#{ctx.User.Discriminator} You must either set a distance to receive notifications from (`set-distance <meters> <latitude>,<longitude>`) or provide a city/area for the subscription. Aborting request.", DiscordColor.Red);
return;
}

// Loop through each valid pokemon entry provided
foreach (var (pokemonId, form) in validation.Valid)
Expand Down Expand Up @@ -501,7 +506,7 @@ await ctx.RespondEmbed
]
public async Task PokeMeNotAsync(CommandContext ctx,
[Description("Pokemon name or id to unsubscribe from Pokemon spawn notifications.")] string poke,
[Description("City or area to remove from the subscription, or leave blank for all cities."), RemainingText] string city = "all")
[Description("City or area to remove from the subscription, 'All' for all areas, or leave blank to remove the whole subscription.")] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -556,7 +561,9 @@ public async Task PokeMeNotAsync(CommandContext ctx,
}

// Check if there are no more areas set for the Pokemon subscription
if (subPkmn.Areas.Count == 0)
//if (subPkmn.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the Pokemon subscription, delete it
var result = subPkmn.Id.Remove<PokemonSubscription>();
Expand Down Expand Up @@ -593,7 +600,7 @@ public async Task PokeMeNotAsync(CommandContext ctx,
]
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."), RemainingText] string city = "all")
[Description("City or area to add to the subscription, 'All' for all areas, or leave blank to use set distance."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand All @@ -620,6 +627,12 @@ public async Task RaidMeAsync(CommandContext ctx,
}

var areas = SubscriptionAreas.GetAreas(server, city);
if (areas.Count == 0 && subscription.DistanceM == 0)
{
await ctx.RespondEmbed($"{ctx.User.Username}#{ctx.User.Discriminator} You must either set a distance to receive notifications from (`set-distance <meters> <latitude>,<longitude>`) or provide a city/area for the subscription. Aborting request.", DiscordColor.Red);
return;
}

foreach (var (pokemonId, form) in validation.Valid)
{
var subRaid = subscription.Raids.FirstOrDefault(x => x.PokemonId == pokemonId && string.Compare(x.Form, form, true) == 0);
Expand Down Expand Up @@ -669,7 +682,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_RAID_SUBSCRIPTIONS
]
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."), RemainingText] string city = "all")
[Description("City or area to remove from the subscription, 'All' for all areas, or leave blank to remove the whole subscription."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -729,7 +742,9 @@ await ctx.RespondEmbed(Translator.Instance.Translate("ERROR_NO_RAID_SUBSCRIPTION
}

// Check if there are no more areas set for the Pokemon subscription
if (subRaid.Areas.Count == 0)
//if (subRaid.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the Pokemon subscription, delete it
if (!subRaid.Id.Remove<RaidSubscription>())
Expand Down Expand Up @@ -765,7 +780,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_RAID_SUBSCRIPTIONS
]
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."), RemainingText] string city = "all")
[Description("City or area to add to the subscription, 'All' for all areas, or leave blank to use set distance."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand All @@ -785,6 +800,12 @@ public async Task QuestMeAsync(CommandContext ctx,
}

var areas = SubscriptionAreas.GetAreas(server, city);
if (areas.Count == 0 && subscription.DistanceM == 0)
{
await ctx.RespondEmbed($"{ctx.User.Username}#{ctx.User.Discriminator} You must either set a distance to receive notifications from (`set-distance <meters> <latitude>,<longitude>`) or provide a city/area for the subscription. Aborting request.", DiscordColor.Red);
return;
}

var subQuest = subscription.Quests.FirstOrDefault(x => string.Compare(x.RewardKeyword, rewardKeyword, true) == 0);
if (subQuest != null)
{
Expand Down Expand Up @@ -829,7 +850,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_QUEST_SUBSCRIPTION
]
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 removed."), RemainingText] string city = "all")
[Description("City or area to remove from the subscription, 'All' for all areas, or leave blank to remove the whole subscription."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -883,7 +904,9 @@ await ctx.RespondEmbed(Translator.Instance.Translate("ERROR_NO_QUEST_SUBSCRIPTIO
}

// Check if there are no more areas set for the Pokemon subscription
if (subQuest.Areas.Count == 0)
//if (subQuest.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the Pokemon subscription, delete it
if (!subQuest.Id.Remove<QuestSubscription>())
Expand Down Expand Up @@ -998,7 +1021,7 @@ public async Task GymMeNotAsync(CommandContext ctx,
]
public async Task InvMeAsync(CommandContext ctx,
[Description("Comma delimited list of Pokemon name(s) and/or Pokedex IDs to subscribe to rewards from Team Rocket Invasion notifications.")] string poke,
[Description("City to send the notification if the invasion appears in otherwise if null all will be sent."), RemainingText] string city = "all")
[Description("City or area to add to the subscription, 'All' for all areas, or leave blank to use set distance."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand All @@ -1025,6 +1048,12 @@ public async Task InvMeAsync(CommandContext ctx,
}

var areas = SubscriptionAreas.GetAreas(server, city);
if (areas.Count == 0 && subscription.DistanceM == 0)
{
await ctx.RespondEmbed($"{ctx.User.Username}#{ctx.User.Discriminator} You must either set a distance to receive notifications from (`set-distance <meters> <latitude>,<longitude>`) or provide a city/area for the subscription. Aborting request.", DiscordColor.Red);
return;
}

foreach (var (pokemonId, form) in validation.Valid)
{
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonId == pokemonId);
Expand Down Expand Up @@ -1074,7 +1103,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_INVASION_SUBSCRIPT
]
public async Task InvMeNotAsync(CommandContext ctx,
[Description("Comma delimited list of Pokemon name(s) and/or Pokedex IDs to unsubscribe from rewards for Team Rocket Invasion notifications.")] string poke,
[Description("City to send the notification if the raid appears in otherwise if null all will be sent."), RemainingText] string city = "all")
[Description("City or area to remove from the subscription, 'All' for all areas, or leave blank to remove the whole subscription."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -1132,7 +1161,9 @@ await ctx.RespondEmbed(Translator.Instance.Translate("ERROR_NO_INVASION_SUBSCRIP
}

// Check if there are no more areas set for the invasion subscription
if (subInvasion.Areas.Count == 0)
//if (subInvasion.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the invasion subscription, delete it
if (!subInvasion.Id.Remove<InvasionSubscription>())
Expand Down Expand Up @@ -1218,7 +1249,7 @@ public async Task PvpMeAsync(CommandContext ctx,
[Description("PvP league")] string league,
[Description("Minimum PvP ranking.")] int minimumRank = 5,
[Description("Minimum PvP rank percentage.")] double minimumPercent = 0.0,
[Description("City to send the notification if the PvP Pokemon appears in otherwise if null all will be sent"), RemainingText] string city = "all")
[Description("City or area to add to the subscription, 'All' for all areas, or leave blank to use set distance."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -1274,6 +1305,12 @@ public async Task PvpMeAsync(CommandContext ctx,
}

var areas = SubscriptionAreas.GetAreas(server, city);
if (areas.Count == 0 && subscription.DistanceM == 0)
{
await ctx.RespondEmbed($"{ctx.User.Username}#{ctx.User.Discriminator} You must either set a distance to receive notifications from (`set-distance <meters> <latitude>,<longitude>`) or provide a city/area for the subscription. Aborting request.", DiscordColor.Red);
return;
}

foreach (var (pokemonId, form) in validation.Valid)
{
if (!MasterFile.Instance.Pokedex.ContainsKey(pokemonId))
Expand Down Expand Up @@ -1365,7 +1402,7 @@ await ctx.RespondEmbed
public async Task PvpMeNotAsync(CommandContext ctx,
[Description("Comma delimited list of Pokemon name(s) and/or Pokedex IDs to subscribe to Pokemon spawn notifications.")] string poke,
[Description("PvP league")] string league,
[Description("City to remove the PvP notifications from otherwise if null all will be removed."), RemainingText] string city = "all")
[Description("City or area to remove from the subscription, 'All' for all areas, or leave blank to remove the whole subscription."), RemainingText] string city = null)
{
if (!await CanExecute(ctx))
return;
Expand Down Expand Up @@ -2373,7 +2410,7 @@ private async Task RemovePokemonSubscription(CommandContext ctx, SubscriptionObj
}

// Check if there are no more areas set for the Pokemon subscription
if (subPkmn.Areas.Count == 0)
if (areas.Count == 0)
{
// If no more areas set for the Pokemon subscription, delete it
var result = subPkmn.Id.Remove<PokemonSubscription>();
Expand Down Expand Up @@ -2417,8 +2454,8 @@ private async Task RemovePvPSubscription(CommandContext ctx, SubscriptionObject
}
}

// Check if there are no more areas set for the PvP Pokemon subscription
if (subPvP.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (areas.Count == 0)
{
// If no more areas set for the PvP Pokemon subscription, delete it
var result = subPvP.Id.Remove<PvPSubscription>();
Expand Down Expand Up @@ -2466,7 +2503,7 @@ private async Task RemoveRaidSubscription(CommandContext ctx, SubscriptionObject
}

// Check if there are no more areas set for the Pokemon subscription
if (subRaid.Areas.Count == 0)
if (areas.Count == 0)
{
// If no more areas set for the Pokemon subscription, delete it
if (!subRaid.Id.Remove<RaidSubscription>())
Expand Down Expand Up @@ -3195,9 +3232,12 @@ internal class SubscriptionAreas
{
public static List<string> GetAreas(DiscordServerConfig server, string city)
{
if (string.IsNullOrEmpty(city))
return new List<string>();

// Parse user defined cities
var validCities = server.EnableCities ? server.CityRoles : server.Geofences.Select(g => g.Name).ToList();
var cities = string.IsNullOrEmpty(city) || string.Compare(city, Strings.All, true) == 0
var cities = /*string.IsNullOrEmpty(city) ||*/ string.Compare(city, Strings.All, true) == 0
? validCities
: city.RemoveSpaces();
var validAreas = validCities.Select(x => x.ToLower());
Expand Down
1 change: 0 additions & 1 deletion src/Data/Subscriptions/Models/PokemonSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public PokemonSubscription()
MaximumLevel = 35;
Gender = "*";
Form = null;
//City = null;
Areas = new List<string>();
IVList = new List<string>();
}
Expand Down

0 comments on commit 7a0df3c

Please sign in to comment.