]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Discord Command Arguments as List (#41113)
authorsleepyyapril <123355664+sleepyyapril@users.noreply.github.com>
Mon, 27 Oct 2025 14:15:51 +0000 (11:15 -0300)
committerGitHub <noreply@github.com>
Mon, 27 Oct 2025 14:15:51 +0000 (14:15 +0000)
* 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*

Content.Server/Discord/DiscordLink/DiscordChatLink.cs
Content.Server/Discord/DiscordLink/DiscordLink.cs

index 358bc4ab3ec14145536064ab582e727ec76e3beb..34ddc39f71209b2bcb56935c1d8689bdea4b7fde 100644 (file)
@@ -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))
index cbfe12f180fcc54307a0d7fa3c46de19bc279f0d..5bfb61d4d10560e04eba57a17a9348a2166df089 100644 (file)
@@ -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;