mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Added a command for switching a direction of the Discord Bridge.
This commit is contained in:
parent
c49544e0e5
commit
6851b8a96c
2 changed files with 80 additions and 11 deletions
|
|
@ -11,6 +11,13 @@ using Tomlet.Attributes;
|
||||||
|
|
||||||
namespace MinecraftClient.ChatBots
|
namespace MinecraftClient.ChatBots
|
||||||
{
|
{
|
||||||
|
internal enum BridgeDirection
|
||||||
|
{
|
||||||
|
Both = 0,
|
||||||
|
Minecraft,
|
||||||
|
Discord
|
||||||
|
}
|
||||||
|
|
||||||
public class DiscordBridge : ChatBot
|
public class DiscordBridge : ChatBot
|
||||||
{
|
{
|
||||||
private static DiscordBridge? instance = null;
|
private static DiscordBridge? instance = null;
|
||||||
|
|
@ -18,6 +25,7 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
private DiscordClient? _client;
|
private DiscordClient? _client;
|
||||||
private DiscordChannel? _channel;
|
private DiscordChannel? _channel;
|
||||||
|
private BridgeDirection bridgeDirection = BridgeDirection.Both;
|
||||||
|
|
||||||
public static Configs Config = new();
|
public static Configs Config = new();
|
||||||
|
|
||||||
|
|
@ -56,6 +64,8 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
|
RegisterChatBotCommand("dscbridge", "bot.DiscordBridge.desc", "dscbridge direction <both|mc|discord>", OnDscCommand);
|
||||||
|
|
||||||
Task.Run(async () => await MainAsync());
|
Task.Run(async () => await MainAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,9 +100,51 @@ namespace MinecraftClient.ChatBots
|
||||||
return instance;
|
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)
|
public override void GetText(string text)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
text = GetVerbatim(text).Trim();
|
text = GetVerbatim(text).Trim();
|
||||||
|
|
@ -133,31 +185,31 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
public void SendMessage(string message)
|
public void SendMessage(string message)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client.SendMessageAsync(_channel, message).Wait();
|
_client!.SendMessageAsync(_channel, message).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(DiscordMessageBuilder builder)
|
public void SendMessage(DiscordMessageBuilder builder)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client.SendMessageAsync(_channel, builder).Wait();
|
_client!.SendMessageAsync(_channel, builder).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(DiscordEmbedBuilder embedBuilder)
|
public void SendMessage(DiscordEmbedBuilder embedBuilder)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_client.SendMessageAsync(_channel, embedBuilder).Wait();
|
_client!.SendMessageAsync(_channel, embedBuilder).Wait();
|
||||||
|
|
||||||
}
|
}
|
||||||
public void SendImage(string filePath, string? text = null)
|
public void SendImage(string filePath, string? text = null)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||||
|
|
@ -167,19 +219,24 @@ 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendFile(FileStream fileStream)
|
public void SendFile(FileStream fileStream)
|
||||||
{
|
{
|
||||||
if (_client == null || _channel == null)
|
if (!CanSendMessages())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var messageBuilder = new DiscordMessageBuilder().WithFile(fileStream);
|
var messageBuilder = new DiscordMessageBuilder().WithFile(fileStream);
|
||||||
SendMessage(messageBuilder);
|
SendMessage(messageBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CanSendMessages()
|
||||||
|
{
|
||||||
|
return _client == null || _channel == null || bridgeDirection == BridgeDirection.Minecraft ? false : true;
|
||||||
|
}
|
||||||
|
|
||||||
async Task MainAsync()
|
async Task MainAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -248,10 +305,16 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
string message = e.Message.Content.Trim();
|
string message = e.Message.Content.Trim();
|
||||||
|
|
||||||
|
if (bridgeDirection == BridgeDirection.Discord)
|
||||||
|
{
|
||||||
|
if (!message.StartsWith(".dscbridge"))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (message.StartsWith("."))
|
if (message.StartsWith("."))
|
||||||
{
|
{
|
||||||
await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":gear:"));
|
|
||||||
message = message[1..];
|
message = message[1..];
|
||||||
|
await e.Message.CreateReactionAsync(DiscordEmoji.FromName(_client, ":gear:"));
|
||||||
|
|
||||||
string? result = "";
|
string? result = "";
|
||||||
PerformInternalCommand(message, ref result);
|
PerformInternalCommand(message, ref result);
|
||||||
|
|
|
||||||
|
|
@ -622,6 +622,12 @@ 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.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}'!
|
||||||
|
bot.DiscordBridge.direction.both=Both
|
||||||
|
bot.DiscordBridge.direction.minecraft=Minecraft
|
||||||
|
bot.DiscordBridge.direction.discord=Discord
|
||||||
|
|
||||||
# Farmer
|
# Farmer
|
||||||
botname.Farmer=Farmer
|
botname.Farmer=Farmer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue