From 1f3283b853967ba762445a71b0dc331a7b23b63f Mon Sep 17 00:00:00 2001 From: Noi Date: Wed, 15 Dec 2021 16:57:18 -0800 Subject: [PATCH] Add PendingAutoRole --- .../Module/PendingAutoRole/ModuleConfig.cs | 18 ++++ .../Module/PendingAutoRole/PendingAutoRole.cs | 82 +++++++++++++++++++ RegexBot/RegexBot.cs | 1 + RegexBot/RegexBot.csproj | 2 +- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 RegexBot/Module/PendingAutoRole/ModuleConfig.cs create mode 100644 RegexBot/Module/PendingAutoRole/PendingAutoRole.cs diff --git a/RegexBot/Module/PendingAutoRole/ModuleConfig.cs b/RegexBot/Module/PendingAutoRole/ModuleConfig.cs new file mode 100644 index 0000000..f0e686f --- /dev/null +++ b/RegexBot/Module/PendingAutoRole/ModuleConfig.cs @@ -0,0 +1,18 @@ +using Newtonsoft.Json.Linq; +using Noikoio.RegexBot.ConfigItem; + +namespace Noikoio.RegexBot.Module.PendingAutoRole +{ + class ModuleConfig + { + public EntityName Role { get; } + + public ModuleConfig(JObject conf) + { + var cfgRole = conf["Role"]?.Value(); + if (string.IsNullOrWhiteSpace(cfgRole)) + throw new RuleImportException("Role was not specified."); + Role = new EntityName(cfgRole, EntityType.Role); + } + } +} diff --git a/RegexBot/Module/PendingAutoRole/PendingAutoRole.cs b/RegexBot/Module/PendingAutoRole/PendingAutoRole.cs new file mode 100644 index 0000000..58c9336 --- /dev/null +++ b/RegexBot/Module/PendingAutoRole/PendingAutoRole.cs @@ -0,0 +1,82 @@ +using Discord.WebSocket; +using Newtonsoft.Json.Linq; +using Noikoio.RegexBot.ConfigItem; +using System.Linq; +using System.Threading.Tasks; + +namespace Noikoio.RegexBot.Module.PendingAutoRole { + /// + /// Automatically sets a specified role when a user is no longer in pending status + /// + class PendingAutoRole : BotModule + { + // Config: + // Role: string - Name or ID of the role to apply. Takes EntityName format. + // WaitTime: number - Amount of time in seconds to wait until applying the role to a new user. + public PendingAutoRole(DiscordSocketClient client) : base(client) + { + client.GuildAvailable += Client_GuildAvailable; + client.GuildMemberUpdated += Client_GuildMemberUpdated; + } + + private async Task Client_GuildAvailable(SocketGuild arg) { + var conf = GetState(arg.Id); + if (conf == null) return; + var trole = GetRole(arg); + if (trole == null) { + await Log("Unable to enumerate. WAs the role renamed or deleted?"); + return; + } + + foreach (var user in arg.Users.Where(u => u.IsPending.HasValue && u.IsPending.Value == false)) { + if (user.Roles.Contains(trole)) continue; + await user.AddRoleAsync(trole); + } + } + + private async Task Client_GuildMemberUpdated(SocketGuildUser previous, SocketGuildUser current) { + var conf = GetState(current.Guild.Id); + if (conf == null) return; + + if (!(previous.IsPending.HasValue && current.IsPending.HasValue)) return; + if (previous.IsPending == true && current.IsPending == false) { + var r = GetRole(current.Guild); + if (r == null) { + await Log($"Failed to update {current} - was the role renamed or deleted?"); + return; + } + await current.AddRoleAsync(r); + } + } + + public override Task CreateInstanceState(JToken configSection) + { + if (configSection == null) return Task.FromResult(null); + if (configSection.Type != JTokenType.Object) + { + throw new RuleImportException("Configuration for this section is invalid."); + } + return Task.FromResult(new ModuleConfig((JObject)configSection)); + } + + // can return null + private SocketRole GetRole(SocketGuild g) + { + var conf = GetState(g.Id); + if (conf == null) return null; + + if (conf.Role.Id.HasValue) + { + var result = g.GetRole(conf.Role.Id.Value); + if (result != null) return result; + } + else + { + foreach (var role in g.Roles) + if (string.Equals(conf.Role.Name, role.Name)) return role; + } + Log("Unable to find role in " + g.Name).Wait(); + return null; + } + } +} diff --git a/RegexBot/RegexBot.cs b/RegexBot/RegexBot.cs index 8dcea41..4c3b517 100644 --- a/RegexBot/RegexBot.cs +++ b/RegexBot/RegexBot.cs @@ -60,6 +60,7 @@ namespace Noikoio.RegexBot new Module.ModCommands.ModCommands(_client), new Module.AutoRespond.AutoRespond(_client), new Module.EntryAutoRole.EntryAutoRole(_client), + new Module.PendingAutoRole.PendingAutoRole(_client), new Module.VoiceRoleSync.VoiceRoleSync(_client), new Module.VoteTempChannel.VoteTempChannel(_client), diff --git a/RegexBot/RegexBot.csproj b/RegexBot/RegexBot.csproj index 2219e09..55fcf16 100644 --- a/RegexBot/RegexBot.csproj +++ b/RegexBot/RegexBot.csproj @@ -7,7 +7,7 @@ Highly configurable Discord moderation bot Noikoio - 2.6.6 + 2.6.7