mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Fixed Map Chat Bot not sending maps in case the Discord Bridge was not ready and connected.
This commit is contained in:
parent
8071cd1b78
commit
cbfc592e27
2 changed files with 44 additions and 18 deletions
|
|
@ -14,6 +14,7 @@ namespace MinecraftClient.ChatBots
|
||||||
public class DiscordBridge : ChatBot
|
public class DiscordBridge : ChatBot
|
||||||
{
|
{
|
||||||
private static DiscordBridge? instance = null;
|
private static DiscordBridge? instance = null;
|
||||||
|
public bool IsConnected { get; private set; }
|
||||||
|
|
||||||
private DiscordClient? _client;
|
private DiscordClient? _client;
|
||||||
private DiscordChannel? _channel;
|
private DiscordChannel? _channel;
|
||||||
|
|
@ -80,6 +81,7 @@ namespace MinecraftClient.ChatBots
|
||||||
}).Wait();
|
}).Wait();
|
||||||
|
|
||||||
_client.DisconnectAsync().Wait();
|
_client.DisconnectAsync().Wait();
|
||||||
|
IsConnected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -280,6 +282,7 @@ namespace MinecraftClient.ChatBots
|
||||||
Color = new DiscordColor(0x00FF00)
|
Color = new DiscordColor(0x00FF00)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
IsConnected = true;
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,12 @@ namespace MinecraftClient.ChatBots
|
||||||
{
|
{
|
||||||
public static Configs Config = new();
|
public static Configs Config = new();
|
||||||
|
|
||||||
|
public struct DiscordMap
|
||||||
|
{
|
||||||
|
public string FileName;
|
||||||
|
public int MapId;
|
||||||
|
}
|
||||||
|
|
||||||
[TomlDoNotInlineObject]
|
[TomlDoNotInlineObject]
|
||||||
public class Configs
|
public class Configs
|
||||||
{
|
{
|
||||||
|
|
@ -56,6 +62,8 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
private readonly Dictionary<int, McMap> cachedMaps = new();
|
private readonly Dictionary<int, McMap> cachedMaps = new();
|
||||||
|
|
||||||
|
private readonly Queue<DiscordMap> discordQueue = new();
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(baseDirectory))
|
if (!Directory.Exists(baseDirectory))
|
||||||
|
|
@ -238,28 +246,43 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
if (Config.Send_Rendered_To_Discord)
|
if (Config.Send_Rendered_To_Discord)
|
||||||
{
|
{
|
||||||
|
// We need to queue up images because Discord Bridge is not ready immediatelly
|
||||||
if (DiscordBridge.Config.Enabled)
|
if (DiscordBridge.Config.Enabled)
|
||||||
|
discordQueue.Enqueue(new DiscordMap { FileName = fileName, MapId = map.MapId });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update()
|
||||||
|
{
|
||||||
|
if (!DiscordBridge.Config.Enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
DiscordBridge? discordBridge = DiscordBridge.GetInstance();
|
||||||
|
|
||||||
|
if (discordBridge == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!discordBridge.IsConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (discordQueue.Count > 0)
|
||||||
|
{
|
||||||
|
DiscordMap discordMap = discordQueue.Dequeue();
|
||||||
|
string fileName = discordMap.FileName;
|
||||||
|
|
||||||
|
// We must convert to a PNG in order to send to Discord, BMP does not work
|
||||||
|
string newFileName = fileName.Replace(".bmp", ".png");
|
||||||
|
using (var image = new MagickImage(fileName))
|
||||||
{
|
{
|
||||||
DiscordBridge? discordBridge = DiscordBridge.GetInstance();
|
image.Write(newFileName);
|
||||||
|
discordBridge.SendImage(newFileName, $"> A render of the map with an id: **{discordMap.MapId}**");
|
||||||
|
newFileName = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + newFileName;
|
||||||
|
|
||||||
if (discordBridge == null)
|
// Delete the temporary file
|
||||||
return;
|
if (File.Exists(newFileName))
|
||||||
|
File.Delete(newFileName);
|
||||||
|
|
||||||
// We must convert to a PNG in order to send to Discord, BMP does not work
|
LogToConsole(Translations.TryGet("bot.map.sent_to_discord", discordMap.MapId));
|
||||||
string newFileName = fileName.Replace(".bmp", ".png");
|
|
||||||
using (var image = new MagickImage(fileName))
|
|
||||||
{
|
|
||||||
image.Write(newFileName);
|
|
||||||
discordBridge.SendImage(newFileName, $"> A render of the map with an id: **{map.MapId}**");
|
|
||||||
|
|
||||||
newFileName = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + newFileName;
|
|
||||||
|
|
||||||
// Delete the temporary file
|
|
||||||
if (File.Exists(newFileName))
|
|
||||||
File.Delete(newFileName);
|
|
||||||
|
|
||||||
LogToConsole(Translations.TryGet("bot.map.sent_to_discord", map.MapId));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue