Sets up guild role checking

This commit is contained in:
benbierens 2023-10-22 10:38:46 +02:00
parent 4aa4731480
commit e16b1ce079
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
14 changed files with 60 additions and 35 deletions

View File

@ -0,0 +1,35 @@
using Discord.WebSocket;
namespace BiblioTech
{
public class AdminChecker
{
private SocketGuild guild = null!;
private ulong[] adminIds = Array.Empty<ulong>();
private DateTime lastUpdate = DateTime.MinValue;
public void SetGuild(SocketGuild guild)
{
this.guild = guild;
}
public bool IsUserAdmin(ulong userId)
{
if (ShouldUpdate()) UpdateAdminIds();
return adminIds.Contains(userId);
}
private bool ShouldUpdate()
{
return !adminIds.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10);
}
private void UpdateAdminIds()
{
lastUpdate = DateTime.UtcNow;
var adminRole = guild.Roles.Single(r => r.Name == Program.Config.AdminRoleName);
adminIds = adminRole.Members.Select(m => m.Id).ToArray();
}
}
}

View File

@ -1,6 +1,6 @@
using Discord.WebSocket;
using Discord;
using BiblioTech.TokenCommands;
using BiblioTech.Commands;
namespace BiblioTech
{
@ -37,7 +37,7 @@ namespace BiblioTech
protected bool IsSenderAdmin(SocketSlashCommand command)
{
return Program.AdminChecker.IsUserAdmin(command.User.Id);
}
protected ulong GetUserId(UserOption userOption, SocketSlashCommand command)

View File

@ -22,6 +22,7 @@ namespace BiblioTech
private async Task Client_Ready()
{
var guild = client.Guilds.Single(g => g.Name == Program.Config.ServerName);
Program.AdminChecker.SetGuild(guild);
var builders = commands.Select(c =>
{

View File

@ -1,6 +1,6 @@
using Discord.WebSocket;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class ClearUserAssociationCommand : BaseCommand
{

View File

@ -1,6 +1,6 @@
using Discord.WebSocket;
namespace BiblioTech
namespace BiblioTech.Commands
{
public class DeploymentsCommand : BaseCommand
{

View File

@ -1,7 +1,7 @@
using Discord.WebSocket;
using GethPlugin;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class EthAddressOption : CommandOption
{

View File

@ -3,13 +3,13 @@ using Core;
using Discord.WebSocket;
using GethPlugin;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class GetBalanceCommand : BaseNetCommand
{
private readonly UserAssociateCommand userAssociateCommand;
private readonly UserOption optionalUser = new UserOption(
description: "If set, get balance for another user. (Optional, admin-only)",
description: "If set, get balance for another user. (Optional, admin-only)",
isRequired: false);
public GetBalanceCommand(DeploymentsFilesMonitor monitor, CoreInterface ci, UserAssociateCommand userAssociateCommand)

View File

@ -3,7 +3,7 @@ using Core;
using Discord.WebSocket;
using GethPlugin;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class MintCommand : BaseNetCommand
{

View File

@ -1,6 +1,6 @@
using Discord.WebSocket;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class ReportHistoryCommand : BaseCommand
{

View File

@ -1,10 +1,13 @@
using Discord.WebSocket;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class UserAssociateCommand : BaseCommand
{
private readonly EthAddressOption ethOption = new EthAddressOption();
private readonly UserOption optionalUser = new UserOption(
description: "If set, associates Ethereum address for another user. (Optional, admin-only)",
isRequired: false);
public override string Name => "set";
public override string StartingMessage => "hold on...";
@ -15,12 +18,12 @@ namespace BiblioTech.TokenCommands
protected override async Task Invoke(SocketSlashCommand command)
{
var userId = command.User.Id;
var userId = GetUserId(optionalUser, command);
var data = await ethOption.Parse(command);
if (data == null) return;
var currentAddress = Program.UserRepo.GetCurrentAddressForUser(userId);
if (currentAddress != null)
if (currentAddress != null && !IsSenderAdmin(command))
{
await command.FollowupAsync($"You've already set your Ethereum address to {currentAddress}.");
return;

View File

@ -1,7 +1,7 @@
using Discord;
using Discord.WebSocket;
namespace BiblioTech.TokenCommands
namespace BiblioTech.Commands
{
public class UserOption : CommandOption
{

View File

@ -10,10 +10,7 @@ namespace BiblioTech
public CodexDeployment[] GetDeployments()
{
if (ShouldUpdate())
{
UpdateDeployments();
}
if (ShouldUpdate()) UpdateDeployments();
return deployments;
}

View File

@ -1,5 +1,5 @@
using ArgsUniform;
using BiblioTech.TokenCommands;
using BiblioTech.Commands;
using Core;
using Discord;
using Discord.WebSocket;
@ -14,6 +14,7 @@ namespace BiblioTech
public static Configuration Config { get; private set; } = null!;
public static DeploymentsFilesMonitor DeploymentFilesMonitor { get; } = new DeploymentsFilesMonitor();
public static UserRepo UserRepo { get; } = new UserRepo();
public static AdminChecker AdminChecker { get; } = new AdminChecker();
public static Task Main(string[] args)
{
@ -43,9 +44,13 @@ namespace BiblioTech
var ci = entryPoint.CreateInterface();
var associateCommand = new UserAssociateCommand();
var handler = new CommandHandler(client,
new GetBalanceCommand(monitor, ci),
new ClearUserAssociationCommand(),
new GetBalanceCommand(monitor, ci, associateCommand),
new MintCommand(monitor, ci),
new ReportHistoryCommand(),
associateCommand,
new DeploymentsCommand(monitor)
);

View File

@ -1,16 +0,0 @@
using Discord.WebSocket;
namespace BiblioTech.TokenCommands
{
public class ShowIdCommand : BaseCommand
{
public override string Name => "my-id";
public override string StartingMessage => "...";
public override string Description => "Shows you your Discord ID. (Useful for admins)";
protected override async Task Invoke(SocketSlashCommand command)
{
await command.FollowupAsync("Your ID: " + command.User.Id);
}
}
}