-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from foldingcash/dev
.NET Upgrade and some usability enhancements
- Loading branch information
Showing
22 changed files
with
253 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
DiscordBot/DiscordBot.Core/BotConfig.cs → DiscordBot/DiscordBot.Core/BotSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
Oops, something went wrong.