Files
ClubPenguinServer/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs
rootdarkarchon 9eb5967935 rework server responsibilities (#18)
* rework server responsibilities
add remote configuration

* start metrics only when compiled as not debug

* add some more logging to discord bot

* fixes of some casts

* make metrics port configurable, minor fixes

* add docker bullshit

* md formatting

* adjustments to docker stuff

* fix docker json files, fix some stuff in discord bot, add /useradd for Discord bot

* adjust docker configs and fix sharded.bat

* fixes for logs, cache file provider repeat trying to open filestream

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
2022-12-27 13:48:05 +01:00

62 lines
2.0 KiB
C#

using System.Collections.Concurrent;
using MareSynchronosShared.Metrics;
namespace MareSynchronosServices.Discord;
public class DiscordBotServices
{
public ConcurrentQueue<KeyValuePair<ulong, Action<IServiceProvider>>> VerificationQueue { get; } = new();
public ConcurrentDictionary<ulong, DateTime> LastVanityChange = new();
public ConcurrentDictionary<string, DateTime> LastVanityGidChange = new();
public ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new();
public ConcurrentDictionary<ulong, string> DiscordRelinkLodestoneMapping = new();
public readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" };
private readonly IServiceProvider _serviceProvider;
public ILogger<DiscordBotServices> Logger { get; init; }
public MareMetrics Metrics { get; init; }
private CancellationTokenSource? verificationTaskCts;
public DiscordBotServices(IServiceProvider serviceProvider, ILogger<DiscordBotServices> logger, MareMetrics metrics)
{
_serviceProvider = serviceProvider;
Logger = logger;
Metrics = metrics;
}
public Task Start()
{
_ = ProcessVerificationQueue();
return Task.CompletedTask;
}
public Task Stop()
{
verificationTaskCts?.Cancel();
return Task.CompletedTask;
}
private async Task ProcessVerificationQueue()
{
verificationTaskCts = new CancellationTokenSource();
while (!verificationTaskCts.IsCancellationRequested)
{
if (VerificationQueue.TryDequeue(out var queueitem))
{
try
{
queueitem.Value.Invoke(_serviceProvider);
Logger.LogInformation("Sent login information to user");
}
catch (Exception e)
{
Logger.LogError(e, "Error during queue work");
}
}
await Task.Delay(TimeSpan.FromSeconds(2), verificationTaskCts.Token).ConfigureAwait(false);
}
}
}