add descriptions to roles that can set vanity ids

This commit is contained in:
rootdarkarchon
2024-02-15 21:10:09 +01:00
parent bd128e832f
commit e63f056c38
4 changed files with 39 additions and 18 deletions

View File

@@ -11,6 +11,7 @@ using MareSynchronosShared.Services;
using MareSynchronosShared.Utils; using MareSynchronosShared.Utils;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using StackExchange.Redis; using StackExchange.Redis;
using System.Text; using System.Text;
using System.Threading.Channels; using System.Threading.Channels;
@@ -54,6 +55,9 @@ internal class DiscordBot : IHostedService
var token = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.DiscordBotToken), string.Empty); var token = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.DiscordBotToken), string.Empty);
if (!string.IsNullOrEmpty(token)) if (!string.IsNullOrEmpty(token))
{ {
_logger.LogInformation("Starting DiscordBot");
_logger.LogInformation("Using Configuration: " + _configurationService.ToString());
_interactionModule = new InteractionService(_discordClient); _interactionModule = new InteractionService(_discordClient);
_interactionModule.Log += Log; _interactionModule.Log += Log;
await _interactionModule.AddModuleAsync(typeof(MareModule), _services).ConfigureAwait(false); await _interactionModule.AddModuleAsync(typeof(MareModule), _services).ConfigureAwait(false);
@@ -213,18 +217,28 @@ internal class DiscordBot : IHostedService
{ {
while (!_updateStatusCts.IsCancellationRequested) while (!_updateStatusCts.IsCancellationRequested)
{ {
var vanityRoles = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), Array.Empty<ulong>()); try
if (vanityRoles.Length != _botServices.VanityRoles.Count)
{ {
_botServices.VanityRoles.Clear(); _logger.LogInformation("Updating Vanity Roles");
foreach (var role in vanityRoles) Dictionary<ulong, string> vanityRoles = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), new Dictionary<ulong, string>());
if (vanityRoles.Keys.Count != _botServices.VanityRoles.Count)
{ {
var restrole = guild.GetRole(role); _botServices.VanityRoles.Clear();
if (restrole != null) _botServices.VanityRoles.Add(restrole); 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) if (prevMessage == null)
{ {
var msg = await channel.SendMessageAsync(embed: eb.Build(), components: cb.Build()).ConfigureAwait(false); 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 else
{ {
@@ -405,7 +426,7 @@ internal class DiscordBot : IHostedService
_logger.LogInformation("Getting application commands from guild {guildName}", guild.Name); _logger.LogInformation("Getting application commands from guild {guildName}", guild.Name);
var restGuild = await _discordClient.Rest.GetGuildAsync(guild.Id); var restGuild = await _discordClient.Rest.GetGuildAsync(guild.Id);
ulong[] allowedRoleIds = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), Array.Empty<ulong>()); Dictionary<ulong, string> allowedRoleIds = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.VanityRoles), new Dictionary<ulong, string>());
_logger.LogInformation($"Allowed role ids: {string.Join(", ", allowedRoleIds)}"); _logger.LogInformation($"Allowed role ids: {string.Join(", ", allowedRoleIds)}");
if (allowedRoleIds.Any()) if (allowedRoleIds.Any())
@@ -423,7 +444,7 @@ internal class DiscordBot : IHostedService
var discordUser = await restGuild.GetUserAsync(lodestoneAuth.DiscordId).ConfigureAwait(false); 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<ulong>())}"); _logger.LogInformation($"Checking User: {lodestoneAuth.DiscordId}, {lodestoneAuth.User.UID} ({lodestoneAuth.User.Alias}), User in Roles: {string.Join(", ", discordUser?.RoleIds ?? new List<ulong>())}");
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"); _logger.LogInformation($"User {lodestoneAuth.User.UID} not in allowed roles, deleting alias");
lodestoneAuth.User.Alias = null; 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<ulong>())}"); _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<ulong>())}");
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"); _logger.LogInformation($"User {lodestoneUser.User.UID} not in allowed roles, deleting group alias");
group.Alias = null; group.Alias = null;

View File

@@ -13,7 +13,7 @@ public class DiscordBotServices
public ConcurrentDictionary<ulong, DateTime> LastVanityChange = new(); public ConcurrentDictionary<ulong, DateTime> LastVanityChange = new();
public ConcurrentDictionary<string, DateTime> LastVanityGidChange = new(); public ConcurrentDictionary<string, DateTime> LastVanityGidChange = new();
public ConcurrentDictionary<ulong, ulong> ValidInteractions { get; } = new(); public ConcurrentDictionary<ulong, ulong> ValidInteractions { get; } = new();
public List<RestRole> VanityRoles { get; set; } = new(); public Dictionary<RestRole, string> VanityRoles { get; set; } = new();
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private CancellationTokenSource verificationTaskCts; private CancellationTokenSource verificationTaskCts;

View File

@@ -17,13 +17,13 @@ public partial class MareWizardModule
StringBuilder sb = new(); StringBuilder sb = new();
var user = await Context.Guild.GetUserAsync(Context.User.Id).ConfigureAwait(false); 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) if (!userIsInVanityRole)
{ {
sb.AppendLine("To be able to set Vanity IDs you must have one of the following roles:"); sb.AppendLine("To be able to set Vanity IDs you must have one of the following roles:");
foreach (var role in _botServices.VanityRoles) foreach (var role in _botServices.VanityRoles)
{ {
sb.Append("- ").AppendLine(role.Mention); sb.Append("- ").Append(role.Key.Mention).Append(" (").Append(role.Value).AppendLine(")");
} }
} }
else else

View File

@@ -9,7 +9,7 @@ public class ServicesConfiguration : MareConfigurationBase
public ulong? DiscordChannelForReports { get; set; } = null; public ulong? DiscordChannelForReports { get; set; } = null;
public ulong? DiscordChannelForCommands { get; set; } = null; public ulong? DiscordChannelForCommands { get; set; } = null;
public Uri MainServerGrpcAddress { get; set; } = null; public Uri MainServerGrpcAddress { get; set; } = null;
public ulong[]? VanityRoles { get; set; } = null; public Dictionary<ulong, string> VanityRoles { get; set; } = new Dictionary<ulong, string>();
public override string ToString() public override string ToString()
{ {
@@ -22,7 +22,7 @@ public class ServicesConfiguration : MareConfigurationBase
sb.AppendLine($"{nameof(DiscordChannelForCommands)} => {DiscordChannelForCommands}"); sb.AppendLine($"{nameof(DiscordChannelForCommands)} => {DiscordChannelForCommands}");
foreach (var role in VanityRoles) foreach (var role in VanityRoles)
{ {
sb.AppendLine($"{nameof(VanityRoles)} => {role}"); sb.AppendLine($"{nameof(VanityRoles)} => {role.Key} = {role.Value}");
} }
return sb.ToString(); return sb.ToString();
} }