Skip to content

Commit 348abd9

Browse files
committed
feat: Add command prefix setting
1 parent 52ac4c7 commit 348abd9

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
using Discord;
22
using DockerDiscordBot.Mappings;
3+
using DockerDiscordBot.Settings;
34
using MediatR;
45
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Options;
57

68
namespace DockerDiscordBot.Commands.Help;
79

810
public sealed class HelpCommandHandler : IRequestHandler<HelpCommand>
911
{
12+
private readonly string _commandPrefix;
1013
private readonly ILogger<HelpCommandHandler> _logger;
1114

12-
public HelpCommandHandler(ILogger<HelpCommandHandler> logger)
15+
public HelpCommandHandler(
16+
ILogger<HelpCommandHandler> logger,
17+
IOptions<ApplicationSettings> _options)
1318
{
1419
_logger = logger;
20+
_commandPrefix = _options.Value.CommandPrefix;
1521
}
1622

1723
public async Task Handle(HelpCommand request, CancellationToken cancellationToken)
1824
{
1925
_logger.LogInformation("Executing {Command}", nameof(HelpCommand));
20-
21-
var commands = CommandMapping.Commands.Keys.Select(key => $"`!{key}`").ToList();
22-
26+
27+
var commands = CommandMapping.Commands.Keys.Select(key => $"`{_commandPrefix}{key}`").ToList();
28+
2329
var embed = new EmbedBuilder()
2430
.WithTitle("Available Commands")
2531
.WithDescription(string.Join(", ", commands))
2632
.WithColor(Color.Blue)
2733
.Build();
28-
34+
2935
await request.Message.Channel.SendMessageAsync(embed: embed);
3036
}
3137
}

DockerDiscordBot/Extensions/CommandsExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ namespace DockerDiscordBot.Extensions;
66

77
public static class CommandsExtensions
88
{
9-
private static readonly string s_prefix = "!";
10-
11-
public static Command? GetCommand(this SocketMessage message)
9+
public static Command? GetCommand(this SocketMessage message, string prefix)
1210
{
13-
if (!message.Content.StartsWith(s_prefix))
11+
if (!message.Content.StartsWith(prefix))
1412
{
1513
return null;
1614
}

DockerDiscordBot/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
1112

1213
namespace DockerDiscordBot;
1314

@@ -38,11 +39,14 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
3839
.AddOptions<ApplicationSettings>()
3940
.Bind(_configuration.GetSection("ApplicationSettings"))
4041
.Validate(x =>
41-
!string.IsNullOrWhiteSpace(x.DiscordToken),
42+
!string.IsNullOrWhiteSpace(x.DiscordToken),
4243
"Discord token is required.")
4344
.Validate(x =>
44-
!string.IsNullOrWhiteSpace(x.DockerHost),
45+
!string.IsNullOrWhiteSpace(x.DockerHost),
4546
"Docker host is required.")
47+
.Validate(x =>
48+
!string.IsNullOrWhiteSpace(x.CommandPrefix),
49+
"Command prefix is required.")
4650
.ValidateOnStart();
4751

4852
services.AddMediatR(config =>
@@ -81,6 +85,10 @@ private static async Task RunAsync()
8185
var logger = _services.GetRequiredService<ILogger<Program>>();
8286
logger.LogInformation("Bot starting...");
8387

88+
var options = _services.GetRequiredService<IOptions<ApplicationSettings>>().Value;
89+
logger.LogInformation("Command prefix: {Prefix}", options.CommandPrefix);
90+
logger.LogInformation("Admin user: {User}", options.AdminUser);
91+
8492
var client = _services.GetRequiredService<IDiscordService>();
8593
await client.StartAsync();
8694

DockerDiscordBot/Services/DiscordService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ private async Task MessageReceivedAsync(SocketMessage message)
5050
{
5151
return;
5252
}
53-
54-
var command = message.GetCommand();
53+
54+
var command = message.GetCommand(_options.CommandPrefix);
5555

5656
if (command is null)
5757
{
5858
_logger.LogDebug("Command not found: {Message}", message.Content);
5959
return;
6060
}
61-
61+
6262
if (!string.IsNullOrWhiteSpace(_options.AdminUser) && message.Author.Username != _options.AdminUser)
6363
{
6464
_logger.LogDebug("Unauthorized user: {User}", message.Author.Username);

DockerDiscordBot/Settings/ApplicationSettings.cs

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

DockerDiscordBot/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ApplicationSettings": {
1717
"DiscordToken": "",
1818
"DockerHost": "unix:///var/run/docker.sock",
19-
"AdminUser": ""
19+
"AdminUser": "",
20+
"CommandPrefix": "!"
2021
}
2122
}

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ The bot can be configured using environment variables:
7777
| `ApplicationSettings__DiscordToken` | Discord bot token | `null` | ✅ |
7878
| `ApplicationSettings__AdminUser` | Discord admin username | `null` | ✅ |
7979
| `ApplicationSettings__CommandPrefix` | Command prefix | `!` | ❌ |
80-
| `ApplicationSettings__EmbedColor` | Embed color | `#7289DA` | ❌ |
8180

8281
## License
8382

0 commit comments

Comments
 (0)