aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--DiscordToXIV/Configuration.cs1
-rw-r--r--DiscordToXIV/DiscordMessenger.cs54
-rw-r--r--DiscordToXIV/Plugin.cs51
-rw-r--r--DiscordToXIV/Windows/ConfigWindow.cs8
4 files changed, 114 insertions, 0 deletions
diff --git a/DiscordToXIV/Configuration.cs b/DiscordToXIV/Configuration.cs
index 8a1e369..32fe363 100644
--- a/DiscordToXIV/Configuration.cs
+++ b/DiscordToXIV/Configuration.cs
@@ -17,6 +17,7 @@ public class Configuration : IPluginConfiguration
public bool HideWelcomeMessage { get; set; } = false;
public bool HideAttachmentUrls { get; set; } = false;
public bool HideStickerUrls { get; set; } = false;
+ public string DiscordAuthToken { get; set; } = "";
public void Save()
{
diff --git a/DiscordToXIV/DiscordMessenger.cs b/DiscordToXIV/DiscordMessenger.cs
new file mode 100644
index 0000000..a8d51f9
--- /dev/null
+++ b/DiscordToXIV/DiscordMessenger.cs
@@ -0,0 +1,54 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DiscordToXIV
+{
+ public class DiscordMessenger
+ {
+ private readonly string _authorization;
+ private readonly string _baseUrl;
+ private readonly Plugin Plugin;
+
+ public DiscordMessenger(Plugin plugin, string authorization = "", string baseUrl = "https://discord.com/api/v9/channels/")
+ {
+ Plugin = plugin;
+ _authorization = authorization;
+ _baseUrl = baseUrl;
+ }
+
+ public async Task SendMessageAsync(string message, string channelId)
+ {
+ try
+ {
+ var request = (HttpWebRequest)WebRequest.Create($"{_baseUrl}{channelId}/messages");
+ request.Method = "POST";
+ request.ContentType = "application/json";
+ request.Headers["Authorization"] = _authorization;
+ var payload = $"{{\"content\":\"{message}\"}}";
+ var data = Encoding.UTF8.GetBytes(payload);
+ using (var stream = await request.GetRequestStreamAsync())
+ {
+ await stream.WriteAsync(data, 0, data.Length);
+ }
+ using (var response = (HttpWebResponse)await request.GetResponseAsync())
+ {
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ Plugin.ChatGui.PrintError("Failed to send message!");
+ }
+ else
+ {
+ Plugin.ChatGui.Print(">> " + Plugin.Configuration.ChannelMappings[channelId] + ": " + message);
+ }
+ }
+ }
+ catch (WebException ex)
+ {
+ Plugin.ChatGui.PrintError($"Error sending message: {ex.Message}");
+ }
+ }
+ }
+}
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<IWebSocketConnection> connectedClients;
public Configuration Configuration { get; init; }
@@ -50,11 +52,24 @@ public sealed class Plugin : IDalamudPlugin
WindowSystem.AddWindow(MainWindow);
cancellationTokenSource = new CancellationTokenSource();
connectedClients = new List<IWebSocketConnection>();
+ 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 <channel_id>"
+ });
+
+ CommandManager.AddHandler("/pdtxs", new CommandInfo(SendMessageCommand)
+ {
+ HelpMessage = "Send a message to the selected channel. Usage: /pdtxs <message>"
+ });
+
+
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)
{
diff --git a/DiscordToXIV/Windows/ConfigWindow.cs b/DiscordToXIV/Windows/ConfigWindow.cs
index ca3363e..261cd24 100644
--- a/DiscordToXIV/Windows/ConfigWindow.cs
+++ b/DiscordToXIV/Windows/ConfigWindow.cs
@@ -201,6 +201,14 @@ public override void Draw()
ImGui.Text("Hides URLs of stickers in chat");
ImGui.EndTooltip();
}
+
+ ImGui.Text("Your Discord Auth Token is required if you wish to send messages");
+ var discordAuthToken = Configuration.DiscordAuthToken;
+ if (ImGui.InputText("Discord Auth Token", ref discordAuthToken, 256))
+ {
+ Configuration.DiscordAuthToken = discordAuthToken;
+ Configuration.Save();
+ }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage