feat: add /info command (resolves #2)

This commit is contained in:
Oliver Booth 2023-08-26 21:31:29 +01:00
parent e293b9b67c
commit 25d725e1b3
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,57 @@
using System.Text;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using VPLink.Services;
namespace VPLink.Commands;
/// <summary>
/// Represents a class which implements the <c>info</c> command.
/// </summary>
internal sealed class InfoCommand : InteractionModuleBase<SocketInteractionContext>
{
private readonly BotService _botService;
private readonly DiscordSocketClient _discordClient;
/// <summary>
/// Initializes a new instance of the <see cref="InfoCommand" /> class.
/// </summary>
/// <param name="botService">The bot service.</param>
/// <param name="discordClient"></param>
public InfoCommand(BotService botService, DiscordSocketClient discordClient)
{
_botService = botService;
_discordClient = discordClient;
}
[SlashCommand("info", "Displays information about the bot.")]
[RequireContext(ContextType.Guild)]
public async Task InfoAsync()
{
SocketGuildUser member = Context.Guild.GetUser(_discordClient.CurrentUser.Id);
string pencilVersion = _botService.Version;
SocketRole? highestRole = member.Roles.Where(r => r.Color != Color.Default).MaxBy(r => r.Position);
var embed = new EmbedBuilder();
embed.WithAuthor(member);
embed.WithColor(highestRole?.Color ?? Color.Default);
embed.WithThumbnailUrl(member.GetAvatarUrl());
embed.WithTitle($"VPLink v{pencilVersion}");
embed.WithDescription("Created by <@94248427663130624>, hosted [on GitHub](https://github.com/oliverbooth/VPLink).");
embed.AddField("Ping", $"{_discordClient.Latency} ms", true);
embed.AddField("Started", $"<t:{_botService.StartedAt.ToUnixTimeSeconds()}:R>", true);
var builder = new StringBuilder();
builder.AppendLine($"VPLink: {pencilVersion}");
builder.AppendLine($"Discord.Net: {_botService.DiscordNetVersion}");
builder.AppendLine($"VP#: {_botService.VpSharpVersion}");
builder.AppendLine($"CLR: {Environment.Version.ToString(3)}");
builder.AppendLine($"Host: {Environment.OSVersion}");
embed.AddField("Version", $"```\n{builder}\n```");
await RespondAsync(embed: embed.Build(), ephemeral: true).ConfigureAwait(false);
}
}

View File

@ -25,6 +25,8 @@ builder.Configuration.AddTomlFile("data/config.toml", true, true);
builder.Logging.ClearProviders();
builder.Logging.AddSerilog();
builder.Services.AddHostedSingleton<BotService>();
builder.Services.AddSingleton<VirtualParadiseClient>();
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>();

View File

@ -0,0 +1,61 @@
using System.Reflection;
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using VpSharp;
namespace VPLink.Services;
internal sealed class BotService : BackgroundService
{
private readonly ILogger<BotService> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="BotService" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
public BotService(ILogger<BotService> logger)
{
_logger = logger;
var attribute = typeof(BotService).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
Version = attribute?.InformationalVersion ?? "Unknown";
attribute = typeof(DiscordSocketClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
DiscordNetVersion = attribute?.InformationalVersion ?? "Unknown";
attribute = typeof(VirtualParadiseClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
VpSharpVersion = attribute?.InformationalVersion ?? "Unknown";
}
/// <summary>
/// Gets the Discord.Net version.
/// </summary>
/// <value>The Discord.Net version.</value>
public string DiscordNetVersion { get; }
/// <summary>
/// Gets the date and time at which the bot was started.
/// </summary>
/// <value>The start timestamp.</value>
public DateTimeOffset StartedAt { get; private set; }
/// <summary>
/// Gets the bot version.
/// </summary>
/// <value>The bot version.</value>
public string Version { get; }
/// <summary>
/// Gets the VP# version.
/// </summary>
/// <value>The VP# version.</value>
public string VpSharpVersion { get; }
/// <inheritdoc />
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
StartedAt = DateTimeOffset.UtcNow;
_logger.LogInformation("VPLink v{Version} is starting...", Version);
return Task.CompletedTask;
}
}

View File

@ -44,6 +44,7 @@ internal sealed class DiscordService : BackgroundService
_logger.LogInformation("Establishing relay");
_logger.LogInformation("Adding command modules");
await _interactionService.AddModuleAsync<InfoCommand>(_serviceProvider).ConfigureAwait(false);
await _interactionService.AddModuleAsync<WhoCommand>(_serviceProvider).ConfigureAwait(false);
_discordClient.Ready += OnReady;