Restrict admin commands to admin channel

This commit is contained in:
benbierens 2023-10-25 10:38:21 +02:00
parent 6e82d6b1e6
commit f148598a21
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
9 changed files with 40 additions and 6 deletions

View File

@ -14,6 +14,7 @@ namespace CodexDiscordBotPlugin
AddEnvVar("TOKEN", config.Token);
AddEnvVar("SERVERNAME", config.ServerName);
AddEnvVar("ADMINROLE", config.AdminRoleName);
AddEnvVar("ADMINCHANNELNAME", config.AdminChannelName);
}
}
}

View File

@ -1,20 +1,20 @@
using CodexPlugin;
namespace CodexDiscordBotPlugin
namespace CodexDiscordBotPlugin
{
public class DiscordBotStartupConfig
{
public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName)
public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName, string adminChannelName)
{
Name = name;
Token = token;
ServerName = serverName;
AdminRoleName = adminRoleName;
AdminChannelName = adminChannelName;
}
public string Name { get; }
public string Token { get; }
public string ServerName { get; }
public string AdminRoleName { get; }
public string AdminChannelName { get; }
}
}

View File

@ -20,6 +20,11 @@ namespace BiblioTech
return adminIds.Contains(userId);
}
public bool IsAdminChannel(ISocketMessageChannel channel)
{
return channel.Name == Program.Config.AdminChannelName;
}
private bool ShouldUpdate()
{
return !adminIds.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10);

View File

@ -39,6 +39,11 @@ namespace BiblioTech
return Program.AdminChecker.IsUserAdmin(command.User.Id);
}
protected bool IsInAdminChannel(SocketSlashCommand command)
{
return Program.AdminChecker.IsAdminChannel(command.Channel);
}
protected ulong GetUserId(UserOption userOption, CommandContext context)
{
var targetUser = userOption.GetOptionUserId(context);

View File

@ -1,5 +1,6 @@
using BiblioTech.Options;
using CodexPlugin;
using Discord.WebSocket;
namespace BiblioTech.Commands
{
@ -41,6 +42,12 @@ namespace BiblioTech.Commands
return;
}
if (!IsInAdminChannel(context.Command))
{
await context.Command.FollowupAsync("Please use admin commands only in the admin channel.");
return;
}
await clearCommand.CommandHandler(context);
await reportCommand.CommandHandler(context);
await deployListCommand.CommandHandler(context);

View File

@ -18,5 +18,8 @@ namespace BiblioTech
[Uniform("admin-role", "a", "ADMINROLE", true, "Name of the Discord server admin role")]
public string AdminRoleName { get; set; } = string.Empty;
[Uniform("admin-channel-name", "ac", "ADMINCHANNELNAME", true, "Name of the Discord server channel where admin commands are allowed.")]
public string AdminChannelName { get; set; } = "admin-channel";
}
}

View File

@ -113,6 +113,9 @@ namespace CodexNetDeployer
[Uniform("dbot-adminrolename", "dbotarn", "DBOTADMINROLENAME", false, "Required if discord-bot is true. Name of the Discord role which will have access to admin features.")]
public string DiscordBotAdminRoleName { get; set; } = string.Empty;
[Uniform("dbot-adminchannelname", "dbotacn", "DBOTADMINCHANNELNAME", false, "Required if discord-bot is true. Name of the Discord channel in which admin commands are allowed.")]
public string DiscordBotAdminChannelName { get; set; } = string.Empty;
public List<string> Validate()
{
var errors = new List<string>();
@ -147,6 +150,14 @@ namespace CodexNetDeployer
if (PublicGethDiscPort == 0) errors.Add("Geth public discovery port is not set.");
}
if (DeployDiscordBot)
{
StringIsSet(nameof(DiscordBotToken), DiscordBotToken, errors);
StringIsSet(nameof(DiscordBotServerName), DiscordBotServerName, errors);
StringIsSet(nameof(DiscordBotAdminRoleName), DiscordBotAdminRoleName, errors);
StringIsSet(nameof(DiscordBotAdminChannelName), DiscordBotAdminChannelName, errors);
}
return errors;
}

View File

@ -129,7 +129,8 @@ namespace CodexNetDeployer
name: "discordbot-" + config.DeploymentName,
token: config.DiscordBotToken,
serverName: config.DiscordBotServerName,
adminRoleName: config.DiscordBotAdminRoleName));
adminRoleName: config.DiscordBotAdminRoleName,
adminChannelName: config.DiscordBotAdminChannelName));
Log("Discord bot deployed.");
return rc;

View File

@ -25,4 +25,5 @@ dotnet run \
--discord-bot=1 \
--dbot-token=tokenhere \
--dbot-servername=namehere \
--dbot-adminrolename=alsonamehere
--dbot-adminrolename=alsonamehere \
--dbot-adminchannelname=channelname