Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions App.config

This file was deleted.

17 changes: 11 additions & 6 deletions Discord/DiscordBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace SysbotMacro.Discord
{
internal class DiscordBot
{
string _token;
public Action<string> LogAction { get; set; }
private readonly string _token;
public Action<string>? LogAction { get; set; }

private DiscordSocketClient _client;
private DiscordSocketClient? _client;
private readonly List<ulong> _sudoUserIds;
private readonly List<ulong> _channelIds;
private readonly List<Dictionary<string, object>> _ipDict;
private readonly List<Dictionary<string, object>> _macroDict;

private MessageHandler _messageHandler;
private readonly MessageHandler _messageHandler;

public DiscordBot(string token, List<ulong> sudos, List<ulong> channelids, List<Dictionary<string, object>> ipDict, List<Dictionary<string, object>> macroDict)
{
Expand All @@ -40,6 +40,8 @@ public DiscordBot(string token, List<ulong> sudos, List<ulong> channelids, List<

public async Task MainAsync()
{
if (_client == null) return;

_client.Log += LogAsync;
_client.MessageReceived += MessageReceivedAsync;

Expand Down Expand Up @@ -84,8 +86,11 @@ await _messageHandler.HandleMessage(message, async (response) =>
//call to stop the bot
public async Task StopAsync()
{
await _client.LogoutAsync();
await _client.StopAsync();
if (_client != null)
{
await _client.LogoutAsync();
await _client.StopAsync();
}
}
}
}
136 changes: 83 additions & 53 deletions Discord/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Text;
using System.Net;
using Newtonsoft.Json.Linq;
using static LibUsbDotNet.Main.UsbTransferQueue;
using System.Runtime.InteropServices;
using System.IO;

Expand All @@ -21,6 +20,10 @@ namespace SysbotMacro.Discord
{
internal class MessageHandler
{
private const int DEFAULT_SWITCH_PORT = 6000;
private const int PING_TIMEOUT_MS = 2000;
private const string COMMAND_PREFIX = "!";

private readonly List<Dictionary<string, object>> _ipDict;
private readonly List<Dictionary<string, object>> _macroDict;

Expand All @@ -41,8 +44,8 @@ public async Task HandleMessage(SocketMessage message, Func<string, Task> sendRe
var firstPart = parts.FirstOrDefault();


// Check if the first word starts with '!'
if (firstPart != null && firstPart.StartsWith("!")) //edit the prefix here. Maybe i should add it to the form.
// Check if the first word starts with the command prefix
if (firstPart != null && firstPart.StartsWith(COMMAND_PREFIX))
{
var command = firstPart.Substring(1); // Remove prefix

Expand All @@ -64,7 +67,7 @@ public async Task HandleMessage(SocketMessage message, Func<string, Task> sendRe
var embed = new EmbedBuilder
{
Title = "Macro and Switch IP Data",
Color = Color.Blue
Color = global::Discord.Color.Blue
};

string macroField = "";
Expand Down Expand Up @@ -117,35 +120,44 @@ public async Task HandleMessage(SocketMessage message, Func<string, Task> sendRe
return;
}

Ping pingSender = new Ping();

// Ping the IP addres.
PingReply reply = await pingSender.SendPingAsync(switchIp, 2000); // 2000 ms timeout

// Check the status and if down, exit to prevent hanging
if (reply.Status != IPStatus.Success)
using (var pingSender = new Ping())
{
await sendResponse($"Switch IP {switchIp} is not reachable");
return;
}
// Ping the IP address
PingReply reply = await pingSender.SendPingAsync(switchIp, PING_TIMEOUT_MS);

// Check the status and if down, exit to prevent hanging
if (reply.Status != IPStatus.Success)
{
await sendResponse($"Switch IP {switchIp} is not reachable");
return;
}
}

// Execute
await sendResponse($"Executing macro {command} on Switch {switchKey}");
var config = new SwitchConnectionConfig
{
IP = switchIp,
Port = 6000,
Port = DEFAULT_SWITCH_PORT,
Protocol = SwitchProtocol.WiFi
};

var bot = new Bot(config);
bot.Connect();
var cancellationToken = new CancellationToken(); // not tied to anything yet...
await bot.ExecuteCommands(macro, () => false, cancellationToken); // not tied to anything yet... Should i?
bot.Disconnect();

await sendResponse($"Executed macro {command} on Switch {switchKey}");
try
{
bot.Connect();
var cancellationToken = new CancellationToken();
await bot.ExecuteCommands(macro, () => false, cancellationToken);
await sendResponse($"Executed macro {command} on Switch {switchKey}");
}
catch (Exception ex)
{
await sendResponse($"Error executing macro: {ex.Message}");
}
finally
{
try { bot.Disconnect(); } catch { }
}
}
// command: to check the status of all switches
else if (command == "status")
Expand All @@ -159,22 +171,27 @@ public async Task HandleMessage(SocketMessage message, Func<string, Task> sendRe
var ipAddress = switchDict.ContainsKey("IPAddress") ? switchDict["IPAddress"].ToString() : "Unknown";

// Initialize a new instance of the Ping class.
Ping pingSender = new Ping();

PingReply reply = await pingSender.SendPingAsync(ipAddress, 2000); // 2000 ms timeout

// Check the status.
string status = reply.Status == IPStatus.Success ? "Up" : "Down";

embedDescription.AppendLine($"Name: {switchName}, IP: {ipAddress}, Status: {status}");
using (var pingSender = new Ping())
{
try
{
PingReply reply = await pingSender.SendPingAsync(ipAddress, PING_TIMEOUT_MS);
string status = reply.Status == IPStatus.Success ? "Up" : "Down";
embedDescription.AppendLine($"Name: {switchName}, IP: {ipAddress}, Status: {status}");
}
catch (Exception ex)
{
embedDescription.AppendLine($"Name: {switchName}, IP: {ipAddress}, Status: Error - {ex.Message}");
}
}
}

// Create embed message with Discord.Net's EmbedBuilder.
var embed = new EmbedBuilder
{
Title = "Switch Status Check",
Description = embedDescription.ToString(),
Color = Color.Green // You can change the color based on your preferences.
Color = global::Discord.Color.Green // You can change the color based on your preferences.
};

// Send the embed.
Expand Down Expand Up @@ -206,39 +223,52 @@ public async Task HandleMessage(SocketMessage message, Func<string, Task> sendRe
var config = new SwitchConnectionConfig
{
IP = switchIp,
Port = 6000,
Port = DEFAULT_SWITCH_PORT,
Protocol = SwitchProtocol.WiFi
};
var bot = new Bot(config);
bot.Connect();
byte[] imageBytes = bot.PixelPeek();
System.IO.File.WriteAllBytes("test.jpg", imageBytes);
string hexString = Encoding.UTF8.GetString(imageBytes);
try
{
bot.Connect();
byte[]? imageBytes = bot.PixelPeek();

if (imageBytes == null || imageBytes.Length == 0)
{
await sendResponse("Failed to capture the screen - no data received.");
return;
}

// Convert the hexadecimal string back to a byte array
byte[] actualImageBytes = Enumerable.Range(0, hexString.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
.ToArray();
string hexString = Encoding.UTF8.GetString(imageBytes);

// Write the byte array to a .jpg file
System.IO.File.WriteAllBytes("test.jpg", actualImageBytes);
// Convert the hexadecimal string back to a byte array
byte[] actualImageBytes = Enumerable.Range(0, hexString.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
.ToArray();

// Send the image as an attachment in Discord
if (actualImageBytes != null && actualImageBytes.Length > 0)
{
using (var stream = new MemoryStream(actualImageBytes))
// Send the image as an attachment in Discord
if (actualImageBytes.Length > 0)
{
using (var stream = new MemoryStream(actualImageBytes))
{
stream.Seek(0, SeekOrigin.Begin);
var embed = new EmbedBuilder();
embed.ImageUrl = "attachment://screenshot.jpg";
await ((ISocketMessageChannel)message.Channel).SendFileAsync(stream, "screenshot.jpg", $"Here's the screenshot of {switchKey}", embed: embed.Build());
}
}
else
{
stream.Seek(0, SeekOrigin.Begin);
var embed = new EmbedBuilder();
embed.ImageUrl = "attachment://screenshot.jpg";
Console.WriteLine($"Received {actualImageBytes.Length} bytes. First bytes: {actualImageBytes[0]} {actualImageBytes[1]} ...");
await ((ISocketMessageChannel)message.Channel).SendFileAsync(stream, "screenshot.jpg", $"Here's the screenshot of {switchKey}", embed: embed.Build());
await sendResponse("Failed to process screen capture data.");
}
}
else
catch (Exception ex)
{
await sendResponse($"Error capturing screenshot: {ex.Message}");
}
finally
{
await sendResponse("Failed to capture the screen.");
try { bot.Disconnect(); } catch { }
}


Expand Down
Loading