mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Another bug fix and added safeguards to the Discord Bridge chat bot.
This commit is contained in:
parent
b89aeda198
commit
9f5b7d50df
2 changed files with 68 additions and 21 deletions
|
|
@ -49,12 +49,18 @@ namespace MinecraftClient.ChatBots
|
||||||
[TomlInlineComment("$config.ChatBot.DiscordBridge.OwnersIds$")]
|
[TomlInlineComment("$config.ChatBot.DiscordBridge.OwnersIds$")]
|
||||||
public ulong[] OwnersIds = new[] { 978757810781323276UL };
|
public ulong[] OwnersIds = new[] { 978757810781323276UL };
|
||||||
|
|
||||||
|
[TomlInlineComment("$config.ChatBot.DiscordBridge.MessageSendTimeout$")]
|
||||||
|
public int Message_Send_Timeout = 3;
|
||||||
|
|
||||||
[TomlPrecedingComment("$config.ChatBot.DiscordBridge.Formats$")]
|
[TomlPrecedingComment("$config.ChatBot.DiscordBridge.Formats$")]
|
||||||
public string PrivateMessageFormat = "**[Private Message]** {username}: {message}";
|
public string PrivateMessageFormat = "**[Private Message]** {username}: {message}";
|
||||||
public string PublicMessageFormat = "{username}: {message}";
|
public string PublicMessageFormat = "{username}: {message}";
|
||||||
public string TeleportRequestMessageFormat = "A new Teleport Request from **{username}**!";
|
public string TeleportRequestMessageFormat = "A new Teleport Request from **{username}**!";
|
||||||
|
|
||||||
public void OnSettingUpdate() { }
|
public void OnSettingUpdate()
|
||||||
|
{
|
||||||
|
Message_Send_Timeout = Message_Send_Timeout <= 0 ? 3 : Message_Send_Timeout;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiscordBridge()
|
public DiscordBridge()
|
||||||
|
|
@ -82,13 +88,21 @@ namespace MinecraftClient.ChatBots
|
||||||
private void Disconnect()
|
private void Disconnect()
|
||||||
{
|
{
|
||||||
if (_client != null)
|
if (_client != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_channel != null)
|
if (_channel != null)
|
||||||
_client.SendMessageAsync(_channel, new DiscordEmbedBuilder
|
_client.SendMessageAsync(_channel, new DiscordEmbedBuilder
|
||||||
{
|
{
|
||||||
Description = Translations.TryGet("bot.DiscordBridge.disconnected"),
|
Description = Translations.TryGet("bot.DiscordBridge.disconnected"),
|
||||||
Color = new DiscordColor(0xFF0000)
|
Color = new DiscordColor(0xFF0000)
|
||||||
}).Wait();
|
}).Wait(Config.Message_Send_Timeout * 1000);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
|
||||||
|
LogDebugToConsole(e);
|
||||||
|
}
|
||||||
|
|
||||||
_client.DisconnectAsync().Wait();
|
_client.DisconnectAsync().Wait();
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
|
|
@ -192,7 +206,15 @@ namespace MinecraftClient.ChatBots
|
||||||
if (!CanSendMessages() || string.IsNullOrEmpty(message))
|
if (!CanSendMessages() || string.IsNullOrEmpty(message))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client!.SendMessageAsync(_channel, message).Wait();
|
try
|
||||||
|
{
|
||||||
|
_client!.SendMessageAsync(_channel, message).Wait(Config.Message_Send_Timeout * 1000);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
|
||||||
|
LogDebugToConsole(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(DiscordMessageBuilder builder)
|
public void SendMessage(DiscordMessageBuilder builder)
|
||||||
|
|
@ -200,7 +222,15 @@ namespace MinecraftClient.ChatBots
|
||||||
if (!CanSendMessages())
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client!.SendMessageAsync(_channel, builder).Wait();
|
try
|
||||||
|
{
|
||||||
|
_client!.SendMessageAsync(_channel, builder).Wait(Config.Message_Send_Timeout * 1000);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
|
||||||
|
LogDebugToConsole(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(DiscordEmbedBuilder embedBuilder)
|
public void SendMessage(DiscordEmbedBuilder embedBuilder)
|
||||||
|
|
@ -208,14 +238,23 @@ namespace MinecraftClient.ChatBots
|
||||||
if (!CanSendMessages())
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client!.SendMessageAsync(_channel, embedBuilder).Wait();
|
try
|
||||||
|
{
|
||||||
|
_client!.SendMessageAsync(_channel, embedBuilder).Wait(Config.Message_Send_Timeout * 1000);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
|
||||||
|
LogDebugToConsole(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void SendImage(string filePath, string? text = null)
|
public void SendImage(string filePath, string? text = null)
|
||||||
{
|
{
|
||||||
if (!CanSendMessages())
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||||
{
|
{
|
||||||
filePath = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
|
filePath = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
|
||||||
|
|
@ -226,7 +265,13 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
messageBuilder.WithFiles(new Dictionary<string, Stream>() { { $"attachment://{filePath}", fs } });
|
messageBuilder.WithFiles(new Dictionary<string, Stream>() { { $"attachment://{filePath}", fs } });
|
||||||
|
|
||||||
_client!.SendMessageAsync(_channel, messageBuilder).Wait();
|
_client!.SendMessageAsync(_channel, messageBuilder).Wait(Config.Message_Send_Timeout * 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
|
||||||
|
LogDebugToConsole(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -235,8 +280,7 @@ namespace MinecraftClient.ChatBots
|
||||||
if (!CanSendMessages())
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var messageBuilder = new DiscordMessageBuilder().WithFile(fileStream);
|
SendMessage(new DiscordMessageBuilder().WithFile(fileStream));
|
||||||
SendMessage(messageBuilder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CanSendMessages()
|
private bool CanSendMessages()
|
||||||
|
|
@ -356,11 +400,12 @@ namespace MinecraftClient.ChatBots
|
||||||
});
|
});
|
||||||
|
|
||||||
IsConnected = true;
|
IsConnected = true;
|
||||||
|
LogToConsole("§y§l§f" + Translations.TryGet("bot.DiscordBridge.connected"));
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
LogToConsole(Translations.TryGet("bot.DiscordBridge.unknown_error"));
|
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.unknown_error"));
|
||||||
LogToConsole(e);
|
LogToConsole(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -622,6 +622,7 @@ bot.DiscordBridge.missing_token=Please provide a valid token!
|
||||||
bot.DiscordBridge.guild_not_found=The provided guild/server with an id '{0}' has not been found!
|
bot.DiscordBridge.guild_not_found=The provided guild/server with an id '{0}' has not been found!
|
||||||
bot.DiscordBridge.channel_not_found=The provided channel with an id '{0}' has not been found!
|
bot.DiscordBridge.channel_not_found=The provided channel with an id '{0}' has not been found!
|
||||||
bot.DiscordBridge.unknown_error=An unknown error has occured!
|
bot.DiscordBridge.unknown_error=An unknown error has occured!
|
||||||
|
bot.DiscordBridge.canceled_sending=Sending message to Discord was canceled due an error occuring. For more info enable Debug.
|
||||||
bot.DiscordBridge.desc=This command allows you to specify in the which direction the messages will be relayed via the Discord Bridge chat bot.
|
bot.DiscordBridge.desc=This command allows you to specify in the which direction the messages will be relayed via the Discord Bridge chat bot.
|
||||||
bot.DiscordBridge.invalid_direction=Invalid direction provided! Avaliable directions: both|b, minecraft|mc, discord|dsc. Example: "dscbridge direction mc"
|
bot.DiscordBridge.invalid_direction=Invalid direction provided! Avaliable directions: both|b, minecraft|mc, discord|dsc. Example: "dscbridge direction mc"
|
||||||
bot.DiscordBridge.direction=Direction of the Discord Brdige has been switched to '{0}'!
|
bot.DiscordBridge.direction=Direction of the Discord Brdige has been switched to '{0}'!
|
||||||
|
|
@ -960,6 +961,7 @@ config.ChatBot.DiscordBridge.Token=Your Discord Bot token.
|
||||||
config.ChatBot.DiscordBridge.GuildId=The ID of a server/guild where you have invited the bot to.
|
config.ChatBot.DiscordBridge.GuildId=The ID of a server/guild where you have invited the bot to.
|
||||||
config.ChatBot.DiscordBridge.ChannelId=The ID of a channel where you want to interact with the MCC using the bot.
|
config.ChatBot.DiscordBridge.ChannelId=The ID of a channel where you want to interact with the MCC using the bot.
|
||||||
config.ChatBot.DiscordBridge.OwnersIds=A list of IDs of people you want to be able to interact with the MCC using the bot.
|
config.ChatBot.DiscordBridge.OwnersIds=A list of IDs of people you want to be able to interact with the MCC using the bot.
|
||||||
|
config.ChatBot.DiscordBridge.MessageSendTimeout=How long to wait (in seconds) if a message can not be sent to discord before canceling the task (minimum 1 second).
|
||||||
config.ChatBot.DiscordBridge.Formats=Message formats\n# Words wrapped with { and } are going to be replaced during the code execution, do not change them!\n# For example. {message} is going to be replace with an actual message, {username} will be replaced with an username, {timestamp} with the current time.\n# For Discord message formatting, check the following: https://bit.ly/3F8CUCm
|
config.ChatBot.DiscordBridge.Formats=Message formats\n# Words wrapped with { and } are going to be replaced during the code execution, do not change them!\n# For example. {message} is going to be replace with an actual message, {username} will be replaced with an username, {timestamp} with the current time.\n# For Discord message formatting, check the following: https://bit.ly/3F8CUCm
|
||||||
|
|
||||||
# ChatBot.Farmer
|
# ChatBot.Farmer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue