Discord Command Arguments as List (#41113)
* start (i got distracted) * feat: list-based arguments for the discord implementation * chore: unnecessary? * chore: rename ArgumentList to Arguments * fix: rename error * chore: todo * fix: error, again. I'm silly. * chore: review * *sound of flames*
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Content.Server.Chat.Managers;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Chat;
|
||||
using NetCord;
|
||||
using NetCord.Gateway;
|
||||
using Robust.Shared.Asynchronous;
|
||||
using Robust.Shared.Configuration;
|
||||
@@ -24,6 +25,10 @@ public sealed class DiscordChatLink : IPostInjectInit
|
||||
{
|
||||
_discordLink.OnMessageReceived += OnMessageReceived;
|
||||
|
||||
#if DEBUG
|
||||
_discordLink.RegisterCommandCallback(OnDebugCommandRun, "debug");
|
||||
#endif
|
||||
|
||||
_configurationManager.OnValueChanged(CCVars.OocDiscordChannelId, OnOocChannelIdChanged, true);
|
||||
_configurationManager.OnValueChanged(CCVars.AdminChatDiscordChannelId, OnAdminChannelIdChanged, true);
|
||||
}
|
||||
@@ -36,6 +41,14 @@ public sealed class DiscordChatLink : IPostInjectInit
|
||||
_configurationManager.UnsubValueChanged(CCVars.AdminChatDiscordChannelId, OnAdminChannelIdChanged);
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
private void OnDebugCommandRun(CommandReceivedEventArgs ev)
|
||||
{
|
||||
var args = string.Join('\n', ev.Arguments);
|
||||
_sawmill.Info($"Provided arguments: \n{args}");
|
||||
}
|
||||
#endif
|
||||
|
||||
private void OnOocChannelIdChanged(string channelId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(channelId))
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.CCVar;
|
||||
using NetCord;
|
||||
using NetCord.Gateway;
|
||||
using NetCord.Rest;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Discord.DiscordLink;
|
||||
|
||||
@@ -18,9 +20,16 @@ public sealed class CommandReceivedEventArgs
|
||||
public string Command { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The arguments to the command. This is everything after the command
|
||||
/// The raw arguments to the command. This is everything after the command
|
||||
/// </summary>
|
||||
public string Arguments { get; init; } = string.Empty;
|
||||
public string RawArguments { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// A list of arguments to the command.
|
||||
/// This uses <see cref="CommandParsing.ParseArguments"/> mostly for maintainability.
|
||||
/// </summary>
|
||||
public List<string> Arguments { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Information about the message that the command was received from. This includes the message content, author, etc.
|
||||
/// Use this to reply to the message, delete it, etc.
|
||||
@@ -66,6 +75,7 @@ public sealed class DiscordLink : IPostInjectInit
|
||||
/// </summary>
|
||||
public event Action<Message>? OnMessageReceived;
|
||||
|
||||
// TODO: consider implementing this in a way where we can unregister it in a similar way
|
||||
public void RegisterCommandCallback(Action<CommandReceivedEventArgs> callback, string command)
|
||||
{
|
||||
OnCommandReceived += args =>
|
||||
@@ -180,24 +190,28 @@ public sealed class DiscordLink : IPostInjectInit
|
||||
var trimmedInput = content[BotPrefix.Length..].Trim();
|
||||
var firstSpaceIndex = trimmedInput.IndexOf(' ');
|
||||
|
||||
string command, arguments;
|
||||
string command, rawArguments;
|
||||
|
||||
if (firstSpaceIndex == -1)
|
||||
{
|
||||
command = trimmedInput;
|
||||
arguments = string.Empty;
|
||||
rawArguments = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
command = trimmedInput[..firstSpaceIndex];
|
||||
arguments = trimmedInput[(firstSpaceIndex + 1)..].Trim();
|
||||
rawArguments = trimmedInput[(firstSpaceIndex + 1)..].Trim();
|
||||
}
|
||||
|
||||
var argumentList = new List<string>();
|
||||
CommandParsing.ParseArguments(rawArguments, argumentList);
|
||||
|
||||
// Raise the event!
|
||||
OnCommandReceived?.Invoke(new CommandReceivedEventArgs
|
||||
{
|
||||
Command = command,
|
||||
Arguments = arguments,
|
||||
Arguments = argumentList,
|
||||
RawArguments = rawArguments,
|
||||
Message = message,
|
||||
});
|
||||
return ValueTask.CompletedTask;
|
||||
|
||||
Reference in New Issue
Block a user