Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Implement shared "info" command
Browse files Browse the repository at this point in the history
Displays ping, uptime, prefix, and version information
  • Loading branch information
oliverbooth committed Mar 20, 2022
1 parent 75100f9 commit 4f8ad48
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BrackeysBot.API/IBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface IBot
/// Gets the bot version.
/// </summary>
/// <value>The bot version.</value>
string Version { get; }
static string Version { get; } = string.Empty;
}
9 changes: 9 additions & 0 deletions BrackeysBot.API/Plugins/IPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public interface IPlugin : IDisposable, IConfigurationHolder
/// <value>The data directory.</value>
DirectoryInfo DataDirectory { get; }

/// <summary>
/// Gets the date and time at which this plugin was last enabled.
/// </summary>
/// <value>
/// A <see cref="DateTimeOffset" /> representing the date and time at which this plugin was enabled, or
/// <see langword="null" /> if this plugin is not currently enabled.
/// </value>
DateTimeOffset? EnableTime { get; }

/// <summary>
/// Gets the logger for this plugin.
/// </summary>
Expand Down
29 changes: 16 additions & 13 deletions BrackeysBot.API/Plugins/MonoPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ namespace BrackeysBot.API.Plugins;
/// </summary>
public abstract class MonoPlugin : IPlugin
{
/// <inheritdoc />
~MonoPlugin()
{
Dispose();
}
internal AssemblyLoadContext LoadContext { get; set; } = null!;

/// <summary>
/// Gets the underlying <see cref="DisCatSharp.DiscordClient" /> instance.
/// </summary>
/// <value>The underlying <see cref="DisCatSharp.DiscordClient" />.</value>
protected internal DiscordClient? DiscordClient { get; internal set; }

/// <inheritdoc />
public IConfiguration Configuration { get; internal set; } = null!;

/// <inheritdoc />
public DirectoryInfo DataDirectory { get; internal set; } = null!;

/// <inheritdoc />
public DateTimeOffset? EnableTime { get; internal set; } = null!;

/// <inheritdoc />
public ILogger Logger { get; internal set; } = null!;

Expand All @@ -38,19 +43,17 @@ public abstract class MonoPlugin : IPlugin
/// <inheritdoc />
public IServiceProvider ServiceProvider { get; internal set; } = null!;

internal AssemblyLoadContext LoadContext { get; set; } = null!;

/// <summary>
/// Gets the underlying <see cref="DisCatSharp.DiscordClient" /> instance.
/// </summary>
/// <value>The underlying <see cref="DisCatSharp.DiscordClient" />.</value>
protected internal DiscordClient? DiscordClient { get; internal set; }

/// <inheritdoc />
public virtual void Dispose()
{
}

/// <inheritdoc />
~MonoPlugin()
{
Dispose();
}

/// <summary>
/// Allows configuration of the plugin's <see cref="IServiceProvider" />.
/// </summary>
Expand Down
9 changes: 3 additions & 6 deletions BrackeysBot/BrackeysBotApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ internal sealed class BrackeysBotApp : BackgroundService, IBot
{
private readonly List<Assembly> _libraries = new();

/// <summary>
/// Initializes a new instance of the <see cref="BrackeysBotApp" /> class.
/// </summary>
public BrackeysBotApp()
static BrackeysBotApp()
{
var apiAssembly = Assembly.GetAssembly(typeof(IBot))!;
var assembly = Assembly.GetAssembly(typeof(BrackeysBotApp))!;
Expand All @@ -35,7 +32,7 @@ public BrackeysBotApp()
/// Gets the version of the API in use.
/// </summary>
/// <value>The API version.</value>
public string ApiVersion { get; }
public static string ApiVersion { get; }

/// <summary>
/// Gets the <c>libraries</c> directory for this bot.
Expand All @@ -50,7 +47,7 @@ public BrackeysBotApp()
public IPluginManager PluginManager { get; } = new SimplePluginManager();

/// <inheritdoc />
public string Version { get; }
public static string Version { get; }

/// <summary>
/// Disables all currently-loaded and currently-enabled plugins.
Expand Down
73 changes: 73 additions & 0 deletions BrackeysBot/Commands/InfoCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Text;
using System.Threading.Tasks;
using BrackeysBot.API.Attributes;
using BrackeysBot.API.Extensions;
using BrackeysBot.API.Plugins;
using DisCatSharp;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Attributes;
using DisCatSharp.Entities;

namespace BrackeysBot.Commands;

/// <summary>
/// Represents a class which implements the <c>info</c> command. The <c>info</c> command requires the mention prefix to
/// discern which bot details are being requested.
/// </summary>
internal sealed class InfoCommand : BaseCommandModule
{
private readonly IPlugin _plugin;

/// <summary>
/// Initializes a new instance of the <see cref="InfoCommand" /> class.
/// </summary>
/// <param name="plugin">The owning plugin.</param>
public InfoCommand(IPlugin plugin)
{
_plugin = plugin;
}

[Command("info")]
[Description("Displays information about the bot.")]
[RequireGuild]
[RequireMentionPrefix]
public async Task InfoCommandAsync(CommandContext context)
{
await context.AcknowledgeAsync();

DiscordColor color = 0;
DiscordGuild guild = context.Guild;
DiscordUser currentUser = context.Client.CurrentUser;

if (guild.Members.TryGetValue(currentUser.Id, out DiscordMember? member))
color = member.Color;

if (color.Value == 0)
color = 0x3F51B5;

var embed = new DiscordEmbedBuilder();
embed.WithFooter(guild.Name, guild.IconUrl);
embed.WithThumbnail(currentUser.GetAvatarUrl(ImageFormat.Png));
embed.WithColor(color);

string prefix = _plugin.Configuration.Get<string>("discord.prefix") ?? "[]";

embed.AddField(Formatter.Underline("Ping"), context.Client.Ping, true);
embed.AddField(Formatter.Underline("Prefix"), prefix, true);
embed.AddFieldIf(_plugin.EnableTime.HasValue, Formatter.Underline("Enabled"),
() => Formatter.Timestamp(_plugin.EnableTime!.Value), true);

var builder = new StringBuilder();
builder.AppendLine($"BrackeysBot: {BrackeysBotApp.Version}");
builder.AppendLine($"BrackeysBot.API: {BrackeysBotApp.ApiVersion}");
builder.AppendLine($"{_plugin.PluginInfo.Name}: {_plugin.PluginInfo.Version}");
builder.AppendLine($"DisCatSharp: {context.Client.VersionString}");
builder.AppendLine($"CLR: {Environment.Version}");
builder.AppendLine($"Host: {Environment.OSVersion}");

embed.AddField(Formatter.Underline("Version"), Formatter.BlockCode(builder.ToString().Trim()));

await context.RespondAsync(embed);
}
}
10 changes: 9 additions & 1 deletion BrackeysBot/Plugins/SimplePluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using BrackeysBot.API.Exceptions;
using BrackeysBot.API.Plugins;
using BrackeysBot.ArgumentConverters;
using BrackeysBot.Commands;
using BrackeysBot.Configuration;
using BrackeysBot.Resources;
using DisCatSharp;
Expand Down Expand Up @@ -83,6 +84,7 @@ public void DisablePlugin(IPlugin plugin)

_loadedPlugins[plugin] = false;

monoPlugin.EnableTime = null;
monoPlugin.DiscordClient?.DisconnectAsync();
Logger.Info(string.Format(LoggerMessages.DisabledPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version));
}
Expand All @@ -95,6 +97,8 @@ public void EnablePlugin(IPlugin plugin)
if (_loadedPlugins[plugin]) return;
if (plugin is not MonoPlugin monoPlugin) return;

monoPlugin.EnableTime = DateTimeOffset.UtcNow;

foreach (IHostedService hostedService in plugin.ServiceProvider.GetServices<IHostedService>())
{
try
Expand Down Expand Up @@ -408,11 +412,14 @@ private IEnumerable<IPlugin> EnumeratePluginDependencies(Type pluginType)

client.MessageCreated += (sender, e) => ClientOnMessageCreated(plugin, sender, e);

return client.UseCommandsNext(new CommandsNextConfiguration
CommandsNextExtension? commandsNext = client.UseCommandsNext(new CommandsNextConfiguration
{
ServiceProvider = plugin.ServiceProvider,
UseDefaultCommandHandler = false
});

commandsNext.RegisterCommands<InfoCommand>();
return commandsNext;
}

private void SetupPluginDataDirectory(PluginInfo pluginInfo, MonoPlugin instance)
Expand Down Expand Up @@ -471,6 +478,7 @@ private void SetupPluginServices(MonoPlugin instance, PluginInfo pluginInfo, Typ
});

serviceCollection.AddSingleton<IPluginManager>(this);
serviceCollection.AddSingleton<IPlugin>(instance);
serviceCollection.AddSingleton(instance.GetType(), instance);
serviceCollection.AddSingleton(instance.Configuration);

Expand Down

0 comments on commit 4f8ad48

Please sign in to comment.