Merge branch 'master' into rpc

This commit is contained in:
Anon 2022-10-24 20:45:40 +00:00 committed by GitHub
commit 477da50fe0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 1230 additions and 116 deletions

View file

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Inventory;
using MinecraftClient.Mapping;
using Tomlet.Attributes;
@ -143,7 +145,8 @@ namespace MinecraftClient.ChatBots
private Entity? fishingBobber;
private Location LastPos = Location.Zero;
private DateTime CaughtTime = DateTime.Now;
private int fishItemCounter = 10;
private int fishItemCounter = 15;
private Dictionary<ItemType, uint> fishItemCnt = new();
private Entity fishItem = new(-1, EntityType.Item, Location.Zero);
private int counter = 0;
@ -182,7 +185,7 @@ namespace MinecraftClient.ChatBots
public string CommandHandler(string cmd, string[] args)
{
if (args.Length > 0)
if (args.Length >= 1)
{
switch (args[0])
{
@ -206,13 +209,47 @@ namespace MinecraftClient.ChatBots
}
StopFishing();
return Translations.Get("bot.autoFish.stop");
case "status":
if (args.Length >= 2)
{
if (args[1] == "clear")
{
fishItemCnt = new();
return Translations.Get("bot.autoFish.status_clear");
}
else
{
return GetCommandHelp("status");
}
}
else
{
if (fishItemCnt.Count == 0)
return Translations.Get("bot.autoFish.status_info");
List<KeyValuePair<ItemType, uint>> orderedList = fishItemCnt.OrderBy(x => x.Value).ToList();
int maxLen = orderedList[^1].Value.ToString().Length;
StringBuilder sb = new();
sb.Append(Translations.Get("bot.autoFish.status_info"));
foreach ((ItemType type, uint cnt) in orderedList)
{
sb.Append(Environment.NewLine);
string cntStr = cnt.ToString();
sb.Append(' ', maxLen - cntStr.Length).Append(cntStr);
sb.Append(" x ");
sb.Append(Item.GetTypeString(type));
}
return sb.ToString();
}
case "help":
return GetCommandHelp(args.Length >= 2 ? args[1] : "");
default:
return GetHelp();
}
}
else return GetHelp();
else
return GetHelp();
}
private void StartFishing()
@ -246,7 +283,7 @@ namespace MinecraftClient.ChatBots
isFishing = false;
state = FishingState.Stopping;
}
fishItemCounter = 10;
fishItemCounter = 15;
}
private void UseFishRod()
@ -259,8 +296,9 @@ namespace MinecraftClient.ChatBots
public override void Update()
{
if (fishItemCounter < 10)
if (fishItemCounter < 15)
++fishItemCounter;
lock (stateLock)
{
switch (state)
@ -283,7 +321,7 @@ namespace MinecraftClient.ChatBots
{
if (castTimeout < 6000)
castTimeout *= 2; // Exponential backoff
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.cast_timeout", castTimeout / 10.0));
LogToConsole(GetShortTimestamp() + ": " + Translations.Get("bot.autoFish.cast_timeout", castTimeout / 10.0));
counter = Settings.DoubleToTick(Config.Cast_Delay);
state = FishingState.WaitingToCast;
@ -292,7 +330,7 @@ namespace MinecraftClient.ChatBots
case FishingState.WaitingFishToBite:
if (++counter > Settings.DoubleToTick(Config.Fishing_Timeout))
{
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.fishing_timeout"));
LogToConsole(GetShortTimestamp() + ": " + Translations.Get("bot.autoFish.fishing_timeout"));
counter = Settings.DoubleToTick(Config.Cast_Delay);
state = FishingState.WaitingToCast;
@ -347,8 +385,8 @@ namespace MinecraftClient.ChatBots
public override void OnEntitySpawn(Entity entity)
{
if (fishItemCounter < 10 && entity.Type == EntityType.Item && Math.Abs(entity.Location.Y - LastPos.Y) < 2.0 &&
Math.Abs(entity.Location.X - LastPos.X) < 0.1 && Math.Abs(entity.Location.Z - LastPos.Z) < 0.1)
if (fishItemCounter < 15 && entity.Type == EntityType.Item && Math.Abs(entity.Location.Y - LastPos.Y) < 2.2 &&
Math.Abs(entity.Location.X - LastPos.X) < 0.12 && Math.Abs(entity.Location.Z - LastPos.Z) < 0.12)
{
if (Config.Log_Fish_Bobber)
LogToConsole(string.Format("Item ({0}) spawn at {1}, distance = {2:0.00}", entity.ID, entity.Location, entity.Location.Distance(LastPos)));
@ -359,9 +397,9 @@ namespace MinecraftClient.ChatBots
if (Config.Log_Fish_Bobber)
LogToConsole(string.Format("FishingBobber spawn at {0}, distance = {1:0.00}", entity.Location, GetCurrentLocation().Distance(entity.Location)));
fishItemCounter = 10;
fishItemCounter = 15;
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.throw"));
LogToConsole(GetShortTimestamp() + ": " + Translations.Get("bot.autoFish.throw"));
lock (stateLock)
{
fishingBobber = entity;
@ -377,7 +415,7 @@ namespace MinecraftClient.ChatBots
public override void OnEntityDespawn(Entity entity)
{
if (entity != null && entity.Type == EntityType.FishingBobber && entity.ID == fishingBobber!.ID)
if (entity != null && fishingBobber != null && entity.Type == EntityType.FishingBobber && entity.ID == fishingBobber!.ID)
{
if (Config.Log_Fish_Bobber)
LogToConsole(string.Format("FishingBobber despawn at {0}", entity.Location));
@ -431,10 +469,15 @@ namespace MinecraftClient.ChatBots
public override void OnEntityMetadata(Entity entity, Dictionary<int, object?> metadata)
{
if (fishItemCounter < 10 && entity.ID == fishItem.ID && metadata.TryGetValue(8, out object? item))
if (fishItemCounter < 15 && entity.ID == fishItem.ID && metadata.TryGetValue(8, out object? itemObj))
{
LogToConsole(Translations.Get("bot.autoFish.got", ((Item)item!).ToFullString()));
fishItemCounter = 10;
fishItemCounter = 15;
Item item = (Item)itemObj!;
LogToConsole(Translations.Get("bot.autoFish.got", item.ToFullString()));
if (fishItemCnt.ContainsKey(item.Type))
fishItemCnt[item.Type] += (uint)item.Count;
else
fishItemCnt.Add(item.Type, (uint)item.Count);
}
}
@ -471,10 +514,10 @@ namespace MinecraftClient.ChatBots
{
++fishCount;
if (Config.Enable_Move && Config.Movements.Length > 0)
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.caught_at",
LogToConsole(GetShortTimestamp() + ": " + Translations.Get("bot.autoFish.caught_at",
fishingBobber!.Location.X, fishingBobber!.Location.Y, fishingBobber!.Location.Z, fishCount));
else
LogToConsole(GetTimestamp() + ": " + Translations.Get("bot.autoFish.caught", fishCount));
LogToConsole(GetShortTimestamp() + ": " + Translations.Get("bot.autoFish.caught", fishCount));
lock (stateLock)
{
@ -580,7 +623,7 @@ namespace MinecraftClient.ChatBots
private static string GetHelp()
{
return Translations.Get("bot.autoFish.available_cmd", "start, stop, help");
return Translations.Get("bot.autoFish.available_cmd", "start, stop, status, help");
}
private string GetCommandHelp(string cmd)
@ -590,8 +633,9 @@ namespace MinecraftClient.ChatBots
#pragma warning disable format // @formatter:off
"start" => Translations.Get("bot.autoFish.help.start"),
"stop" => Translations.Get("bot.autoFish.help.stop"),
"status" => Translations.Get("bot.autoFish.help.status"),
"help" => Translations.Get("bot.autoFish.help.help"),
_ => GetHelp(),
_ => GetHelp(),
#pragma warning restore format // @formatter:on
};
}

View file

@ -0,0 +1,414 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.Logging;
using Tomlet.Attributes;
namespace MinecraftClient.ChatBots
{
public class DiscordBridge : ChatBot
{
private enum BridgeDirection
{
Both = 0,
Minecraft,
Discord
}
private static DiscordBridge? instance = null;
public bool IsConnected { get; private set; }
private DiscordClient? discordBotClient;
private DiscordChannel? discordChannel;
private BridgeDirection bridgeDirection = BridgeDirection.Both;
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
[NonSerialized]
private const string BotName = "DiscordBridge";
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;
[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()
{
Message_Send_Timeout = Message_Send_Timeout <= 0 ? 3 : Message_Send_Timeout;
}
}
public DiscordBridge()
{
instance = this;
}
public override void Initialize()
{
RegisterChatBotCommand("dscbridge", "bot.DiscordBridge.desc", "dscbridge direction <both|mc|discord>", OnDscCommand);
Task.Run(async () => await MainAsync());
}
~DiscordBridge()
{
Disconnect();
}
public override void OnUnload()
{
Disconnect();
}
private void Disconnect()
{
if (discordBotClient != null)
{
try
{
if (discordChannel != null)
discordBotClient.SendMessageAsync(discordChannel, 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);
}
discordBotClient.DisconnectAsync().Wait();
IsConnected = false;
}
}
public static DiscordBridge? GetInstance()
{
return instance;
}
private string OnDscCommand(string cmd, string[] args)
{
if (args.Length == 2)
{
if (args[0].ToLower().Equals("direction"))
{
string direction = args[1].ToLower().Trim();
string? bridgeName = "";
switch (direction)
{
case "b":
case "both":
bridgeName = "bot.DiscordBridge.direction.both";
bridgeDirection = BridgeDirection.Both;
break;
case "mc":
case "minecraft":
bridgeName = "bot.DiscordBridge.direction.minecraft";
bridgeDirection = BridgeDirection.Minecraft;
break;
case "d":
case "dcs":
case "discord":
bridgeName = "bot.DiscordBridge.direction.discord";
bridgeDirection = BridgeDirection.Discord;
break;
default:
return Translations.TryGet("bot.DiscordBridge.invalid_direction");
}
return Translations.TryGet("bot.DiscordBridge.direction", Translations.TryGet(bridgeName));
};
}
return "dscbridge direction <both|mc|discord>";
}
public override void GetText(string text)
{
if (!CanSendMessages())
return;
text = GetVerbatim(text).Trim();
// Stop the crash when an empty text is recived somehow
if (string.IsNullOrEmpty(text))
return;
string message = "";
string username = "";
bool teleportRequest = false;
if (IsPrivateMessage(text, ref message, ref username))
message = Config.PrivateMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsChatMessage(text, ref message, ref username))
message = Config.PublicMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsTeleportRequest(text, ref username))
{
message = Config.TeleportRequestMessageFormat.Replace("{username}", username).Replace("{timestamp}", GetTimestamp()).Trim();
teleportRequest = true;
}
else message = text;
if (teleportRequest)
{
var messageBuilder = new DiscordMessageBuilder()
.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")
});
SendMessage(messageBuilder);
return;
}
else SendMessage(message);
}
public void SendMessage(string message)
{
if (!CanSendMessages() || string.IsNullOrEmpty(message))
return;
try
{
discordBotClient!.SendMessageAsync(discordChannel, 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)
{
if (!CanSendMessages())
return;
try
{
discordBotClient!.SendMessageAsync(discordChannel, 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)
{
if (!CanSendMessages())
return;
try
{
discordBotClient!.SendMessageAsync(discordChannel, 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;
try
{
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);
messageBuilder.WithFiles(new Dictionary<string, Stream>() { { $"attachment://{filePath}", fs } });
discordBotClient!.SendMessageAsync(discordChannel, messageBuilder).Wait(Config.Message_Send_Timeout * 1000);
}
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.canceled_sending"));
LogDebugToConsole(e);
}
}
public void SendFile(FileStream fileStream)
{
if (!CanSendMessages())
return;
SendMessage(new DiscordMessageBuilder().WithFile(fileStream));
}
private bool CanSendMessages()
{
return discordBotClient == null || discordChannel == null || bridgeDirection == BridgeDirection.Minecraft ? false : true;
}
async Task MainAsync()
{
try
{
if (string.IsNullOrEmpty(Config.Token.Trim()))
{
LogToConsole(Translations.TryGet("bot.DiscordBridge.missing_token"));
UnloadBot();
return;
}
discordBotClient = new DiscordClient(new DiscordConfiguration()
{
Token = Config.Token.Trim(),
TokenType = TokenType.Bot,
AutoReconnect = true,
Intents = DiscordIntents.All,
MinimumLogLevel = Settings.Config.Logging.DebugMessages ?
(LogLevel.Trace | LogLevel.Information | LogLevel.Debug | LogLevel.Critical | LogLevel.Error | LogLevel.Warning) : LogLevel.None
});
try
{
await discordBotClient.GetGuildAsync(Config.GuildId);
}
catch (Exception e)
{
if (e is NotFoundException)
{
LogToConsole(Translations.TryGet("bot.DiscordBridge.guild_not_found", Config.GuildId));
UnloadBot();
return;
}
LogDebugToConsole("Exception when trying to find the guild:");
LogDebugToConsole(e);
}
try
{
discordChannel = await discordBotClient.GetChannelAsync(Config.ChannelId);
}
catch (Exception e)
{
if (e is NotFoundException)
{
LogToConsole(Translations.TryGet("bot.DiscordBridge.channel_not_found", Config.ChannelId));
UnloadBot();
return;
}
LogDebugToConsole("Exception when trying to find the channel:");
LogDebugToConsole(e);
}
discordBotClient.MessageCreated += async (source, e) =>
{
if (e.Guild.Id != Config.GuildId)
return;
if (e.Channel.Id != Config.ChannelId)
return;
if (!Config.OwnersIds.Contains(e.Author.Id))
return;
string message = e.Message.Content.Trim();
if (string.IsNullOrEmpty(message) || string.IsNullOrWhiteSpace(message))
return;
if (bridgeDirection == BridgeDirection.Discord)
{
if (!message.StartsWith(".dscbridge"))
return;
}
if (message.StartsWith("."))
{
message = message[1..];
await e.Message.CreateReactionAsync(DiscordEmoji.FromName(discordBotClient, ":gear:"));
string? result = "";
PerformInternalCommand(message, ref result);
result = string.IsNullOrEmpty(result) ? "-" : result;
await e.Message.DeleteOwnReactionAsync(DiscordEmoji.FromName(discordBotClient, ":gear:"));
await e.Message.CreateReactionAsync(DiscordEmoji.FromName(discordBotClient, ":white_check_mark:"));
await e.Message.RespondAsync($"{Translations.TryGet("bot.DiscordBridge.command_executed")}:\n```{result}```");
}
else SendText(message);
};
discordBotClient.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 discordBotClient.ConnectAsync();
await discordBotClient.SendMessageAsync(discordChannel, new DiscordEmbedBuilder
{
Description = Translations.TryGet("bot.DiscordBridge.connected"),
Color = new DiscordColor(0x00FF00)
});
IsConnected = true;
LogToConsole("§y§l§f" + Translations.TryGet("bot.DiscordBridge.connected"));
await Task.Delay(-1);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.DiscordBridge.unknown_error"));
LogToConsole(e);
return;
}
}
}
}

View file

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
using MinecraftClient.Mapping;
using Tomlet.Attributes;
@ -12,6 +15,12 @@ namespace MinecraftClient.ChatBots
{
public static Configs Config = new();
public struct QueuedMap
{
public string FileName;
public int MapId;
}
[TomlDoNotInlineObject]
public class Configs
{
@ -35,22 +44,45 @@ namespace MinecraftClient.ChatBots
[TomlInlineComment("$config.ChatBot.Map.Notify_On_First_Update$")]
public bool Notify_On_First_Update = true;
public void OnSettingUpdate() { }
[TomlInlineComment("$config.ChatBot.Map.Rasize_Rendered_Image$")]
public bool Rasize_Rendered_Image = false;
[TomlInlineComment("$config.ChatBot.Map.Resize_To$")]
public int Resize_To = 512;
[TomlPrecedingComment("$config.ChatBot.Map.Send_Rendered_To_Bridges$")]
public bool Send_Rendered_To_Discord = false;
public bool Send_Rendered_To_Telegram = false;
public void OnSettingUpdate()
{
if (Resize_To <= 0)
Resize_To = 128;
}
}
private readonly string baseDirectory = @"Rendered_Maps";
private readonly Dictionary<int, McMap> cachedMaps = new();
private readonly Queue<QueuedMap> discordQueue = new();
public override void Initialize()
{
if (!Directory.Exists(baseDirectory))
Directory.CreateDirectory(baseDirectory);
DeleteRenderedMaps();
RegisterChatBotCommand("maps", "bot.map.cmd.desc", "maps list|render <id> or maps l|r <id>", OnMapCommand);
}
public override void OnUnload()
{
DeleteRenderedMaps();
}
private void DeleteRenderedMaps()
{
if (Config.Delete_All_On_Unload)
{
@ -199,7 +231,93 @@ namespace MinecraftClient.ChatBots
}
}
file.Close();
LogToConsole(Translations.TryGet("bot.map.rendered", map.MapId, fileName));
if (Config.Rasize_Rendered_Image)
{
using (var image = new MagickImage(fileName))
{
var size = new MagickGeometry(Config.Resize_To, Config.Resize_To);
size.IgnoreAspectRatio = true;
image.Resize(size);
image.Write(fileName);
LogToConsole(Translations.TryGet("bot.map.resized_rendered_image", map.MapId, Config.Resize_To));
}
}
if (Config.Send_Rendered_To_Discord || Config.Send_Rendered_To_Telegram)
{
// We need to queue up images because Discord/Telegram Bridge is not ready immediatelly
if (DiscordBridge.Config.Enabled || TelegramBridge.Config.Enabled)
discordQueue.Enqueue(new QueuedMap { FileName = fileName, MapId = map.MapId });
}
}
public override void Update()
{
DiscordBridge? discordBridge = DiscordBridge.GetInstance();
TelegramBridge? telegramBridge = TelegramBridge.GetInstance();
if (Config.Send_Rendered_To_Discord)
{
if (discordBridge == null || (discordBridge != null && !discordBridge.IsConnected))
return;
}
if (Config.Send_Rendered_To_Telegram)
{
if (telegramBridge == null || (telegramBridge != null && !telegramBridge.IsConnected))
return;
}
if (discordQueue.Count > 0)
{
QueuedMap map = discordQueue.Dequeue();
string fileName = map.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))
{
image.Write(newFileName);
if (Config.Send_Rendered_To_Discord)
discordBridge!.SendImage(newFileName, $"> A render of the map with an id: **{map.MapId}**");
if (Config.Send_Rendered_To_Telegram)
telegramBridge!.SendImage(newFileName, $"A render of the map with an id: *{map.MapId}*");
newFileName = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + newFileName;
if (Config.Send_Rendered_To_Discord)
LogToConsole(Translations.TryGet("bot.map.sent_to_discord", map.MapId));
if (Config.Send_Rendered_To_Telegram)
LogToConsole(Translations.TryGet("bot.map.sent_to_telegram", map.MapId));
// Wait for 2 seconds and then try until file is free for deletion
// 10 seconds timeout
Task.Run(async () =>
{
await Task.Delay(2000);
var time = Stopwatch.StartNew();
while (time.ElapsedMilliseconds < 10000) // 10 seconds
{
try
{
// Delete the temporary file
if (File.Exists(newFileName))
File.Delete(newFileName);
}
catch (IOException e) { }
}
});
}
}
}
private void RenderInConsole(McMap map)
@ -389,8 +507,8 @@ namespace MinecraftClient.ChatBots
return new(
r: (byte)((Colors[baseColorId][0] * shadeMultiplier) / 255),
g: (byte)((Colors[baseColorId][1] * shadeMultiplier) / 255),
b: (byte)((Colors[baseColorId][2] * shadeMultiplier) / 255),
g: (byte)((Colors[baseColorId][1] * shadeMultiplier) / 255),
b: (byte)((Colors[baseColorId][2] * shadeMultiplier) / 255),
a: 255
);
}

View file

@ -158,7 +158,7 @@ namespace MinecraftClient.ChatBots
{
try
{
CSharpRunner.Run(this, lines, args, localVars);
CSharpRunner.Run(this, lines, args, localVars, scriptName: file!);
}
catch (CSharpException e)
{

View file

@ -0,0 +1,358 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.Entities;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
using Tomlet.Attributes;
using File = System.IO.File;
namespace MinecraftClient.ChatBots
{
public class TelegramBridge : ChatBot
{
private enum BridgeDirection
{
Both = 0,
Minecraft,
Telegram
}
private static TelegramBridge? instance = null;
public bool IsConnected { get; private set; }
private TelegramBotClient? botClient;
private CancellationTokenSource? cancellationToken;
private BridgeDirection bridgeDirection = BridgeDirection.Both;
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
[NonSerialized]
private const string BotName = "TelegramBridge";
public bool Enabled = false;
[TomlInlineComment("$config.ChatBot.TelegramBridge.Token$")]
public string Token = "your bot token here";
[TomlInlineComment("$config.ChatBot.TelegramBridge.ChannelId$")]
public string ChannelId = "";
[TomlInlineComment("$config.ChatBot.TelegramBridge.Authorized_Chat_Ids$")]
public long[] Authorized_Chat_Ids = Array.Empty<long>();
[TomlInlineComment("$config.ChatBot.TelegramBridge.MessageSendTimeout$")]
public int Message_Send_Timeout = 3;
[TomlPrecedingComment("$config.ChatBot.TelegramBridge.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()
{
Message_Send_Timeout = Message_Send_Timeout <= 0 ? 3 : Message_Send_Timeout;
}
}
public TelegramBridge()
{
instance = this;
}
public override void Initialize()
{
RegisterChatBotCommand("tgbridge", "bot.TelegramBridge.desc", "tgbridge direction <both|mc|telegram>", OnTgCommand);
Task.Run(async () => await MainAsync());
}
~TelegramBridge()
{
Disconnect();
}
public override void OnUnload()
{
Disconnect();
}
private void Disconnect()
{
if (botClient != null)
{
try
{
SendMessage(Translations.TryGet("bot.TelegramBridge.disconnected"));
cancellationToken?.Cancel();
botClient = null;
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.canceled_sending"));
LogDebugToConsole(e);
}
IsConnected = false;
}
}
public static TelegramBridge? GetInstance()
{
return instance;
}
private string OnTgCommand(string cmd, string[] args)
{
if (args.Length == 2)
{
if (args[0].ToLower().Equals("direction"))
{
string direction = args[1].ToLower().Trim();
string? bridgeName = "";
switch (direction)
{
case "b":
case "both":
bridgeName = "bot.TelegramBridge.direction.both";
bridgeDirection = BridgeDirection.Both;
break;
case "mc":
case "minecraft":
bridgeName = "bot.TelegramBridge.direction.minecraft";
bridgeDirection = BridgeDirection.Minecraft;
break;
case "t":
case "tg":
case "telegram":
bridgeName = "bot.TelegramBridge.direction.discord";
bridgeDirection = BridgeDirection.Telegram;
break;
default:
return Translations.TryGet("bot.TelegramBridge.invalid_direction");
}
return Translations.TryGet("bot.TelegramBridge.direction", Translations.TryGet(bridgeName));
};
}
return "dscbridge direction <both|mc|discord>";
}
public override void GetText(string text)
{
if (!CanSendMessages())
return;
text = GetVerbatim(text).Trim();
// Stop the crash when an empty text is recived somehow
if (string.IsNullOrEmpty(text))
return;
string message = "";
string username = "";
if (IsPrivateMessage(text, ref message, ref username))
message = Config.PrivateMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsChatMessage(text, ref message, ref username))
message = Config.PublicMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsTeleportRequest(text, ref username))
message = Config.TeleportRequestMessageFormat.Replace("{username}", username).Replace("{timestamp}", GetTimestamp()).Trim();
else message = text;
SendMessage(message);
}
public void SendMessage(string message)
{
if (!CanSendMessages() || string.IsNullOrEmpty(message))
return;
try
{
botClient!.SendTextMessageAsync(Config.ChannelId.Trim(), message, ParseMode.Markdown).Wait(Config.Message_Send_Timeout);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.canceled_sending"));
LogDebugToConsole(e);
}
}
public void SendImage(string filePath, string? text = null)
{
if (!CanSendMessages())
return;
try
{
string fileName = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
Stream stream = File.OpenRead(filePath);
botClient!.SendDocumentAsync(
Config.ChannelId.Trim(),
document: new InputOnlineFile(content: stream, fileName),
caption: text,
parseMode: ParseMode.Markdown).Wait(Config.Message_Send_Timeout * 1000);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.canceled_sending"));
LogDebugToConsole(e);
}
}
private bool CanSendMessages()
{
return botClient != null && !string.IsNullOrEmpty(Config.ChannelId.Trim()) && bridgeDirection != BridgeDirection.Minecraft;
}
async Task MainAsync()
{
try
{
if (string.IsNullOrEmpty(Config.Token.Trim()))
{
LogToConsole(Translations.TryGet("bot.TelegramBridge.missing_token"));
UnloadBot();
return;
}
if (string.IsNullOrEmpty(Config.ChannelId.Trim()))
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.missing_channel_id"));
botClient = new TelegramBotClient(Config.Token.Trim());
cancellationToken = new CancellationTokenSource();
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: new ReceiverOptions
{
// receive all update types
AllowedUpdates = Array.Empty<UpdateType>()
},
cancellationToken: cancellationToken.Token
);
IsConnected = true;
SendMessage("✅ " + Translations.TryGet("bot.TelegramBridge.connected"));
LogToConsole("§y§l§f" + Translations.TryGet("bot.TelegramBridge.connected"));
if (Config.Authorized_Chat_Ids.Length == 0)
{
SendMessage("⚠️ *" + Translations.TryGet("bot.TelegramBridge.missing_authorized_channels") + "* ⚠️");
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.missing_authorized_channels"));
return;
}
await Task.Delay(-1);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.unknown_error"));
LogToConsole(e);
return;
}
}
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken _cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
var chatId = message.Chat.Id;
var text = message.Text;
if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
return;
if (text.ToLower().Contains("/start"))
return;
if (text.ToLower().Contains(".chatid"))
{
await botClient.SendTextMessageAsync(chatId: chatId,
replyToMessageId: message.MessageId,
text: $"Chat ID: {chatId}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;
}
if (Config.Authorized_Chat_Ids.Length > 0 && !Config.Authorized_Chat_Ids.Contains(chatId))
{
LogDebugToConsole($"Unauthorized message '{messageText}' received in a chat with with an ID: {chatId} !");
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId: message.MessageId,
text: Translations.TryGet("bot.TelegramBridge.unauthorized"),
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;
}
LogDebugToConsole($"Received a '{messageText}' message in a chat with with an ID: {chatId} .");
if (bridgeDirection == BridgeDirection.Telegram)
{
if (!text.StartsWith(".dscbridge"))
return;
}
if (text.StartsWith("."))
{
var command = text[1..];
string? result = "";
PerformInternalCommand(command, ref result);
result = string.IsNullOrEmpty(result) ? "-" : result;
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId:
message.MessageId,
text: $"{Translations.TryGet("bot.TelegramBridge.command_executed")}:\n\n{result}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
}
else SendText(text);
}
private Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken _cancellationToken)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
LogToConsole("§w§l§f" + ErrorMessage);
return Task.CompletedTask;
}
}
}