From 693b2a975dabacbfe8402b32cc40fdf505fa78f3 Mon Sep 17 00:00:00 2001 From: versx Date: Sun, 27 Dec 2020 19:16:13 -0800 Subject: [PATCH] Create "Free" role config option (#111) * Create "Free" role config option * Update config.example.json --- config.example.json | 2 ++ src/Commands/Feeds.cs | 45 ++++++------------------ src/Commands/Notifications.cs | 10 +----- src/Configuration/DiscordServerConfig.cs | 3 ++ 4 files changed, 16 insertions(+), 44 deletions(-) diff --git a/config.example.json b/config.example.json index 8b1f48ef..193178da 100644 --- a/config.example.json +++ b/config.example.json @@ -10,6 +10,7 @@ "emojiGuildId": 000000000000000001, "ownerId": 000000000000000000, "donorRoleIds": [], + "freeRoleName": "", "moderatorRoleIds": [], "token": "", "alarms": "alarms.json", @@ -96,6 +97,7 @@ "emojiGuildId": 000000000000000001, "ownerId": 000000000000000000, "donorRoleIds": [], + "freeRoleName": "", "moderatorRoleIds": [], "token": "", "alarms": "alarms2.json", diff --git a/src/Commands/Feeds.cs b/src/Commands/Feeds.cs index e089da13..80e9cbee 100644 --- a/src/Commands/Feeds.cs +++ b/src/Commands/Feeds.cs @@ -77,13 +77,13 @@ public async Task FeedMeAsync(CommandContext ctx, return; var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x)); - - var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _dep.WhConfig); if (!_dep.WhConfig.Servers.ContainsKey(guildId)) return; var server = _dep.WhConfig.Servers[guildId]; - if (server.CitiesRequireSupporterRole && !isSupporter) + var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _dep.WhConfig); + var isFreeRole = string.IsNullOrEmpty(server.FreeRoleName) ? false : string.Compare(cityName, server.FreeRoleName, true) == 0; + if (server.CitiesRequireSupporterRole && !isSupporter && !isFreeRole) { await ctx.DonateUnlockFeaturesMessage(); return; @@ -105,7 +105,7 @@ public async Task FeedMeAsync(CommandContext ctx, var cityRoles = server.CityRoles.Select(x => x.ToLower()); foreach (var city in cityNames) { - if (!cityRoles.Contains(city.ToLower())) + if (!isFreeRole && !cityRoles.Contains(city.ToLower())) { await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TYPE_COMMAND").FormatText(ctx.User.Username, city, server.CommandPrefix), DiscordColor.Red); continue; @@ -128,20 +128,6 @@ public async Task FeedMeAsync(CommandContext ctx, alreadyAssigned.Add(cityRole.Name); } - var cityRaidRole = ctx.Guild.GetRoleFromName($"{city}Raids"); - if (cityRaidRole != null) - { - result = await AddFeedRole(ctx.Member, cityRaidRole); - if (result) - { - assigned.Add(cityRaidRole.Name); - } - else - { - alreadyAssigned.Add(cityRaidRole.Name); - } - } - Thread.Sleep(200); } @@ -178,9 +164,13 @@ public async Task FeedMeNotAsync(CommandContext ctx, return; var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x)); + if (!_dep.WhConfig.Servers.ContainsKey(guildId)) + return; + var server = _dep.WhConfig.Servers[guildId]; var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _dep.WhConfig); - if (_dep.WhConfig.Servers[guildId].CitiesRequireSupporterRole && !isSupporter) + var isFreeRole = string.IsNullOrEmpty(server.FreeRoleName) ? false : string.Compare(cityName, server.FreeRoleName, true) == 0; + if (server.CitiesRequireSupporterRole && !isSupporter && !isFreeRole) { await ctx.DonateUnlockFeaturesMessage(); return; @@ -193,17 +183,15 @@ public async Task FeedMeNotAsync(CommandContext ctx, return; } - var server = _dep.WhConfig.Servers[guildId]; var unassigned = new List(); var alreadyUnassigned = new List(); try { var cityNames = cityName.RemoveSpaces(); - var cityRoles = server.CityRoles; foreach (var city in cityNames) { - if (!cityRoles.Exists(x => string.Compare(city, x, true) == 0)) + if (!isFreeRole && !server.CityRoles.Exists(x => string.Compare(city, x, true) == 0)) { await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TYPE_COMMAND").FormatText(ctx.User.Username, city, server.CommandPrefix), DiscordColor.Red); continue; @@ -225,19 +213,6 @@ public async Task FeedMeNotAsync(CommandContext ctx, alreadyUnassigned.Add(cityRole.Name); } - var cityRaidRole = ctx.Guild.GetRoleFromName($"{city}Raids"); - if (cityRaidRole == null) - continue; - - if (await RemoveFeedRole(ctx.Member, cityRaidRole)) - { - unassigned.Add(cityRaidRole.Name); - } - else - { - alreadyUnassigned.Add(cityRaidRole.Name); - } - Thread.Sleep(200); } diff --git a/src/Commands/Notifications.cs b/src/Commands/Notifications.cs index 4b37e372..4cb1fb2e 100644 --- a/src/Commands/Notifications.cs +++ b/src/Commands/Notifications.cs @@ -353,7 +353,6 @@ public async Task PokeMeAsync(CommandContext ctx, // If so, make sure they specified at least 90% or higher if (realIV < 90) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_INVALID_MINIMUM_IV").FormatText(ctx.User.Username), DiscordColor.Red); return; } @@ -388,7 +387,6 @@ public async Task PokeMeAsync(CommandContext ctx, { if (!MasterFile.Instance.Pokedex.ContainsKey(pokemonId)) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_INVALID_POKEMON_ID").FormatText(ctx.User.Username, pokemonId), DiscordColor.Red); continue; } @@ -399,7 +397,6 @@ public async Task PokeMeAsync(CommandContext ctx, // Check if common type pokemon e.g. Pidgey, Ratatta, Spinarak 'they are beneath him and he refuses to discuss them further' if (pokemonId.IsCommonPokemon() && realIV < Strings.CommonTypeMinimumIV && !isModOrHigher) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_COMMON_TYPE_POKEMON").FormatText(ctx.User.Username, pokemon.Name, Strings.CommonTypeMinimumIV), DiscordColor.Red); continue; } @@ -1248,14 +1245,12 @@ public async Task PvpMeAsync(CommandContext ctx, //You may only subscribe to the top 100 or higher rank. if (minimumRank < Strings.MinimumRank || minimumRank > Strings.MaximumRank) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_INVALID_PVP_RANK_RANGE").FormatText(ctx.User.Username, minimumRank), DiscordColor.Red); return; } if (minimumPercent < Strings.MinimumPercent || minimumPercent > Strings.MaximumPercent) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_INVALID_PVP_RANK_RANGE").FormatText(ctx.User.Username, minimumPercent), DiscordColor.Red); return; } @@ -1283,7 +1278,6 @@ public async Task PvpMeAsync(CommandContext ctx, { if (!MasterFile.Instance.Pokedex.ContainsKey(pokemonId)) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_INVALID_POKEMON_ID").FormatText(ctx.User.Username, pokemonId), DiscordColor.Red); continue; } @@ -1381,7 +1375,6 @@ public async Task PvpMeNotAsync(CommandContext ctx, var subscription = _dep.SubscriptionProcessor.Manager.GetUserSubscriptions(guildId, ctx.User.Id); if (subscription == null || subscription?.PvP?.Count == 0) { - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_NO_POKEMON_SUBSCRIPTIONS").FormatText(ctx.User.Username), DiscordColor.Red); return; } @@ -1411,7 +1404,6 @@ public async Task PvpMeNotAsync(CommandContext ctx, .ToList()? .ForEach(x => x.Id.Remove()); - await ctx.TriggerTypingAsync(); await ctx.RespondEmbed(Translator.Instance.Translate("NOTIFY_SUCCESS_REMOVE_ALL_PVP_SUBSCRIPTIONS").FormatText(ctx.User.Username, pvpLeague)); _dep.SubscriptionProcessor.Manager.ReloadSubscriptions(); return; @@ -2603,7 +2595,7 @@ public async Task ImportAsync(CommandContext ctx) var oldSubscription = _dep.SubscriptionProcessor.Manager.GetUserSubscriptions(guildId, ctx.User.Id); if (oldSubscription != null) { - var result = Data.Subscriptions.SubscriptionManager.RemoveAllUserSubscriptions(guildId, ctx.User.Id); + var result = SubscriptionManager.RemoveAllUserSubscriptions(guildId, ctx.User.Id); if (!result) { _logger.Error($"Failed to clear old user subscriptions for {ctx.User.Username} ({ctx.User.Id}) in guild {ctx.Guild?.Name} ({ctx.Guild?.Id}) before importing."); diff --git a/src/Configuration/DiscordServerConfig.cs b/src/Configuration/DiscordServerConfig.cs index b37fc18a..b7331193 100644 --- a/src/Configuration/DiscordServerConfig.cs +++ b/src/Configuration/DiscordServerConfig.cs @@ -43,6 +43,9 @@ public class DiscordServerConfig [JsonProperty("donorRoleIds")] public List DonorRoleIds { get; set; } + [JsonProperty("freeRoleName")] + public string FreeRoleName { get; set; } + /// /// Gets or sets the moderators of the Discord server ///