Skip to content

Commit

Permalink
Merge pull request #12 from foldingcash/dev
Browse files Browse the repository at this point in the history
.NET Upgrade and some usability enhancements
  • Loading branch information
StrungSafe authored Jul 22, 2024
2 parents da44659 + 92779c6 commit 3839265
Show file tree
Hide file tree
Showing 22 changed files with 253 additions and 170 deletions.
7 changes: 3 additions & 4 deletions DiscordBot/DiscordBot.Console/DiscordBot.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Windsor" Version="5.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordBot.Core\DiscordBot.Core.csproj" />
Expand Down
16 changes: 9 additions & 7 deletions DiscordBot/DiscordBot.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ private static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureServices((context, services) =>
{
services.AddHostedService<Bot>();
services.AddSingleton<ICommandService, CommandProvider>();
services
.AddHostedService<Bot>()
.AddSingleton<ICommandService, CommandProvider>()
.Configure<BotSettings>(context.Configuration.GetSection("AppSettings"));

services.Configure<BotConfig>(context.Configuration.GetSection("AppSettings"));

services.AddSingleton<IFoldingBotModuleService, FoldingBotModuleProvider>();

services.Configure<FoldingBotConfig>(context.Configuration.GetSection("AppSettings"));
services
.AddSingleton<IFoldingBotModuleService, FoldingBotModuleProvider>()
.Configure<FoldingBotSettings>(context.Configuration.GetSection("AppSettings"))
.AddSingleton<IFoldingBotConfigurationService, FoldingBotConfigurationProvider>()
.AddSingleton<IBotConfigurationService>(provider => provider.GetRequiredService<IFoldingBotConfigurationService>());
});
}
}
Expand Down
4 changes: 3 additions & 1 deletion DiscordBot/DiscordBot.Console/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"AdminUser": "AdminUser",
"Token": "Token",

"ConfigurationPath": "./botConfiguration.json",

"HomeUrl": "http://folding.cash",
"FoldingAtHomeUrl": "http://folding.stanford.edu",

"FoldingApiUri": "https://fahstatsapi.azurewebsites.net",
"FoldingApiUri": "https://api.folding.cash",

"Guild": "FoldingCash",
"BotChannel": "general",
Expand Down
37 changes: 19 additions & 18 deletions DiscordBot/DiscordBot.Core/BaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@

internal class BaseModule : BotModule
{
private readonly IOptionsMonitor<BotConfig> botConfigMonitor;
private readonly IOptionsMonitor<BotSettings> botSettingsMonitor;

private readonly ICommandService commandService;

private readonly IBotConfigurationService botConfigurationService;
private readonly ILogger logger;

public BaseModule(ILogger<BaseModule> logger, IOptionsMonitor<BotConfig> botConfigMonitor,
ICommandService commandService)
: base(logger)
public BaseModule(ILogger<BaseModule> logger, IOptionsMonitor<BotSettings> botSettingsMonitor,
ICommandService commandService, IBotConfigurationService botConfigurationService)
: base(logger, botConfigurationService)
{
this.logger = logger;
this.botConfigMonitor = botConfigMonitor;
this.botSettingsMonitor = botSettingsMonitor;
this.commandService = commandService;
this.botConfigurationService = botConfigurationService;
}

private BotConfig config => botConfigMonitor.CurrentValue;
private BotSettings settings => botSettingsMonitor.CurrentValue;

[Hidden]
[Command("bad bot")]
Expand All @@ -51,7 +52,7 @@ public Task AcknowledgeGoodBot()
[AdminOnly]
[Hidden]
[Command("disable command")]
[Alias("dc")]
[Alias("disable", "dc")]
[Usage("{command name}")]
[Summary("Disables a specified command")]
public async Task DisableCommand([Remainder] string commandName)
Expand All @@ -71,28 +72,28 @@ public async Task DisableCommand([Remainder] string commandName)
}

logger.LogDebug("Disabling a command...");
RuntimeChanges.DisabledCommands.Add(commandName);
await botConfigurationService.AddDisabledCommands(commandName);
await Reply("Completed");
}

[AdminOnly]
[Hidden]
[Command("enable command")]
[Alias("ec")]
[Alias("enable", "ec")]
[Usage("{command name}")]
[Summary("Enables a specified command")]
public async Task EnableCommand([Remainder] string commandName)
{
logger.LogDebug("Enabling a command...");
RuntimeChanges.DisabledCommands.Remove(commandName);
await botConfigurationService.RemoveDisabledCommands(commandName);
await Reply("Completed");
}

[Command("help")]
[Summary("Show the list of available commands")]
public async Task Help()
{
await Reply(Usage());
await Reply(Usage(Context));
}

[Default]
Expand All @@ -101,28 +102,28 @@ public async Task Help()
[Summary("Show the list of available commands")]
public async Task NoCommand()
{
await Reply(Usage());
await Reply(Usage(Context));
}

private IEnumerable<CommandInfo> GetCommands()
private IEnumerable<CommandInfo> GetCommands(SocketCommandContext context)
{
List<CommandInfo> commands = commandService.GetCommands().ToList();
List<CommandInfo> commands = commandService.GetCommands(context).ToList();
commands.Sort((command1, command2) =>
string.Compare(command1.Name, command2.Name, StringComparison.CurrentCulture));
return commands;
}

private string Usage()
private string Usage(SocketCommandContext context)
{
IEnumerable<CommandInfo> commandList = GetCommands();
IEnumerable<CommandInfo> commandList = GetCommands(context);

var builder = new StringBuilder();

builder.AppendLine(
"To use me, tag me or tell me a command and provide additional information when needed.");
builder.AppendLine();
builder.AppendLine("Usage: !{command} {data}");
builder.AppendLine($"Usage: @{config.BotName} {{command}} {{data}}");
builder.AppendLine($"Usage: @{settings.BotName} {{command}} {{data}}");
builder.AppendLine();
builder.AppendLine("Commands -");

Expand Down
14 changes: 8 additions & 6 deletions DiscordBot/DiscordBot.Core/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

public class Bot : IHostedService
{
private readonly IOptionsMonitor<BotConfig> botConfigMonitor;

private readonly IOptionsMonitor<BotSettings> botSettingsMonitor;
private readonly IBotConfigurationService botConfigurationService;
private readonly ICommandService commandService;

private readonly IHostEnvironment environment;
Expand All @@ -26,24 +26,26 @@ public class Bot : IHostedService
private DiscordSocketClient client;

public Bot(ICommandService commandService, ILogger<Bot> logger, IHostEnvironment environment,
IOptionsMonitor<BotConfig> botConfigMonitor)
IOptionsMonitor<BotSettings> botSettingsMonitor, IBotConfigurationService botConfigurationService)
{
this.commandService = commandService;
this.logger = logger;
this.environment = environment;
this.botConfigMonitor = botConfigMonitor;
this.botSettingsMonitor = botSettingsMonitor;
this.botConfigurationService = botConfigurationService;
}

private BotConfig botConfig => botConfigMonitor?.CurrentValue ?? new BotConfig();
private BotSettings botSettings => botSettingsMonitor?.CurrentValue ?? new BotSettings();

public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
LogStartup();
await botConfigurationService.ReadConfiguration();
await commandService.AddModulesAsync();
client = new DiscordSocketClient();
string token = botConfig.Token;
string token = botSettings.Token;
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();

Expand Down
10 changes: 10 additions & 0 deletions DiscordBot/DiscordBot.Core/BotConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;

namespace DiscordBot.Core
{
public class BotConfiguration
{
public IList<string> DisabledCommands { get; set; } = new List<string>();
}
}
60 changes: 60 additions & 0 deletions DiscordBot/DiscordBot.Core/BotConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Microsoft.Extensions.Options;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace DiscordBot.Core
{
public class BotConfigurationProvider<T> : IBotConfigurationService where T: BotConfiguration, new()
{
private readonly IOptionsMonitor<BotSettings> botSettingsMonitor;
protected T configuration;

public BotConfigurationProvider(IOptionsMonitor<BotSettings> botSettingsMonitor)
{
this.botSettingsMonitor = botSettingsMonitor;
}

private BotSettings BotSettings => botSettingsMonitor.CurrentValue;
private string ConfigurationPath => BotSettings.ConfigurationPath;

public Task AddDisabledCommands(string commandName)
{
configuration.DisabledCommands.Add(commandName);
return WriteConfiguration();
}

public bool DisabledCommandsContains(string name)
{
return configuration.DisabledCommands.Contains(name);
}

public async Task ReadConfiguration()
{
T configuration;
if (!File.Exists(ConfigurationPath))
{
configuration = new T();
}
else
{
using var read = File.OpenRead(ConfigurationPath);
configuration = await JsonSerializer.DeserializeAsync<T>(read);
}

this.configuration = configuration;
}

public Task RemoveDisabledCommands(string commandName)
{
configuration.DisabledCommands.Remove(commandName);
return WriteConfiguration();
}

protected async Task WriteConfiguration()
{
using var write = File.OpenWrite(ConfigurationPath);
await JsonSerializer.SerializeAsync(write, configuration);
}
}
}
6 changes: 4 additions & 2 deletions DiscordBot/DiscordBot.Core/BotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ internal class BotModule : ModuleBase<SocketCommandContext>
private readonly Emoji hourglass = new Emoji("\u23F3");

private readonly ILogger logger;
private readonly IBotConfigurationService botConfigurationService;

public BotModule(ILogger logger)
public BotModule(ILogger logger, IBotConfigurationService botConfigurationService)
{
this.logger = logger;
this.botConfigurationService = botConfigurationService;
}

protected async Task Announce(string message, string announceGuild, string announceChannel)
Expand Down Expand Up @@ -52,7 +54,7 @@ protected async Task Reply(Func<Task<string>> getMessage, [CallerMemberName] str
{
CommandAttribute commandAttribute = GetCommandAttribute(methodName);

if (RuntimeChanges.DisabledCommands.Contains(commandAttribute.Text))
if (botConfigurationService.DisabledCommandsContains(commandAttribute.Text))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace DiscordBot.Core
{
public class BotConfig
public class BotSettings
{
public string BotName { get; set; }

public string Token { get; set; }
public string ConfigurationPath { get; set; }
}
}
Loading

0 comments on commit 3839265

Please sign in to comment.