From ec27ec53d784bcba2c3cc701d6aec6f0fa0f7908 Mon Sep 17 00:00:00 2001 From: Milutinke Date: Fri, 21 Oct 2022 02:29:47 +0200 Subject: [PATCH 1/3] Implemented the Discord Bridge Chat Bot. TODO: Translate --- MinecraftClient/ChatBots/DiscordBridge.cs | 218 ++++++++++++++++++++++ MinecraftClient/McClient.cs | 1 + MinecraftClient/MinecraftClient.csproj | 1 + MinecraftClient/Settings.cs | 9 +- 4 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 MinecraftClient/ChatBots/DiscordBridge.cs diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs new file mode 100644 index 00000000..9d3d329c --- /dev/null +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -0,0 +1,218 @@ +using DSharpPlus; +using System.Threading.Tasks; +using System; +using Tomlet.Attributes; +using System.Linq; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using Microsoft.Extensions.Logging; + +namespace MinecraftClient.ChatBots +{ + public class DiscordBridge : ChatBot + { + private DiscordClient? _client; + private DiscordChannel? _channel; + + public static Configs Config = new(); + + [TomlDoNotInlineObject] + public class Configs + { + [NonSerialized] + private const string BotName = "DiscordBridge"; + + public bool Enabled = false; + + public string Token = "your bot token here"; + + public ulong GuildId = 1018553894831403028L; + + public ulong ChannelId = 1018565295654326364L; + + public ulong[]? OwnerIds = new[] { 978757810781323276UL }; + + public bool IgnoreMessagesSentByBots = true; + } + + public override void Initialize() + { + Task.Run(async () => await MainAsync()); + } + + ~DiscordBridge() + { + Disconnect(); + } + + public override void OnUnload() + { + Disconnect(); + } + + private void Disconnect() + { + if (_client != null) + { + if (_channel != null) + _client.SendMessageAsync(_channel, new DiscordEmbedBuilder + { + Description = $"Disconnected from the MCC!", + Color = new DiscordColor(0xFF0000) + }).Wait(); + + _client.DisconnectAsync().Wait(); + } + } + + public override void GetText(string text) + { + if (_client == null || _channel == null) + return; + + text = GetVerbatim(text).Trim(); + + string message = ""; + string username = ""; + bool teleportRequest = false; + + if (IsPrivateMessage(text, ref message, ref username)) + message = $"**[Private Message]** {username}: `{message}`"; + else if (IsChatMessage(text, ref message, ref username)) + message = $"{username}: `{message}`"; + else if (IsTeleportRequest(text, ref username)) + { + message = $"A new Teleport Request from **{username}**"; + teleportRequest = true; + } + else message = text; + + SendMessageToDiscord(message, teleportRequest); + } + + private void SendMessageToDiscord(string message, bool teleportRequest = false) + { + if (_client == null || _channel == null) + return; + + if (teleportRequest) + { + var messageBuilder = new DiscordMessageBuilder() + .WithContent(message) + .AddComponents(new DiscordButtonComponent(ButtonStyle.Success, "accept_teleport", "Accept")) + .AddComponents(new DiscordButtonComponent(ButtonStyle.Danger, "deny_teleport", "Deny")); + + _client.SendMessageAsync(_channel, messageBuilder).Wait(); + return; + } + + _client.SendMessageAsync(_channel, message).Wait(); + } + + async Task MainAsync() + { + try + { + if (string.IsNullOrEmpty(Config.Token.Trim())) + { + LogToConsole(Translations.TryGet("bot.DiscordBridge.missing_token")); + UnloadBot(); + return; + } + + _client = new DiscordClient(new DiscordConfiguration() + { + Token = Config.Token.Trim(), + TokenType = TokenType.Bot, + AutoReconnect = true, + Intents = DiscordIntents.All, + MinimumLogLevel = LogLevel.None + }); + + try + { + await _client.GetGuildAsync(Config.GuildId); + } + catch (Exception e) + { + if (e is NotFoundException) + { + LogToConsole(Translations.TryGet("bot.DiscordBridge.guild_not_found", Config.GuildId)); + UnloadBot(); + return; + } + } + + try + { + _channel = await _client.GetChannelAsync(Config.ChannelId); + } + catch (Exception e) + { + if (e is NotFoundException) + { + LogToConsole(Translations.TryGet("bot.DiscordBridge.channel_not_found", Config.ChannelId)); + UnloadBot(); + return; + } + } + + _client.MessageCreated += async (source, e) => + { + if (Config.IgnoreMessagesSentByBots && e.Author.IsBot) + return; + + if (e.Guild.Id != Config.GuildId) + return; + + if (e.Channel.Id != Config.ChannelId) + return; + + if (!Config.OwnerIds!.Contains(e.Author.Id)) + return; + + string message = e.Message.Content.Trim(); + + if (message.StartsWith(".")) + { + await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":gear:")); + message = message[1..]; + string? result = ""; + PerformInternalCommand(message, ref result); + result = string.IsNullOrEmpty(result) ? "-" : result; + await e.Message.DeleteOwnReactionAsync(DiscordEmoji.FromName(_client, ":gear:")); + await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":white_check_mark:")); + await e.Message.RespondAsync($"The command was executed with the result:\n```{result}```"); + } + else SendText(message); + }; + + _client.ComponentInteractionCreated += async (s, e) => + { + if (!(e.Id.Equals("accept_teleport") || e.Id.Equals("deny_teleport"))) + return; + + string result = e.Id.Equals("accept_teleport") ? "Accepted :white_check_mark:" : "Denied :x:"; + SendText(e.Id.Equals("accept_teleport") ? "/tpaccept" : "/tpdeny"); + await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent(result)); + }; + + await _client.ConnectAsync(); + + await _client.SendMessageAsync(_channel, new DiscordEmbedBuilder + { + Description = $"Succesfully connected with the MCC!", + Color = new DiscordColor(0x00FF00) + }); + + await Task.Delay(-1); + } + catch (Exception e) + { + LogToConsole(Translations.TryGet("bot.DiscordBridge.unknown_error")); + LogToConsole(e); + return; + } + } + } +} diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 677cad72..f829745d 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -264,6 +264,7 @@ namespace MinecraftClient if (Config.ChatBot.AutoRelog.Enabled) { BotLoad(new AutoRelog()); } if (Config.ChatBot.AutoRespond.Enabled) { BotLoad(new AutoRespond()); } if (Config.ChatBot.ChatLog.Enabled) { BotLoad(new ChatLog()); } + if (Config.ChatBot.DiscordBridge.Enabled) { BotLoad(new DiscordBridge()); } if (Config.ChatBot.Farmer.Enabled) { BotLoad(new Farmer()); } if (Config.ChatBot.FollowPlayer.Enabled) { BotLoad(new FollowPlayer()); } if (Config.ChatBot.HangmanGame.Enabled) { BotLoad(new HangmanGame()); } diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index b7f45d16..67e98941 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -35,6 +35,7 @@ + diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 7880ead0..f536c53a 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -567,7 +567,7 @@ namespace MinecraftClient public enum ForgeConfigType { no, auto, force }; - public enum TerminalColorDepthType { bit_4, bit_8, bit_24}; + public enum TerminalColorDepthType { bit_4, bit_8, bit_24 }; } public struct AccountInfoConfig @@ -1090,6 +1090,13 @@ namespace MinecraftClient set { ChatBots.ChatLog.Config = value; ChatBots.ChatLog.Config.OnSettingUpdate(); } } + //[TomlPrecedingComment("$config.ChatBot.DiscordBridge")] + public ChatBots.DiscordBridge.Configs DiscordBridge + { + get { return ChatBots.DiscordBridge.Config; } + set { ChatBots.DiscordBridge.Config = value; } + } + [TomlPrecedingComment("$config.ChatBot.Farmer$")] public ChatBots.Farmer.Configs Farmer { From 148cb34878d606530c4236cadc1c0b396b8560a0 Mon Sep 17 00:00:00 2001 From: Milutinke Date: Fri, 21 Oct 2022 02:43:43 +0200 Subject: [PATCH 2/3] Removed unecessary check and made buttons for teleport request inline which looks way cleaner. --- MinecraftClient/ChatBots/DiscordBridge.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs index 9d3d329c..54a364a3 100644 --- a/MinecraftClient/ChatBots/DiscordBridge.cs +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -31,8 +31,6 @@ namespace MinecraftClient.ChatBots public ulong ChannelId = 1018565295654326364L; public ulong[]? OwnerIds = new[] { 978757810781323276UL }; - - public bool IgnoreMessagesSentByBots = true; } public override void Initialize() @@ -98,9 +96,15 @@ namespace MinecraftClient.ChatBots if (teleportRequest) { var messageBuilder = new DiscordMessageBuilder() - .WithContent(message) - .AddComponents(new DiscordButtonComponent(ButtonStyle.Success, "accept_teleport", "Accept")) - .AddComponents(new DiscordButtonComponent(ButtonStyle.Danger, "deny_teleport", "Deny")); + .WithEmbed(new DiscordEmbedBuilder + { + Description = message, + Color = new DiscordColor(0x3399FF) + }) + .AddComponents(new DiscordComponent[]{ + new DiscordButtonComponent(ButtonStyle.Success, "accept_teleport", "Accept"), + new DiscordButtonComponent(ButtonStyle.Danger, "deny_teleport", "Deny") + }); _client.SendMessageAsync(_channel, messageBuilder).Wait(); return; @@ -159,9 +163,6 @@ namespace MinecraftClient.ChatBots _client.MessageCreated += async (source, e) => { - if (Config.IgnoreMessagesSentByBots && e.Author.IsBot) - return; - if (e.Guild.Id != Config.GuildId) return; @@ -177,9 +178,11 @@ namespace MinecraftClient.ChatBots { await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":gear:")); message = message[1..]; + string? result = ""; PerformInternalCommand(message, ref result); result = string.IsNullOrEmpty(result) ? "-" : result; + await e.Message.DeleteOwnReactionAsync(DiscordEmoji.FromName(_client, ":gear:")); await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":white_check_mark:")); await e.Message.RespondAsync($"The command was executed with the result:\n```{result}```"); From 12b317644da4efdc2496585fbeb1b5028a61a9a5 Mon Sep 17 00:00:00 2001 From: Milutinke Date: Fri, 21 Oct 2022 12:25:33 +0200 Subject: [PATCH 3/3] Transalted and added detailed comments and instructions and a reference to the documentation article. Fully tested, ready for merge. --- MinecraftClient/ChatBots/DiscordBridge.cs | 25 +++++++++++++++-------- MinecraftClient/Resources/lang/en.ini | 16 +++++++++++++++ MinecraftClient/Settings.cs | 2 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs index 54a364a3..ca6d425a 100644 --- a/MinecraftClient/ChatBots/DiscordBridge.cs +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -24,13 +24,22 @@ namespace MinecraftClient.ChatBots public bool Enabled = false; + [TomlInlineComment("$config.ChatBot.DiscordBridge.Token$")] public string Token = "your bot token here"; + [TomlInlineComment("$config.ChatBot.DiscordBridge.GuildId$")] public ulong GuildId = 1018553894831403028L; + [TomlInlineComment("$config.ChatBot.DiscordBridge.ChannelId$")] public ulong ChannelId = 1018565295654326364L; - public ulong[]? OwnerIds = new[] { 978757810781323276UL }; + [TomlInlineComment("$config.ChatBot.DiscordBridge.OwnersIds$")] + public ulong[]? OwnersIds = new[] { 978757810781323276UL }; + + [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 override void Initialize() @@ -55,7 +64,7 @@ namespace MinecraftClient.ChatBots if (_channel != null) _client.SendMessageAsync(_channel, new DiscordEmbedBuilder { - Description = $"Disconnected from the MCC!", + Description = Translations.TryGet("bot.DiscordBridge.disconnected"), Color = new DiscordColor(0xFF0000) }).Wait(); @@ -75,12 +84,12 @@ namespace MinecraftClient.ChatBots bool teleportRequest = false; if (IsPrivateMessage(text, ref message, ref username)) - message = $"**[Private Message]** {username}: `{message}`"; + message = Config.PrivateMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim(); else if (IsChatMessage(text, ref message, ref username)) - message = $"{username}: `{message}`"; + message = Config.PublicMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim(); else if (IsTeleportRequest(text, ref username)) { - message = $"A new Teleport Request from **{username}**"; + message = Config.TeleportRequestMessageFormat.Replace("{username}", username).Replace("{timestamp}", GetTimestamp()).Trim(); teleportRequest = true; } else message = text; @@ -169,7 +178,7 @@ namespace MinecraftClient.ChatBots if (e.Channel.Id != Config.ChannelId) return; - if (!Config.OwnerIds!.Contains(e.Author.Id)) + if (!Config.OwnersIds!.Contains(e.Author.Id)) return; string message = e.Message.Content.Trim(); @@ -185,7 +194,7 @@ namespace MinecraftClient.ChatBots await e.Message.DeleteOwnReactionAsync(DiscordEmoji.FromName(_client, ":gear:")); await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":white_check_mark:")); - await e.Message.RespondAsync($"The command was executed with the result:\n```{result}```"); + await e.Message.RespondAsync($"{Translations.TryGet("bot.DiscordBridge.command_executed")}:\n```{result}```"); } else SendText(message); }; @@ -204,7 +213,7 @@ namespace MinecraftClient.ChatBots await _client.SendMessageAsync(_channel, new DiscordEmbedBuilder { - Description = $"Succesfully connected with the MCC!", + Description = Translations.TryGet("bot.DiscordBridge.connected"), Color = new DiscordColor(0x00FF00) }); diff --git a/MinecraftClient/Resources/lang/en.ini b/MinecraftClient/Resources/lang/en.ini index 3543b494..53318238 100644 --- a/MinecraftClient/Resources/lang/en.ini +++ b/MinecraftClient/Resources/lang/en.ini @@ -597,6 +597,14 @@ bot.autoRespond.match=match: {0}\nregex: {1}\naction: {2}\nactionPrivate: {3}\na # ChatLog bot.chatLog.invalid_file=Path '{0}' contains invalid characters. +# DiscordBridge +bot.DiscordBridge.command_executed=The command was executed with the result +bot.DiscordBridge.connected=Succesfully connected with MCC! +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! + # Mailer bot.mailer.init=Initializing Mailer with settings: bot.mailer.init.db= - Database File: {0} @@ -904,6 +912,14 @@ config.ChatBot.AutoRespond.Match_Colors=Do not remove colors from text (Note: Yo # ChatBot.ChatLog config.ChatBot.ChatLog=Logs chat messages in a file on disk. +# ChatBot.DiscordBridge +config.ChatBot.DiscordBridge=This bot allows you to send and recieve messages and commands via a Discord channel.\n# For Setup you can either use the documentation or read here (Documentation has images).\n# Documentation: https://mccteam.github.io/guide/chat-bots.html#discord-bridge\n# Setup:\n# First you need to create a Bot on the Discord Developers Portal, here is a video tutorial: https://www.youtube.com/watch?v=2FgMnZViNPA .\n# IMPORTANT: When creating a bot, you MUST ENABLE "Message Content Intent" and "Presence Intent" in order for bot to work! Also follow along carefully do not miss any steps!\n# When making a bot, copy the generated token and paste it here in "Token" field (tokens are important, keep them safe).\n# Copy the "Application ID" and go to: https://bit.ly/2Spn2Q3 .\n# Paste the id you have copied and check the "Administrator" field in permissions, then click on the link at the bottom.\n# This will open an invitation menu with your servers, choose the server you want to invite the bot on and invite him.\n# Once you've invited the bot, go to your Discord client and go to Settings -> Advanced and Enable "Developer Mode".\n# Exit the settings and right click on a server you have invited the bot to in the server list, then click "Copy ID", and paste the id here in "GuildId".\n# Then right click on a channel where you want to interact with the bot and again right click -> "Copy ID", pase the copied id here in "ChannelId".\n# And for the end, send a message in the channel, right click on your nick and again right click -> "Copy ID", then paste the id here in "OwnersIds".\n# How to use:\n# To execute an MCC command, prefix it with a dot ".", example: ".move 143 64 735" .\n# To send a message, simply type it out and hit enter. +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.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 config.ChatBot.Farmer=Automatically farms crops for you (plants, breaks and bonemeals them).\n# Crop types available: Beetroot, Carrot, Melon, Netherwart, Pumpkin, Potato, Wheat.\n# Usage: "/farmer start" command and "/farmer stop" command.\n# NOTE: This a newly added bot, it is not perfect and was only tested in 1.19.2, there are some minor issues like not being able to bonemeal carrots/potatoes sometimes.\n# or bot jumps onto the farm land and breaks it (this happens rarely but still happens). We are looking forward at improving this.\n# It is recommended to keep the farming area walled off and flat to avoid the bot jumping.\n# Also, if you have your farmland that is one block high, make it 2 or more blocks high so the bot does not fall through, as it can happen sometimes when the bot reconnects.\n# The bot also does not pickup all items if they fly off to the side, we have a plan to implement this option in the future as well as drop off and bonemeal refill chest(s). config.ChatBot.Farmer.Delay_Between_Tasks=Delay between tasks in seconds (Minimum 1 second) diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index f536c53a..fdae982b 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -1090,7 +1090,7 @@ namespace MinecraftClient set { ChatBots.ChatLog.Config = value; ChatBots.ChatLog.Config.OnSettingUpdate(); } } - //[TomlPrecedingComment("$config.ChatBot.DiscordBridge")] + [TomlPrecedingComment("$config.ChatBot.DiscordBridge$")] public ChatBots.DiscordBridge.Configs DiscordBridge { get { return ChatBots.DiscordBridge.Config; }