From e63f056c38f661f14e6dbc32d23ff6925d7ad27b Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Thu, 15 Feb 2024 21:10:09 +0100 Subject: [PATCH] add descriptions to roles that can set vanity ids --- .../Discord/DiscordBot.cs | 47 ++++++++++++++----- .../Discord/DiscordBotServices.cs | 2 +- .../Discord/MareWizardModule.Vanity.cs | 4 +- .../Utils/ServicesConfiguration.cs | 4 +- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs index 5b8480d..4c7c289 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs @@ -11,6 +11,7 @@ using MareSynchronosShared.Services; using MareSynchronosShared.Utils; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using StackExchange.Redis; using System.Text; using System.Threading.Channels; @@ -54,6 +55,9 @@ internal class DiscordBot : IHostedService var token = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.DiscordBotToken), string.Empty); if (!string.IsNullOrEmpty(token)) { + _logger.LogInformation("Starting DiscordBot"); + _logger.LogInformation("Using Configuration: " + _configurationService.ToString()); + _interactionModule = new InteractionService(_discordClient); _interactionModule.Log += Log; await _interactionModule.AddModuleAsync(typeof(MareModule), _services).ConfigureAwait(false); @@ -213,18 +217,28 @@ internal class DiscordBot : IHostedService { while (!_updateStatusCts.IsCancellationRequested) { - var vanityRoles = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), Array.Empty()); - if (vanityRoles.Length != _botServices.VanityRoles.Count) + try { - _botServices.VanityRoles.Clear(); - foreach (var role in vanityRoles) + _logger.LogInformation("Updating Vanity Roles"); + Dictionary vanityRoles = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), new Dictionary()); + if (vanityRoles.Keys.Count != _botServices.VanityRoles.Count) { - var restrole = guild.GetRole(role); - if (restrole != null) _botServices.VanityRoles.Add(restrole); - } - } + _botServices.VanityRoles.Clear(); + foreach (var role in vanityRoles) + { + _logger.LogInformation("Adding Role: {id} => {desc}", role.Key, role.Value); - await Task.Delay(TimeSpan.FromSeconds(30), _updateStatusCts.Token).ConfigureAwait(false); + var restrole = guild.GetRole(role.Key); + if (restrole != null) _botServices.VanityRoles.Add(restrole, role.Value); + } + } + + await Task.Delay(TimeSpan.FromSeconds(30), _updateStatusCts.Token).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Error during UpdateVanityRoles"); + } } } @@ -270,7 +284,14 @@ internal class DiscordBot : IHostedService if (prevMessage == null) { var msg = await channel.SendMessageAsync(embed: eb.Build(), components: cb.Build()).ConfigureAwait(false); - await msg.PinAsync().ConfigureAwait(false); + try + { + await msg.PinAsync().ConfigureAwait(false); + } + catch (Exception) + { + // swallow + } } else { @@ -405,7 +426,7 @@ internal class DiscordBot : IHostedService _logger.LogInformation("Getting application commands from guild {guildName}", guild.Name); var restGuild = await _discordClient.Rest.GetGuildAsync(guild.Id); - ulong[] allowedRoleIds = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), Array.Empty()); + Dictionary allowedRoleIds = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), new Dictionary()); _logger.LogInformation($"Allowed role ids: {string.Join(", ", allowedRoleIds)}"); if (allowedRoleIds.Any()) @@ -423,7 +444,7 @@ internal class DiscordBot : IHostedService var discordUser = await restGuild.GetUserAsync(lodestoneAuth.DiscordId).ConfigureAwait(false); _logger.LogInformation($"Checking User: {lodestoneAuth.DiscordId}, {lodestoneAuth.User.UID} ({lodestoneAuth.User.Alias}), User in Roles: {string.Join(", ", discordUser?.RoleIds ?? new List())}"); - if (discordUser == null || !discordUser.RoleIds.Any(u => allowedRoleIds.Contains(u))) + if (discordUser == null || !discordUser.RoleIds.Any(u => allowedRoleIds.Keys.Contains(u))) { _logger.LogInformation($"User {lodestoneAuth.User.UID} not in allowed roles, deleting alias"); lodestoneAuth.User.Alias = null; @@ -453,7 +474,7 @@ internal class DiscordBot : IHostedService _logger.LogInformation($"Checking Group: {group.GID}, owned by {lodestoneUser?.User?.UID ?? string.Empty} ({lodestoneUser?.User?.Alias ?? string.Empty}), User in Roles: {string.Join(", ", discordUser?.RoleIds ?? new List())}"); - if (lodestoneUser == null || discordUser == null || !discordUser.RoleIds.Any(u => allowedRoleIds.Contains(u))) + if (lodestoneUser == null || discordUser == null || !discordUser.RoleIds.Any(u => allowedRoleIds.Keys.Contains(u))) { _logger.LogInformation($"User {lodestoneUser.User.UID} not in allowed roles, deleting group alias"); group.Alias = null; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs index eab4abf..0e744dc 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs @@ -13,7 +13,7 @@ public class DiscordBotServices public ConcurrentDictionary LastVanityChange = new(); public ConcurrentDictionary LastVanityGidChange = new(); public ConcurrentDictionary ValidInteractions { get; } = new(); - public List VanityRoles { get; set; } = new(); + public Dictionary VanityRoles { get; set; } = new(); private readonly IServiceProvider _serviceProvider; private CancellationTokenSource verificationTaskCts; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Vanity.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Vanity.cs index a27139d..53d7826 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Vanity.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Vanity.cs @@ -17,13 +17,13 @@ public partial class MareWizardModule StringBuilder sb = new(); var user = await Context.Guild.GetUserAsync(Context.User.Id).ConfigureAwait(false); - bool userIsInVanityRole = _botServices.VanityRoles.Exists(u => user.RoleIds.Contains(u.Id)) || !_botServices.VanityRoles.Any(); + bool userIsInVanityRole = _botServices.VanityRoles.Keys.Any(u => user.RoleIds.Contains(u.Id)) || !_botServices.VanityRoles.Any(); if (!userIsInVanityRole) { sb.AppendLine("To be able to set Vanity IDs you must have one of the following roles:"); foreach (var role in _botServices.VanityRoles) { - sb.Append("- ").AppendLine(role.Mention); + sb.Append("- ").Append(role.Key.Mention).Append(" (").Append(role.Value).AppendLine(")"); } } else diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs index 72d0aac..448cb76 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs @@ -9,7 +9,7 @@ public class ServicesConfiguration : MareConfigurationBase public ulong? DiscordChannelForReports { get; set; } = null; public ulong? DiscordChannelForCommands { get; set; } = null; public Uri MainServerGrpcAddress { get; set; } = null; - public ulong[]? VanityRoles { get; set; } = null; + public Dictionary VanityRoles { get; set; } = new Dictionary(); public override string ToString() { @@ -22,7 +22,7 @@ public class ServicesConfiguration : MareConfigurationBase sb.AppendLine($"{nameof(DiscordChannelForCommands)} => {DiscordChannelForCommands}"); foreach (var role in VanityRoles) { - sb.AppendLine($"{nameof(VanityRoles)} => {role}"); + sb.AppendLine($"{nameof(VanityRoles)} => {role.Key} = {role.Value}"); } return sb.ToString(); }