Another bug fix and added safeguards to the Discord Bridge chat bot.

Another bug fix and added safeguards to the Discord Bridge chat bot.
This commit is contained in:
Anon 2022-10-22 14:36:02 +00:00 committed by GitHub
commit 1a739eeab5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 21 deletions

View file

@ -49,12 +49,18 @@ namespace MinecraftClient.ChatBots
[TomlInlineComment("$config.ChatBot.DiscordBridge.OwnersIds$")]
public ulong[] OwnersIds = new[] { 978757810781323276UL };
[TomlInlineComment("$config.ChatBot.DiscordBridge.MessageSendTimeout$")]
public int Message_Send_Timeout = 3;
[TomlPrecedingComment("$config.ChatBot.DiscordBridge.Formats$")]
public string PrivateMessageFormat = "**[Private Message]** {username}: {message}";
public string PublicMessageFormat = "{username}: {message}";
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()
@ -83,12 +89,20 @@ namespace MinecraftClient.ChatBots
{
if (_client != null)
{
if (_channel != null)
_client.SendMessageAsync(_channel, new DiscordEmbedBuilder
{
Description = Translations.TryGet("bot.DiscordBridge.disconnected"),
Color = new DiscordColor(0xFF0000)
}).Wait();
try
{
if (_channel != null)
_client.SendMessageAsync(_channel, new DiscordEmbedBuilder
{
Description = Translations.TryGet("bot.DiscordBridge.disconnected"),
Color = new DiscordColor(0xFF0000)
}).Wait(Config.Message_Send_Timeout * 1000);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
LogDebugToConsole(e);
}
_client.DisconnectAsync().Wait();
IsConnected = false;
@ -192,7 +206,15 @@ namespace MinecraftClient.ChatBots
if (!CanSendMessages() || string.IsNullOrEmpty(message))
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)
@ -200,7 +222,15 @@ namespace MinecraftClient.ChatBots
if (!CanSendMessages())
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)
@ -208,25 +238,40 @@ namespace MinecraftClient.ChatBots
if (!CanSendMessages())
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)
{
if (!CanSendMessages())
return;
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
try
{
filePath = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
var messageBuilder = new DiscordMessageBuilder();
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
filePath = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
var messageBuilder = new DiscordMessageBuilder();
if (text != null)
messageBuilder.WithContent(text);
if (text != null)
messageBuilder.WithContent(text);
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())
return;
var messageBuilder = new DiscordMessageBuilder().WithFile(fileStream);
SendMessage(messageBuilder);
SendMessage(new DiscordMessageBuilder().WithFile(fileStream));
}
private bool CanSendMessages()
@ -356,11 +400,12 @@ namespace MinecraftClient.ChatBots
});
IsConnected = true;
LogToConsole("§y§l§f" + Translations.TryGet("bot.DiscordBridge.connected"));
await Task.Delay(-1);
}
catch (Exception e)
{
LogToConsole(Translations.TryGet("bot.DiscordBridge.unknown_error"));
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.unknown_error"));
LogToConsole(e);
return;
}

View file

@ -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.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.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.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}'!
@ -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.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.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
# ChatBot.Farmer