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

Commit

Permalink
Made rolesync also use the new scheduled command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Jul 18, 2022
1 parent dd46603 commit b906596
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion SCPDiscordPlugin/BotListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public BotListener(SCPDiscord plugin)
break;

case MessageWrapper.MessageOneofCase.ConsoleCommand:
plugin.sync.ScheduleConsoleCommand(data.ConsoleCommand);
plugin.sync.ScheduleDiscordCommand(data.ConsoleCommand);
break;

case MessageWrapper.MessageOneofCase.RoleResponse:
Expand Down
2 changes: 1 addition & 1 deletion SCPDiscordPlugin/RoleSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void ReceiveQueryResponse(string userID, List<ulong> roleIDs)
command = command.Replace("<var:" + variable.Key + ">", variable.Value);
}
plugin.Debug("Running rolesync command: " + command);
plugin.Debug("Command response: " + plugin.ConsoleCommand(null, command.Split(' ')[0], command.Split(' ').Skip(1).ToArray()));
plugin.sync.ScheduleRoleSyncCommand(command);
}

plugin.Verbose("Synced " + player.Name + " (" + player.UserID + ") with Discord role id " + keyValuePair.Key);
Expand Down
24 changes: 0 additions & 24 deletions SCPDiscordPlugin/SCPDiscord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,29 +366,5 @@ public bool GetPlayerName(string steamID, ref string name)
}
return false;
}

/// <summary>
/// Runs a console command.
/// </summary>
/// <param name="user">The user to run the command as, null for the server itself.</param>
/// <param name="command">The name of the command to run.</param>
/// <param name="arguments">Command arguments split up into individual strings.</param>
/// <returns></returns>
public string ConsoleCommand(ICommandSender user, string command, string[] arguments)
{
if (user == null)
{
user = Server;
}

string[] feedback = plugin.PluginManager.CommandManager.CallCommand(user, command, arguments);

StringBuilder builder = new StringBuilder();
foreach (string line in feedback)
{
builder.Append(line + "\n");
}
return builder.ToString();
}
}
}
38 changes: 34 additions & 4 deletions SCPDiscordPlugin/SynchronousExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCPDiscord.Interface;
using Smod2.Commands;
using Smod2.EventHandlers;
using Smod2.Events;

Expand All @@ -11,29 +13,57 @@ public class SynchronousExecutor : IEventHandlerFixedUpdate
{
private readonly SCPDiscord plugin;
private readonly ConcurrentQueue<ConsoleCommand> queuedCommands = new ConcurrentQueue<ConsoleCommand>();

private readonly ConcurrentQueue<string> queuedRoleSyncCommands = new ConcurrentQueue<string>();
public SynchronousExecutor(SCPDiscord pl)
{
plugin = pl;
}

public void ScheduleConsoleCommand(ConsoleCommand command)
public void ScheduleDiscordCommand(ConsoleCommand command)
{
queuedCommands.Enqueue(command);
}

public void ScheduleRoleSyncCommand(string command)
{
queuedRoleSyncCommands.Enqueue(command);
}

public void OnFixedUpdate(FixedUpdateEvent ev)
{
while(queuedCommands.TryDequeue(out ConsoleCommand command))
{
string[] words = command.Command.Split(' ');
string response = plugin.ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray());
string response = ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray());
Dictionary<string, string> variables = new Dictionary<string, string>
{
{ "feedback", response }
};
plugin.SendMessageByID(command.ChannelID, "botresponses.consolecommandfeedback", variables);
}

while(queuedRoleSyncCommands.TryDequeue(out string stringCommand))
{
string[] words = stringCommand.Split(' ');
plugin.Debug("RoleSync command response: " + ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray()));
}
}

private string ConsoleCommand(ICommandSender user, string command, string[] arguments)
{
if (user == null)
{
user = plugin.Server;
}

string[] feedback = plugin.PluginManager.CommandManager.CallCommand(user, command, arguments);

StringBuilder builder = new StringBuilder();
foreach (string line in feedback)
{
builder.Append(line + "\n");
}
return builder.ToString();
}
}
}

0 comments on commit b906596

Please sign in to comment.