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.Server.Chat.Managers;
|
||||||
using Content.Shared.CCVar;
|
using Content.Shared.CCVar;
|
||||||
using Content.Shared.Chat;
|
using Content.Shared.Chat;
|
||||||
|
using NetCord;
|
||||||
using NetCord.Gateway;
|
using NetCord.Gateway;
|
||||||
using Robust.Shared.Asynchronous;
|
using Robust.Shared.Asynchronous;
|
||||||
using Robust.Shared.Configuration;
|
using Robust.Shared.Configuration;
|
||||||
@@ -24,6 +25,10 @@ public sealed class DiscordChatLink : IPostInjectInit
|
|||||||
{
|
{
|
||||||
_discordLink.OnMessageReceived += OnMessageReceived;
|
_discordLink.OnMessageReceived += OnMessageReceived;
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
_discordLink.RegisterCommandCallback(OnDebugCommandRun, "debug");
|
||||||
|
#endif
|
||||||
|
|
||||||
_configurationManager.OnValueChanged(CCVars.OocDiscordChannelId, OnOocChannelIdChanged, true);
|
_configurationManager.OnValueChanged(CCVars.OocDiscordChannelId, OnOocChannelIdChanged, true);
|
||||||
_configurationManager.OnValueChanged(CCVars.AdminChatDiscordChannelId, OnAdminChannelIdChanged, true);
|
_configurationManager.OnValueChanged(CCVars.AdminChatDiscordChannelId, OnAdminChannelIdChanged, true);
|
||||||
}
|
}
|
||||||
@@ -36,6 +41,14 @@ public sealed class DiscordChatLink : IPostInjectInit
|
|||||||
_configurationManager.UnsubValueChanged(CCVars.AdminChatDiscordChannelId, OnAdminChannelIdChanged);
|
_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)
|
private void OnOocChannelIdChanged(string channelId)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(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 Content.Shared.CCVar;
|
||||||
using NetCord;
|
using NetCord;
|
||||||
using NetCord.Gateway;
|
using NetCord.Gateway;
|
||||||
using NetCord.Rest;
|
using NetCord.Rest;
|
||||||
using Robust.Shared.Configuration;
|
using Robust.Shared.Configuration;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Server.Discord.DiscordLink;
|
namespace Content.Server.Discord.DiscordLink;
|
||||||
|
|
||||||
@@ -18,9 +20,16 @@ public sealed class CommandReceivedEventArgs
|
|||||||
public string Command { get; init; } = string.Empty;
|
public string Command { get; init; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </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>
|
/// <summary>
|
||||||
/// Information about the message that the command was received from. This includes the message content, author, etc.
|
/// 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.
|
/// Use this to reply to the message, delete it, etc.
|
||||||
@@ -66,6 +75,7 @@ public sealed class DiscordLink : IPostInjectInit
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public event Action<Message>? OnMessageReceived;
|
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)
|
public void RegisterCommandCallback(Action<CommandReceivedEventArgs> callback, string command)
|
||||||
{
|
{
|
||||||
OnCommandReceived += args =>
|
OnCommandReceived += args =>
|
||||||
@@ -180,24 +190,28 @@ public sealed class DiscordLink : IPostInjectInit
|
|||||||
var trimmedInput = content[BotPrefix.Length..].Trim();
|
var trimmedInput = content[BotPrefix.Length..].Trim();
|
||||||
var firstSpaceIndex = trimmedInput.IndexOf(' ');
|
var firstSpaceIndex = trimmedInput.IndexOf(' ');
|
||||||
|
|
||||||
string command, arguments;
|
string command, rawArguments;
|
||||||
|
|
||||||
if (firstSpaceIndex == -1)
|
if (firstSpaceIndex == -1)
|
||||||
{
|
{
|
||||||
command = trimmedInput;
|
command = trimmedInput;
|
||||||
arguments = string.Empty;
|
rawArguments = string.Empty;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
command = trimmedInput[..firstSpaceIndex];
|
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!
|
// Raise the event!
|
||||||
OnCommandReceived?.Invoke(new CommandReceivedEventArgs
|
OnCommandReceived?.Invoke(new CommandReceivedEventArgs
|
||||||
{
|
{
|
||||||
Command = command,
|
Command = command,
|
||||||
Arguments = arguments,
|
Arguments = argumentList,
|
||||||
|
RawArguments = rawArguments,
|
||||||
Message = message,
|
Message = message,
|
||||||
});
|
});
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
|
|||||||
Reference in New Issue
Block a user