Skip to content

Commit 52ac4c7

Browse files
committed
feat: Add admin user option
1 parent 1ddcad7 commit 52ac4c7

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

DockerDiscordBot/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
3838
.AddOptions<ApplicationSettings>()
3939
.Bind(_configuration.GetSection("ApplicationSettings"))
4040
.Validate(x =>
41-
!string.IsNullOrWhiteSpace(x.DiscordToken),
41+
!string.IsNullOrWhiteSpace(x.DiscordToken),
4242
"Discord token is required.")
4343
.Validate(x =>
44-
!string.IsNullOrWhiteSpace(x.DockerHost),
44+
!string.IsNullOrWhiteSpace(x.DockerHost),
4545
"Docker host is required.")
4646
.ValidateOnStart();
4747

DockerDiscordBot/Services/DiscordService.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class DiscordService : IDiscordService
1414
private readonly IMediator _bus;
1515
private readonly DiscordSocketClient _client;
1616
private readonly ILogger<DiscordService> _logger;
17-
private readonly string _token;
17+
private readonly ApplicationSettings _options;
1818

1919
public DiscordService(
2020
DiscordSocketClient client,
@@ -24,7 +24,7 @@ public DiscordService(
2424
_client = client;
2525
_logger = logger;
2626
_bus = bus;
27-
_token = options.Value.DiscordToken;
27+
_options = options.Value;
2828
}
2929

3030
public async Task StartAsync()
@@ -33,7 +33,7 @@ public async Task StartAsync()
3333
_client.Ready += ReadyAsync;
3434
_client.MessageReceived += MessageReceivedAsync;
3535

36-
await _client.LoginAsync(TokenType.Bot, _token);
36+
await _client.LoginAsync(TokenType.Bot, _options.DiscordToken);
3737
await _client.StartAsync();
3838
}
3939

@@ -50,12 +50,19 @@ private async Task MessageReceivedAsync(SocketMessage message)
5050
{
5151
return;
5252
}
53-
53+
5454
var command = message.GetCommand();
5555

5656
if (command is null)
5757
{
58-
_logger.LogWarning("Unhandled command: {Command}", message.Content);
58+
_logger.LogDebug("Command not found: {Message}", message.Content);
59+
return;
60+
}
61+
62+
if (!string.IsNullOrWhiteSpace(_options.AdminUser) && message.Author.Username != _options.AdminUser)
63+
{
64+
_logger.LogDebug("Unauthorized user: {User}", message.Author.Username);
65+
await message.Channel.SendMessageAsync("You are not authorized to use this bot.");
5966
return;
6067
}
6168

DockerDiscordBot/Settings/ApplicationSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public sealed class ApplicationSettings
44
{
55
public required string DiscordToken { get; init; }
66
public required string DockerHost { get; init; }
7+
public string AdminUser { get; init; } = string.Empty;
78
}

DockerDiscordBot/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"ApplicationSettings": {
1717
"DiscordToken": "",
18-
"DockerHost": "unix:///var/run/docker.sock"
18+
"DockerHost": "unix:///var/run/docker.sock",
19+
"AdminUser": ""
1920
}
2021
}

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
An easy-to-use Discord bot to manage your Docker containers.
77

8+
## Table of Contents
9+
10+
- [Commands](#commands)
11+
- [Installation](#installation)
12+
- [Discord Bot Setup](#discord-bot-setup)
13+
- [Docker Setup](#docker-setup)
14+
- [Configuration](#configuration)
15+
- [License](#license)
16+
817
## Commands
918

1019
| Command | Description |
@@ -59,6 +68,17 @@ services:
5968
- /var/run/docker.sock:/var/run/docker.sock
6069
```
6170
71+
### Configuration
72+
73+
The bot can be configured using environment variables:
74+
75+
| Environment Variable | Description | Default Value | Required |
76+
| --- | --- | --- | --- |
77+
| `ApplicationSettings__DiscordToken` | Discord bot token | `null` | ✅ |
78+
| `ApplicationSettings__AdminUser` | Discord admin username | `null` | ✅ |
79+
| `ApplicationSettings__CommandPrefix` | Command prefix | `!` | ❌ |
80+
| `ApplicationSettings__EmbedColor` | Embed color | `#7289DA` | ❌ |
81+
6282
## License
6383

6484
© [Alexander Konietzko](https://alexanderkonietzko.com) 2024

0 commit comments

Comments
 (0)