From 41f6bcf00e21f6d8251c5b955d14663ecb387075 Mon Sep 17 00:00:00 2001 From: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com> Date: Mon, 27 Oct 2025 11:15:51 -0300 Subject: [PATCH] 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* --- .../Discord/DiscordLink/DiscordChatLink.cs | 13 +++++++++ .../Discord/DiscordLink/DiscordLink.cs | 28 ++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Content.Server/Discord/DiscordLink/DiscordChatLink.cs b/Content.Server/Discord/DiscordLink/DiscordChatLink.cs index 358bc4ab3e..34ddc39f71 100644 --- a/Content.Server/Discord/DiscordLink/DiscordChatLink.cs +++ b/Content.Server/Discord/DiscordLink/DiscordChatLink.cs @@ -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)) diff --git a/Content.Server/Discord/DiscordLink/DiscordLink.cs b/Content.Server/Discord/DiscordLink/DiscordLink.cs index cbfe12f180..5bfb61d4d1 100644 --- a/Content.Server/Discord/DiscordLink/DiscordLink.cs +++ b/Content.Server/Discord/DiscordLink/DiscordLink.cs @@ -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; /// - /// The arguments to the command. This is everything after the command + /// The raw arguments to the command. This is everything after the command /// - public string Arguments { get; init; } = string.Empty; + public string RawArguments { get; init; } = string.Empty; + + /// + /// A list of arguments to the command. + /// This uses mostly for maintainability. + /// + public List Arguments { get; init; } = []; + /// /// 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 /// public event Action? OnMessageReceived; + // TODO: consider implementing this in a way where we can unregister it in a similar way public void RegisterCommandCallback(Action 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(); + CommandParsing.ParseArguments(rawArguments, argumentList); + // Raise the event! OnCommandReceived?.Invoke(new CommandReceivedEventArgs { Command = command, - Arguments = arguments, + Arguments = argumentList, + RawArguments = rawArguments, Message = message, }); return ValueTask.CompletedTask;