From f0d76aab62bba2d1d5bec29f4c027ace7dafa8d1 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sun, 22 Sep 2024 23:30:19 -0700 Subject: implement sending discord messages from in-game --- DiscordToXIV/Plugin.cs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'DiscordToXIV/Plugin.cs') diff --git a/DiscordToXIV/Plugin.cs b/DiscordToXIV/Plugin.cs index 2e40bba..29c7d6f 100644 --- a/DiscordToXIV/Plugin.cs +++ b/DiscordToXIV/Plugin.cs @@ -33,6 +33,8 @@ public sealed class Plugin : IDalamudPlugin private bool IsWebSocketServerRunning = false; private WebSocketServer webSocketServer; + private DiscordMessenger discordMessenger; + private string selectedChannelID = ""; private readonly CancellationTokenSource cancellationTokenSource; private readonly List connectedClients; public Configuration Configuration { get; init; } @@ -50,11 +52,24 @@ public sealed class Plugin : IDalamudPlugin WindowSystem.AddWindow(MainWindow); cancellationTokenSource = new CancellationTokenSource(); connectedClients = new List(); + discordMessenger = new DiscordMessenger(this, Configuration.DiscordAuthToken); CommandManager.AddHandler(CommandName, new CommandInfo(OnCommand) { HelpMessage = "Start WebSocket server. Usage: /pdiscordtoxiv [port]" }); + + CommandManager.AddHandler("/pdtxset", new CommandInfo(SetChannelCommand) + { + HelpMessage = "Set a channel ID to send messages to. Usage: /pdtxset " + }); + + CommandManager.AddHandler("/pdtxs", new CommandInfo(SendMessageCommand) + { + HelpMessage = "Send a message to the selected channel. Usage: /pdtxs " + }); + + PluginInterface.UiBuilder.Draw += DrawUI; PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI; PluginInterface.UiBuilder.OpenMainUi += ToggleMainUI; @@ -112,6 +127,42 @@ public sealed class Plugin : IDalamudPlugin } } + + private void SendMessageCommand(string command, string args) + { + if (string.IsNullOrEmpty(args)) + { + ChatGui.PrintError("[DiscordToXIV] No message provided."); + return; + } + + if (string.IsNullOrEmpty(selectedChannelID)) + { + ChatGui.PrintError("[DiscordToXIV] No channel selected."); + return; + } + + discordMessenger.SendMessageAsync(args, selectedChannelID); + } + + private void SetChannelCommand(string command, string args) + { + if (string.IsNullOrEmpty(args)) + { + ChatGui.PrintError("[DiscordToXIV] No channel ID provided."); + return; + } + + if (Configuration.ChannelMappings.ContainsKey(args)) + { + selectedChannelID = args; + ChatGui.Print("[DiscordToXIV] Channel set to " + Configuration.ChannelMappings[args]); + } + else + { + ChatGui.PrintError("[DiscordToXIV] Channel ID not found in configuration."); + } + } private void StartWebSocketServer(int port) { -- cgit v1.2.3