1
0
mirror of https://github.com/oliverbooth/VPLink synced 2024-11-10 04:45:42 +00:00
VPLink/VpBridge/Services/VirtualParadiseService.cs

80 lines
3.5 KiB
C#
Raw Normal View History

2023-08-26 12:45:49 +01:00
using System.Reactive.Linq;
2023-08-22 14:57:18 +01:00
using System.Reactive.Subjects;
using Discord;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using VpSharp;
using VpSharp.Entities;
using Color = System.Drawing.Color;
namespace VpBridge.Services;
/// <inheritdoc cref="IVirtualParadiseService" />
internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadiseService
{
private readonly ILogger<VirtualParadiseService> _logger;
private readonly IConfiguration _configuration;
private readonly VirtualParadiseClient _virtualParadiseClient;
private readonly Subject<VirtualParadiseMessage> _messageReceived = new();
/// <summary>
/// Initializes a new instance of the <see cref="VirtualParadiseService" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="virtualParadiseClient">The Virtual Paradise client.</param>
public VirtualParadiseService(ILogger<VirtualParadiseService> logger,
IConfiguration configuration,
VirtualParadiseClient virtualParadiseClient)
{
_logger = logger;
_configuration = configuration;
_virtualParadiseClient = virtualParadiseClient;
}
/// <inheritdoc />
public IObservable<VirtualParadiseMessage> OnMessageReceived => _messageReceived.AsObservable();
/// <inheritdoc />
public Task SendMessageAsync(IUserMessage message)
{
if (message is null) throw new ArgumentNullException(nameof(message));
if (string.IsNullOrWhiteSpace(message.Content)) return Task.CompletedTask;
if (message.Author.IsBot && !_configuration.GetSection("Bot:RelayBotMessages").Get<bool>())
{
_logger.LogDebug("Bot messages are disabled, ignoring message");
return Task.CompletedTask;
}
_logger.LogInformation("Message by {Author}: {Content}", message.Author, message.Content);
string displayName = message.Author.GlobalName ?? message.Author.Username;
return _virtualParadiseClient.SendMessageAsync(displayName, message.Content, FontStyle.Bold, Color.MidnightBlue);
}
/// <inheritdoc />
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Establishing relay");
_virtualParadiseClient.MessageReceived.Subscribe(_messageReceived);
string username = _configuration.GetSection("VirtualParadise:Username").Value ??
throw new InvalidOperationException("Username is not set.");
string password = _configuration.GetSection("VirtualParadise:Password").Value ??
throw new InvalidOperationException("Password is not set.");
string world = _configuration.GetSection("VirtualParadise:World").Value ??
throw new InvalidOperationException("World is not set.");
string botName = _configuration.GetSection("VirtualParadise:BotName").Value ??
throw new InvalidOperationException("Bot name is not set.");
_logger.LogDebug("Connecting to Virtual Paradise");
await _virtualParadiseClient.ConnectAsync().ConfigureAwait(false);
await _virtualParadiseClient.LoginAsync(username, password, botName).ConfigureAwait(false);
_logger.LogInformation("Entering world {World}", world);
await _virtualParadiseClient.EnterAsync(world).ConfigureAwait(false);
}
}