Compare commits
No commits in common. "main" and "routing" have entirely different histories.
@ -6,7 +6,6 @@ MinimumVisualStudioVersion = 10.0.40219.1
|
|||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE65C879-794A-4695-B659-7376FE7DB5E3}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE65C879-794A-4695-B659-7376FE7DB5E3}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
.gitignore = .gitignore
|
.gitignore = .gitignore
|
||||||
build.py = build.py
|
|
||||||
MinecraftDiscordBot\bin\Debug\net6.0\config.json = MinecraftDiscordBot\bin\Debug\net6.0\config.json
|
MinecraftDiscordBot\bin\Debug\net6.0\config.json = MinecraftDiscordBot\bin\Debug\net6.0\config.json
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
using System.Security.Cryptography;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public class AesCipher : ICipher {
|
|
||||||
private readonly byte[] key;
|
|
||||||
private readonly byte[] iv;
|
|
||||||
|
|
||||||
public AesCipher() {
|
|
||||||
using var aes = Aes.Create();
|
|
||||||
aes.GenerateKey();
|
|
||||||
aes.GenerateIV();
|
|
||||||
key = aes.Key;
|
|
||||||
iv = aes.IV;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] plain) {
|
|
||||||
using var aes = Aes.Create();
|
|
||||||
aes.Key = key;
|
|
||||||
aes.IV = iv;
|
|
||||||
var transformer = aes.CreateEncryptor();
|
|
||||||
using var ms = new MemoryStream();
|
|
||||||
using (var cs = new CryptoStream(ms, transformer, CryptoStreamMode.Write))
|
|
||||||
cs.Write(plain);
|
|
||||||
return ms.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Decrypt(byte[] cipher) {
|
|
||||||
using Aes aes = Aes.Create();
|
|
||||||
aes.Key = key;
|
|
||||||
aes.IV = iv;
|
|
||||||
var transformer = aes.CreateDecryptor();
|
|
||||||
using MemoryStream ms = new MemoryStream(cipher);
|
|
||||||
using CryptoStream cs = new CryptoStream(ms, transformer, CryptoStreamMode.Read);
|
|
||||||
using MemoryStream os = new MemoryStream();
|
|
||||||
cs.CopyTo(os);
|
|
||||||
return os.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ICipher {
|
|
||||||
byte[] Decrypt(byte[] cipher);
|
|
||||||
byte[] Encrypt(byte[] plain);
|
|
||||||
}
|
|
@ -11,11 +11,8 @@ public class BotConfiguration : IBotConfiguration, IBotConfigurator {
|
|||||||
[JsonProperty("token", Required = Required.Always)]
|
[JsonProperty("token", Required = Required.Always)]
|
||||||
[Option('t', "token", HelpText = "The Discord bot token", Required = true)]
|
[Option('t', "token", HelpText = "The Discord bot token", Required = true)]
|
||||||
public string Token { get; init; } = default!;
|
public string Token { get; init; } = default!;
|
||||||
[JsonProperty("address", Required = Required.Always)]
|
|
||||||
[Option('a', "address", HelpText = "The connection string for the websocket", Required = true)]
|
|
||||||
public string Address { get; init; } = default!;
|
|
||||||
[JsonProperty("port", Required = Required.DisallowNull)]
|
[JsonProperty("port", Required = Required.DisallowNull)]
|
||||||
[Option('p', "port", Default = DEFAULT_PORT, HelpText = "The websocket server listen port")]
|
[Option('p', "port", Default = DEFAULT_PORT, HelpText = "The websocket server port")]
|
||||||
public int Port { get; init; } = DEFAULT_PORT;
|
public int Port { get; init; } = DEFAULT_PORT;
|
||||||
[JsonProperty("channels", Required = Required.Always)]
|
[JsonProperty("channels", Required = Required.Always)]
|
||||||
[Option('c', "channel", HelpText = "The list of whitelisted channels", Required = true, Min = 1)]
|
[Option('c', "channel", HelpText = "The list of whitelisted channels", Required = true, Min = 1)]
|
||||||
@ -23,15 +20,6 @@ public class BotConfiguration : IBotConfiguration, IBotConfigurator {
|
|||||||
[JsonProperty("prefix", Required = Required.DisallowNull)]
|
[JsonProperty("prefix", Required = Required.DisallowNull)]
|
||||||
[Option("prefix", Default = DEFAULT_PREFIX, HelpText = "The Discord bot command prefix")]
|
[Option("prefix", Default = DEFAULT_PREFIX, HelpText = "The Discord bot command prefix")]
|
||||||
public string Prefix { get; init; } = DEFAULT_PREFIX;
|
public string Prefix { get; init; } = DEFAULT_PREFIX;
|
||||||
[JsonProperty("host", Required = Required.Always)]
|
|
||||||
[Option("host", Default = DEFAULT_PREFIX, HelpText = "The external websocket hostname.", Required = true)]
|
|
||||||
public string SocketHost { get; init; } = default!;
|
|
||||||
[JsonProperty("admins", Required = Required.DisallowNull)]
|
|
||||||
[Option("admins", Default = new ulong[] { }, HelpText = "The list of bot administrators.")]
|
|
||||||
public ulong[] Administrators { get; init; } = Array.Empty<ulong>();
|
|
||||||
[JsonProperty("logchannel", Required = Required.DisallowNull)]
|
|
||||||
[Option("logchannel", Default = null, HelpText = "Optionally the id of a channel to mirror log to.")]
|
|
||||||
public ulong? LogChannel { get; init; } = null;
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public BotConfiguration Config => this;
|
public BotConfiguration Config => this;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
using MinecraftDiscordBot.Models;
|
namespace MinecraftDiscordBot;
|
||||||
using MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public class ChunkWaiter<T> : IChunkWaiter {
|
public class ChunkWaiter<T> : IChunkWaiter {
|
||||||
public int ID { get; }
|
public int ID { get; }
|
||||||
@ -18,7 +15,7 @@ public class ChunkWaiter<T> : IChunkWaiter {
|
|||||||
public bool IsCancellationRequested => _ct.IsCancellationRequested;
|
public bool IsCancellationRequested => _ct.IsCancellationRequested;
|
||||||
private string?[]? _chunks = null;
|
private string?[]? _chunks = null;
|
||||||
private int _receivedChunks = 0;
|
private int _receivedChunks = 0;
|
||||||
private ResultState? _state = null;
|
private bool _success = true;
|
||||||
private readonly object _syncRoot = new();
|
private readonly object _syncRoot = new();
|
||||||
public void AddChunk(int chunkId, int totalChunks, string value) {
|
public void AddChunk(int chunkId, int totalChunks, string value) {
|
||||||
lock (_syncRoot) {
|
lock (_syncRoot) {
|
||||||
@ -38,15 +35,9 @@ public class ChunkWaiter<T> : IChunkWaiter {
|
|||||||
}
|
}
|
||||||
private void FinalizeResult(string?[] _chunks) {
|
private void FinalizeResult(string?[] _chunks) {
|
||||||
var resultString = string.Concat(_chunks);
|
var resultString = string.Concat(_chunks);
|
||||||
switch (_state) {
|
if (_success) tcs.SetResult(resultParser(resultString));
|
||||||
case ResultState.Successful: tcs.SetResult(resultParser(resultString)); break;
|
else tcs.SetException(new ReplyException(resultString));
|
||||||
case ResultState.Unsuccessful: tcs.SetException(new ReplyException(resultString)); break;
|
|
||||||
case ResultState.Fatal: tcs.SetException(new InvalidProgramException($"Client script failed: {resultString}")); break;
|
|
||||||
default: throw new InvalidProgramException($"Program cannot handle result state '{_state}'!");
|
|
||||||
}
|
|
||||||
Finished = true;
|
Finished = true;
|
||||||
}
|
}
|
||||||
public void SetResultState(ResultState state) => _state = _state is ResultState oldState && state != oldState
|
public void SetUnsuccessful() => _success = false;
|
||||||
? throw new InvalidOperationException("Cannot set two different result states for same message!")
|
|
||||||
: state;
|
|
||||||
}
|
}
|
||||||
|
@ -1,269 +0,0 @@
|
|||||||
local secretToken = "$TOKEN"
|
|
||||||
local connectionUri = "$HOST"
|
|
||||||
local waitSeconds = 5
|
|
||||||
-- https://github.com/cc-tweaked/CC-Tweaked/blob/9cf70b10effeeed23e0e9c537bbbe0b2ff0d1a0f/src/main/java/dan200/computercraft/core/apis/http/options/AddressRule.java#L29
|
|
||||||
-- Chunk size must be less than packet size (type reply, success, chunkids, content: chunk) 16 kb for buffer
|
|
||||||
local maxMessageSize = (128 - 16) * 1024
|
|
||||||
|
|
||||||
local function chunkString(value, chunkSize)
|
|
||||||
if not chunkSize then chunkSize = maxMessageSize end
|
|
||||||
local length = value:len()
|
|
||||||
local total = math.ceil(length / chunkSize)
|
|
||||||
local chunks = {}
|
|
||||||
if length == 0 then
|
|
||||||
total = 1
|
|
||||||
chunks[1] = ""
|
|
||||||
else
|
|
||||||
local i = 1
|
|
||||||
for i=1,total do
|
|
||||||
local pos = 1 + ((i - 1) * chunkSize)
|
|
||||||
chunks[i] = value:sub(pos, pos + chunkSize - 1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return total, chunks
|
|
||||||
end
|
|
||||||
|
|
||||||
local function sendJson(socket, message)
|
|
||||||
return socket.send(textutils.serializeJSON(message))
|
|
||||||
end
|
|
||||||
|
|
||||||
local function sendResponse(socket, id, result, success)
|
|
||||||
if success == nil then success = 0 end
|
|
||||||
|
|
||||||
local total, chunks = chunkString(result)
|
|
||||||
for i, chunk in pairs(chunks) do
|
|
||||||
sendJson(socket, { type = "reply", id = id, result = chunk, chunk = i, total = total, success = success })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- error: no rs system
|
|
||||||
-- return rssystem rs
|
|
||||||
local function getPeripheral(name)
|
|
||||||
local dev = peripheral.find(name)
|
|
||||||
if not dev then error({message = "No peripheral '"..name.."' attached to the computer!"}) end
|
|
||||||
return dev
|
|
||||||
end
|
|
||||||
|
|
||||||
local function runRsCommand(params)
|
|
||||||
local script, reason = loadstring("local rs = peripheral.find(\"rsBridge\") if not rs then error({message = \"RS Bridge is not attached!\"}) end return rs."..params.command)
|
|
||||||
if not script then error({message = "Invalid command: "..reason.."!"}) end
|
|
||||||
local result = table.pack(pcall(script))
|
|
||||||
local success = result[1]
|
|
||||||
if not success then error({message = "Command execution failed: "..result[2].."!"}) end
|
|
||||||
|
|
||||||
local retvals = {}
|
|
||||||
retvals.n = result.n - 1
|
|
||||||
for i=1,retvals.n do retvals[tostring(i)] = result[i + 1] end
|
|
||||||
return textutils.serializeJSON(retvals)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getPeripheralInfo(side)
|
|
||||||
return {type = peripheral.getType(side), methods = peripheral.getMethods(side), side = side}
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getPeripheralList()
|
|
||||||
local pers = {}
|
|
||||||
for i,side in pairs(peripheral.getNames()) do pers[side] = getPeripheralInfo(side) end
|
|
||||||
return pers
|
|
||||||
end
|
|
||||||
|
|
||||||
-- error: any error during execution
|
|
||||||
-- return string result
|
|
||||||
local function getResponse(parsed)
|
|
||||||
if parsed.method == "energyusage" then
|
|
||||||
return tostring(getPeripheral("rsBridge").getEnergyUsage())
|
|
||||||
elseif parsed.method == "energystorage" then
|
|
||||||
return tostring(getPeripheral("rsBridge").getEnergyStorage())
|
|
||||||
elseif parsed.method == "listitems" then
|
|
||||||
return textutils.serializeJSON(getPeripheral("rsBridge").listItems())
|
|
||||||
elseif parsed.method == "listfluids" then
|
|
||||||
return textutils.serializeJSON(getPeripheral("rsBridge").listFluids())
|
|
||||||
elseif parsed.method == "craft" then
|
|
||||||
return tostring(getPeripheral("rsBridge").craftItem(parsed.params))
|
|
||||||
elseif parsed.method == "getitem" then
|
|
||||||
local item = getPeripheral("rsBridge").getItem(parsed.params)
|
|
||||||
if not item then error({message = "Requested item not found!"}) end
|
|
||||||
return textutils.serializeJSON(item)
|
|
||||||
elseif parsed.method == "command" then
|
|
||||||
return runRsCommand(parsed.params)
|
|
||||||
elseif parsed.method == "peripherals" then
|
|
||||||
return textutils.serializeJSON(getPeripheralList())
|
|
||||||
elseif parsed.method == "getonline" then
|
|
||||||
return textutils.serializeJSON(getPeripheral("playerDetector").getOnlinePlayers())
|
|
||||||
elseif parsed.method == "whereis" then
|
|
||||||
local pos = getPeripheral("playerDetector").getPlayerPos(parsed.params.username)
|
|
||||||
if not pos then return "null" end
|
|
||||||
return textutils.serializeJSON(pos)
|
|
||||||
elseif parsed.method == "send" then
|
|
||||||
if not parsed.params.username then
|
|
||||||
getPeripheral("chatBox").sendMessage(parsed.params.message, parsed.params.prefix)
|
|
||||||
else
|
|
||||||
getPeripheral("chatBox").sendMessageToPlayer(parsed.params.message, parsed.params.username, parsed.params.prefix)
|
|
||||||
end
|
|
||||||
return "true"
|
|
||||||
end
|
|
||||||
|
|
||||||
error({message = "No message handler for method: "..parsed.method.."!"})
|
|
||||||
end
|
|
||||||
|
|
||||||
local function logJSON(json, prefix)
|
|
||||||
if not prefix then prefix = "" end
|
|
||||||
for k,v in pairs(json) do
|
|
||||||
local key = prefix..k
|
|
||||||
if type(v) == "table" then
|
|
||||||
logJSON(v, key..".")
|
|
||||||
else
|
|
||||||
print(key, "=", textutils.serializeJSON(v))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- return bool success
|
|
||||||
local function handleMessage(socket, message)
|
|
||||||
local parsed, reason = textutils.unserializeJSON(message)
|
|
||||||
if not parsed then
|
|
||||||
print("Received message:", message)
|
|
||||||
printError("Message could not be parsed:", reason)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
pcall(function() print("Received JSON:") logJSON(parsed) end)
|
|
||||||
|
|
||||||
if parsed.type == "request" then
|
|
||||||
local success, result = pcall(function() return getResponse(parsed) end)
|
|
||||||
if not success then
|
|
||||||
if not result.message then
|
|
||||||
sendResponse(socket, parsed.id, result, 2)
|
|
||||||
else
|
|
||||||
sendResponse(socket, parsed.id, result.message, 1)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
sendResponse(socket, parsed.id, result, 0)
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
printError("Invalid message type:", parsed.type)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local function responder(socket)
|
|
||||||
while true do
|
|
||||||
local message, binary = socket.receive()
|
|
||||||
if not not message and not binary then
|
|
||||||
if message == "outdated" then
|
|
||||||
printError("Current script is outdated! Please update from the host!")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
handleMessage(socket, message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function termWaiter()
|
|
||||||
os.pullEvent("terminate")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function chatEventListener(socket)
|
|
||||||
while true do
|
|
||||||
event, username, message, uuid, hidden = os.pullEvent("chat")
|
|
||||||
sendJson(socket, {type = "chat", username = username, message = message, uuid = uuid, hidden = hidden})
|
|
||||||
print("Chat event relayed!")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function peripheralDetachEventListener(socket)
|
|
||||||
while true do
|
|
||||||
event, side = os.pullEvent("peripheral_detach")
|
|
||||||
sendJson(socket, {type = "peripheral_detach", side = side})
|
|
||||||
print("Peripheral was detached!")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function peripheralAttachEventListener(socket)
|
|
||||||
while true do
|
|
||||||
event, side = os.pullEvent("peripheral")
|
|
||||||
sendJson(socket, {type = "peripheral", peripheral = getPeripheralInfo(side) })
|
|
||||||
print("Peripheral was attached!")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function listAsSet(list)
|
|
||||||
local asSet = {}
|
|
||||||
for i,elem in pairs(list) do asSet[elem] = true end
|
|
||||||
return asSet
|
|
||||||
end
|
|
||||||
|
|
||||||
local function joinSets(a, b)
|
|
||||||
local joined = {}
|
|
||||||
for elem,exists in pairs(a) do if exists then joined[elem] = true end end
|
|
||||||
for elem,exists in pairs(b) do if exists then joined[elem] = true end end
|
|
||||||
return joined
|
|
||||||
end
|
|
||||||
|
|
||||||
local function playerStatusEventListener(socket)
|
|
||||||
local players = {}
|
|
||||||
while true do
|
|
||||||
local pd = peripheral.find("playerDetector")
|
|
||||||
if not not pd then
|
|
||||||
players = listAsSet(pd.getOnlinePlayers())
|
|
||||||
break
|
|
||||||
end
|
|
||||||
printError("playerDetector not connected!")
|
|
||||||
sleep(5)
|
|
||||||
end
|
|
||||||
while true do
|
|
||||||
local pd = peripheral.find("playerDetector")
|
|
||||||
if not not pd then
|
|
||||||
local newPlayers = listAsSet(pd.getOnlinePlayers())
|
|
||||||
for player,_ in pairs(joinSets(players, newPlayers)) do
|
|
||||||
if players[player] and (not newPlayers[player]) then
|
|
||||||
sendJson(socket, {type = "playerstatus", player = player, status = false})
|
|
||||||
elseif (not players[player]) and newPlayers[player] then
|
|
||||||
sendJson(socket, {type = "playerstatus", player = player, status = true})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
players = newPlayers
|
|
||||||
end
|
|
||||||
sleep(1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function eventListeners(socket)
|
|
||||||
parallel.waitForAny(
|
|
||||||
termWaiter,
|
|
||||||
function() chatEventListener(socket) end,
|
|
||||||
function() playerStatusEventListener(socket) end,
|
|
||||||
function() peripheralDetachEventListener(socket) end,
|
|
||||||
function() peripheralAttachEventListener(socket) end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function socketClient()
|
|
||||||
print("Connecting to the socket server at "..connectionUri.."...")
|
|
||||||
local socket, reason = http.websocket(connectionUri)
|
|
||||||
if not socket then error("Socket server could not be reached: "..reason) end
|
|
||||||
print("Connection successful!")
|
|
||||||
|
|
||||||
socket.send("login="..secretToken)
|
|
||||||
parallel.waitForAny(
|
|
||||||
function() responder(socket) end,
|
|
||||||
function() eventListeners(socket) end
|
|
||||||
)
|
|
||||||
socket.close()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function main()
|
|
||||||
while true do
|
|
||||||
local status, error = pcall(socketClient)
|
|
||||||
if status then break end
|
|
||||||
printError("An uncaught exception was raised:", error)
|
|
||||||
printError("Restarting in", waitSeconds, "seconds...")
|
|
||||||
sleep(waitSeconds)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local oldPullEvent = os.pullEvent
|
|
||||||
os.pullEvent = os.pullEventRaw
|
|
||||||
pcall(main)
|
|
||||||
os.pullEvent = oldPullEvent
|
|
@ -1,15 +1,12 @@
|
|||||||
using Discord.WebSocket;
|
using Discord;
|
||||||
using MinecraftDiscordBot.Models;
|
using Discord.WebSocket;
|
||||||
using MinecraftDiscordBot.Services;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Commands;
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
||||||
public record struct HandlerStruct(HandleCommandDelegate<ResponseType> Delegate, CommandHandlerAttribute Attribute);
|
|
||||||
private readonly Dictionary<string, HandlerStruct> _handlers = new();
|
private readonly Dictionary<string, HandlerStruct> _handlers = new();
|
||||||
|
|
||||||
public abstract string HelpTextPrefix { get; }
|
public abstract string HelpTextPrefix { get; }
|
||||||
public CommandRouter() {
|
public CommandRouter() {
|
||||||
foreach (var method in GetType().GetMethods())
|
foreach (var method in GetType().GetMethods())
|
||||||
@ -27,7 +24,7 @@ public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
|||||||
=> Task.FromResult(ResponseType.AsString(GenerateHelp()));
|
=> Task.FromResult(ResponseType.AsString(GenerateHelp()));
|
||||||
private static CommandHandlerAttribute? GetHandlerAttribute(MethodInfo method)
|
private static CommandHandlerAttribute? GetHandlerAttribute(MethodInfo method)
|
||||||
=> method.GetCustomAttributes(typeof(CommandHandlerAttribute), true).OfType<CommandHandlerAttribute>().FirstOrDefault();
|
=> method.GetCustomAttributes(typeof(CommandHandlerAttribute), true).OfType<CommandHandlerAttribute>().FirstOrDefault();
|
||||||
public virtual Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct) => GetHelpText(message, Array.Empty<string>(), ct);
|
public abstract Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct);
|
||||||
public abstract Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct);
|
public abstract Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct);
|
||||||
public Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
public Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||||
=> parameters is { Length: 0 }
|
=> parameters is { Length: 0 }
|
||||||
@ -46,3 +43,5 @@ public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
|||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record struct HandlerStruct(HandleCommandDelegate<ResponseType> Delegate, CommandHandlerAttribute Attribute);
|
@ -1,8 +0,0 @@
|
|||||||
namespace MinecraftDiscordBot.Commands;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
|
||||||
public sealed class CommandHandlerAttribute : Attribute {
|
|
||||||
public CommandHandlerAttribute(string commandName) => CommandName = commandName;
|
|
||||||
public string CommandName { get; }
|
|
||||||
public string? HelpText { get; init; }
|
|
||||||
}
|
|
273
MinecraftDiscordBot/ConnectedComputer.cs
Normal file
273
MinecraftDiscordBot/ConnectedComputer.cs
Normal file
@ -0,0 +1,273 @@
|
|||||||
|
using Discord;
|
||||||
|
using Discord.WebSocket;
|
||||||
|
using Fleck;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
|
public delegate Task<TResponse> HandleCommandDelegate<TResponse>(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
||||||
|
public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||||
|
public sealed class CommandHandlerAttribute : Attribute {
|
||||||
|
public CommandHandlerAttribute(string commandName) => CommandName = commandName;
|
||||||
|
public string CommandName { get; }
|
||||||
|
public string? HelpText { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RefinedStorageService : CommandRouter {
|
||||||
|
private readonly ITaskWaitSource _taskSource;
|
||||||
|
public override string HelpTextPrefix => "!rs ";
|
||||||
|
public RefinedStorageService(ITaskWaitSource taskSource) : base() => _taskSource = taskSource;
|
||||||
|
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
||||||
|
=> Task.FromResult(ResponseType.AsString($"The RS system has no command '{method}'!"));
|
||||||
|
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
|
||||||
|
=> Task.FromResult(ResponseType.AsString("The RS system is online!"));
|
||||||
|
|
||||||
|
private async Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct) {
|
||||||
|
var waiter = _taskSource.GetWaiter(parser, ct);
|
||||||
|
await _taskSource.Send(new RequestMessage(waiter.ID, methodName));
|
||||||
|
return await waiter.Task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private const string CmdEnergyUsage = "energyusage";
|
||||||
|
private const string CmdEnergyStorage = "energystorage";
|
||||||
|
private const string CmdListItems = "listitems";
|
||||||
|
private const string CmdItemName = "itemname";
|
||||||
|
private const string CmdListFluids = "listfluids";
|
||||||
|
|
||||||
|
public async Task<int> GetEnergyUsageAsync(CancellationToken ct) => await Method(CmdEnergyUsage, int.Parse, ct);
|
||||||
|
public async Task<int> GetEnergyStorageAsync(CancellationToken ct) => await Method(CmdEnergyStorage, int.Parse, ct);
|
||||||
|
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) => await Method(CmdListItems, ConnectedComputer.Deserialize<IEnumerable<Item>>(), ct);
|
||||||
|
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) => await Method(CmdListFluids, ConnectedComputer.Deserialize<IEnumerable<Fluid>>(), ct);
|
||||||
|
|
||||||
|
private Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<string> filters, CancellationToken ct)
|
||||||
|
=> FilterItems(message, filters.Select(ItemFilter.Parse), ct);
|
||||||
|
|
||||||
|
private async Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<ItemFilter> filters, CancellationToken ct) {
|
||||||
|
var items = Items?.ToList().AsEnumerable();
|
||||||
|
if (items is null) items = (await RefreshItemList(ct)).ToList();
|
||||||
|
foreach (var filter in filters)
|
||||||
|
items = items.Where(filter.MatchItem);
|
||||||
|
return items.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<List<Item>> RefreshItemList(CancellationToken ct) {
|
||||||
|
var response = await ListItemsAsync(ct);
|
||||||
|
lock (_itemLock) {
|
||||||
|
Items = response.OrderByDescending(i => i.Amount).ToList();
|
||||||
|
return Items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Item>? Items;
|
||||||
|
private readonly object _itemLock = new();
|
||||||
|
|
||||||
|
[CommandHandler(CmdEnergyStorage, HelpText = "Get the amount of energy stored in the RS system.")]
|
||||||
|
public async Task<ResponseType> HandleEnergyStorage(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||||
|
=> ResponseType.AsString($"Refined Storage system stores {await GetEnergyStorageAsync(ct)} RF/t");
|
||||||
|
[CommandHandler(CmdEnergyUsage, HelpText = "Get the amount of energy used by the RS system.")]
|
||||||
|
public async Task<ResponseType> HandleEnergyUsage(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||||
|
=> ResponseType.AsString($"Refined Storage system currently uses {await GetEnergyUsageAsync(ct)} RF/t");
|
||||||
|
[CommandHandler(CmdItemName, HelpText = "Filter items by name.")]
|
||||||
|
public async Task<ResponseType> HandleItemName(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
||||||
|
if (parameters.Length < 2) return ResponseType.AsString($"Usage: {CmdItemName} filters...");
|
||||||
|
else {
|
||||||
|
var items = await FilterItems(message, parameters[1..], ct);
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.AppendLine("Did you mean:");
|
||||||
|
sb.AppendJoin("\n", items.Select(i => i.ToString()));
|
||||||
|
return ResponseType.AsString(sb.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[CommandHandler(CmdListFluids, HelpText = "Gets a list of fluids that are currently stored in the RS system.")]
|
||||||
|
public async Task<ResponseType> HandleFluidListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.Append("The Refined Storage system stores those fluids:");
|
||||||
|
var fluids = await ListFluidsAsync(ct);
|
||||||
|
foreach (var fluid in fluids.OrderByDescending(i => i.Amount))
|
||||||
|
if (fluid.Amount > 10000) sb.AppendFormat("\n{0:n2} B of {1}", fluid.Amount / 1000.0f, fluid.DisplayName);
|
||||||
|
else sb.AppendFormat("\n{0:n0} mB of {1}", fluid.Amount, fluid.DisplayName);
|
||||||
|
return ResponseType.AsString(sb.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[CommandHandler(CmdListItems, HelpText = "Gets a list of items that are currently stored in the RS system.")]
|
||||||
|
public async Task<ResponseType> HandleItemListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.Append("The Refined Storage system currently stores these items:");
|
||||||
|
var items = await RefreshItemList(ct);
|
||||||
|
lock (_itemLock) {
|
||||||
|
int taken = 0;
|
||||||
|
foreach (var item in items) {
|
||||||
|
if (sb.Length > 500) break;
|
||||||
|
sb.AppendFormat("\n{0:n0}x {1}", item.Amount, item.DisplayName);
|
||||||
|
taken++;
|
||||||
|
}
|
||||||
|
if (items.Count > taken) sb.AppendFormat("\nand {0} more items.", items.Skip(taken).Sum(i => i.Amount));
|
||||||
|
}
|
||||||
|
return ResponseType.AsString(sb.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ConnectedComputer : CommandRouter, ITaskWaitSource {
|
||||||
|
protected readonly IWebSocketConnection _socket;
|
||||||
|
public override string HelpTextPrefix => "!";
|
||||||
|
public ConnectedComputer(IWebSocketConnection socket) : base() {
|
||||||
|
socket.OnMessage = OnMessage;
|
||||||
|
_socket = socket;
|
||||||
|
_rs = new RefinedStorageService(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMessage(string message) {
|
||||||
|
if (JsonConvert.DeserializeObject<ReplyMessage>(message) is not ReplyMessage msg) return;
|
||||||
|
IChunkWaiter? waiter;
|
||||||
|
lock (_syncRoot) {
|
||||||
|
if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
||||||
|
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!msg.Success) waiter.SetUnsuccessful();
|
||||||
|
waiter.AddChunk(msg.Chunk, msg.Total, msg.Result);
|
||||||
|
if (waiter.Finished || waiter.IsCancellationRequested)
|
||||||
|
lock (_syncRoot)
|
||||||
|
_waits.Remove(waiter.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task Send(string message) => _socket.Send(message);
|
||||||
|
public Task Send(Message message) => Send(JsonConvert.SerializeObject(message));
|
||||||
|
private readonly object _syncRoot = new();
|
||||||
|
private readonly Dictionary<int, IChunkWaiter> _waits = new();
|
||||||
|
private readonly Random _rnd = new();
|
||||||
|
public IWebSocketConnectionInfo ConnectionInfo => _socket.ConnectionInfo;
|
||||||
|
|
||||||
|
private int GetFreeId() {
|
||||||
|
var attempts = 0;
|
||||||
|
while (true) {
|
||||||
|
var id = _rnd.Next();
|
||||||
|
if (!_waits.ContainsKey(id))
|
||||||
|
return id;
|
||||||
|
Program.LogWarningAsync(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
|
||||||
|
ChunkWaiter<T> waiter;
|
||||||
|
lock (_syncRoot) {
|
||||||
|
waiter = new ChunkWaiter<T>(GetFreeId(), resultParser, ct);
|
||||||
|
_waits.Add(waiter.ID, waiter);
|
||||||
|
}
|
||||||
|
return waiter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly ICommandHandler<ResponseType> _rs;
|
||||||
|
[CommandHandler("rs", HelpText ="Provides some commands for interacting with the Refined Storage system.")]
|
||||||
|
public Task<ResponseType> RefinedStorageHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||||
|
=> _rs.HandleCommand(message, parameters, ct);
|
||||||
|
|
||||||
|
public static Func<string, T> Deserialize<T>() => msg
|
||||||
|
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
||||||
|
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
|
||||||
|
=> Task.FromResult(ResponseType.AsString("The Minecraft server is connected!"));
|
||||||
|
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
||||||
|
=> Task.FromResult(ResponseType.AsString($"What the fuck do you mean by '{method}'?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ITaskWaitSource {
|
||||||
|
ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct);
|
||||||
|
Task Send(Message requestMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class ReplyException : Exception {
|
||||||
|
public ReplyException() { }
|
||||||
|
public ReplyException(string message) : base(message) { }
|
||||||
|
public ReplyException(string message, Exception inner) : base(message, inner) { }
|
||||||
|
protected ReplyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn, Description = "Describes an item in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||||
|
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||||
|
public class Item : Fluid {
|
||||||
|
[JsonProperty("fingerprint", Required = Required.Always)]
|
||||||
|
public Md5Hash Fingerprint { get; set; } = default!;
|
||||||
|
[JsonProperty("nbt", Required = Required.DisallowNull)]
|
||||||
|
public dynamic? NBT { get; set; }
|
||||||
|
public override string ToString() => $"{Amount:n0}x {DisplayName}";
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn, Description = "Describes a fluid in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||||
|
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||||
|
public class Fluid {
|
||||||
|
[JsonProperty("amount", Required = Required.Always)]
|
||||||
|
public int Amount { get; set; }
|
||||||
|
[JsonProperty("displayName", Required = Required.Always)]
|
||||||
|
public string DisplayName { get; set; } = default!;
|
||||||
|
[JsonProperty("tags", Required = Required.DisallowNull)]
|
||||||
|
public string[]? Tags { get; set; } = default;
|
||||||
|
[JsonProperty("name", Required = Required.Always)]
|
||||||
|
public ModItemId ItemId { get; set; } = default!;
|
||||||
|
public override string ToString() => Amount > 10000
|
||||||
|
? $"{Amount / 1000.0f:n2} B of {DisplayName}"
|
||||||
|
: $"{Amount:n0} mB of {DisplayName}";
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ModItemIdJsonConverter))]
|
||||||
|
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||||
|
public class ModItemId {
|
||||||
|
public ModItemId(string name) {
|
||||||
|
var colon = name.IndexOf(':');
|
||||||
|
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
|
||||||
|
ModName = name[..colon];
|
||||||
|
ModItem = name[(colon + 1)..];
|
||||||
|
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
|
||||||
|
}
|
||||||
|
public override string ToString() => $"{ModName}:{ModItem}";
|
||||||
|
public string ModName { get; }
|
||||||
|
public string ModItem { get; }
|
||||||
|
|
||||||
|
public class ModItemIdJsonConverter : JsonConverter<ModItemId> {
|
||||||
|
public override ModItemId? ReadJson(JsonReader reader, Type objectType, ModItemId? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||||
|
=> reader.Value is string value
|
||||||
|
? new(value)
|
||||||
|
: throw new JsonException($"Could not parse mod name with token '{reader.Value}'");
|
||||||
|
public override void WriteJson(JsonWriter writer, ModItemId? value, JsonSerializer serializer) {
|
||||||
|
if (value is null) writer.WriteNull();
|
||||||
|
else writer.WriteValue(value.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(Md5JsonConverter))]
|
||||||
|
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||||
|
public class Md5Hash : IEquatable<Md5Hash?> {
|
||||||
|
private readonly byte[] _hash;
|
||||||
|
public Md5Hash(string hash) : this(Convert.FromHexString(hash)) { }
|
||||||
|
public Md5Hash(byte[] hash) {
|
||||||
|
if (hash is not { Length: 16 }) throw new ArgumentException("Invalid digest size!", nameof(hash));
|
||||||
|
_hash = hash;
|
||||||
|
}
|
||||||
|
public override bool Equals(object? obj) => Equals(obj as Md5Hash);
|
||||||
|
public bool Equals(Md5Hash? other) => other != null && _hash.SequenceEqual(other._hash);
|
||||||
|
public override int GetHashCode() {
|
||||||
|
var hashCode = new HashCode();
|
||||||
|
hashCode.AddBytes(_hash);
|
||||||
|
return hashCode.ToHashCode();
|
||||||
|
}
|
||||||
|
public override string ToString() => Convert.ToHexString(_hash);
|
||||||
|
|
||||||
|
public class Md5JsonConverter : JsonConverter<Md5Hash> {
|
||||||
|
public override Md5Hash? ReadJson(JsonReader reader, Type objectType, Md5Hash? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||||
|
=> reader.Value is string { Length: 32 } value
|
||||||
|
? new(value)
|
||||||
|
: throw new JsonException($"Could not parse MD5 hash with token '{reader.Value}'");
|
||||||
|
public override void WriteJson(JsonWriter writer, Md5Hash? value, JsonSerializer serializer) {
|
||||||
|
if (value is null) writer.WriteNull();
|
||||||
|
else writer.WriteValue(value.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
using MinecraftDiscordBot.Models;
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public interface IChunkWaiter {
|
public interface IChunkWaiter {
|
||||||
bool Finished { get; }
|
bool Finished { get; }
|
||||||
int ID { get; }
|
int ID { get; }
|
||||||
bool IsCancellationRequested { get; }
|
bool IsCancellationRequested { get; }
|
||||||
void AddChunk(int chunkId, int totalChunks, string value);
|
void AddChunk(int chunkId, int totalChunks, string value);
|
||||||
void SetResultState(ResultState state);
|
void SetUnsuccessful();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Commands;
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
public interface ICommandHandler {
|
public interface ICommandHandler {
|
||||||
Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
@ -1,13 +0,0 @@
|
|||||||
using Discord.WebSocket;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public interface IUserRoleManager {
|
|
||||||
/// <summary>
|
|
||||||
/// Verifies that a user is a bot administrator.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="user">User ID.</param>
|
|
||||||
/// <param name="message">An optional message to throw when user is not authorized.</param>
|
|
||||||
/// <exception cref="ReplyException">User is not authorized.</exception>
|
|
||||||
void RequireAdministrator(ulong user, string? message = null);
|
|
||||||
}
|
|
@ -1,6 +1,4 @@
|
|||||||
using MinecraftDiscordBot.Models;
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public abstract class ItemFilter {
|
public abstract class ItemFilter {
|
||||||
public abstract bool Match(Fluid item);
|
public abstract bool Match(Fluid item);
|
||||||
|
65
MinecraftDiscordBot/Message.cs
Normal file
65
MinecraftDiscordBot/Message.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
using Discord.WebSocket;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
|
public abstract class Message {
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public abstract string Type { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CapabilityMessage : Message {
|
||||||
|
public override string Type => "roles";
|
||||||
|
[JsonProperty("role", Required = Required.Always)]
|
||||||
|
public string[] Role { get; set; } = default!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TextMessage : Message {
|
||||||
|
public TextMessage(SocketMessage arg) : this(arg.Author.Username, arg.Content) { }
|
||||||
|
public TextMessage(string author, string content) {
|
||||||
|
Author = author;
|
||||||
|
Content = content;
|
||||||
|
}
|
||||||
|
public override string Type => "text";
|
||||||
|
[JsonProperty("author", Required = Required.Always)]
|
||||||
|
public string Author { get; set; }
|
||||||
|
[JsonProperty("message", Required = Required.Always)]
|
||||||
|
public string Content { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ReplyMessage : Message {
|
||||||
|
public ReplyMessage(int answerId, string result) {
|
||||||
|
AnswerId = answerId;
|
||||||
|
Result = result;
|
||||||
|
}
|
||||||
|
[JsonProperty("id", Required = Required.Always)]
|
||||||
|
public int AnswerId { get; set; }
|
||||||
|
[JsonProperty("result", Required = Required.Always)]
|
||||||
|
public string Result { get; set; }
|
||||||
|
[JsonProperty("chunk", Required = Required.DisallowNull)]
|
||||||
|
public int Chunk { get; set; } = 1;
|
||||||
|
[JsonProperty("total", Required = Required.DisallowNull)]
|
||||||
|
public int Total { get; set; } = 1;
|
||||||
|
/// <summary>
|
||||||
|
/// If at least one packet was received where
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("success", Required = Required.DisallowNull)]
|
||||||
|
public bool Success { get; set; } = true;
|
||||||
|
public override string Type => "reply";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RequestMessage : Message {
|
||||||
|
public RequestMessage(int answerId, string method, Dictionary<string, string>? parameters = null) {
|
||||||
|
AnswerId = answerId;
|
||||||
|
Method = method;
|
||||||
|
Parameters = (parameters ?? Enumerable.Empty<KeyValuePair<string, string>>())
|
||||||
|
.ToDictionary(i => i.Key, i => i.Value);
|
||||||
|
}
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public int AnswerId { get; set; }
|
||||||
|
[JsonProperty("method")]
|
||||||
|
public string Method { get; set; }
|
||||||
|
[JsonProperty("params")]
|
||||||
|
public Dictionary<string, string> Parameters { get; }
|
||||||
|
public override string Type => "request";
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
<Version>1.1.5</Version>
|
<Version>1.0.2</Version>
|
||||||
<Authors>Michael Chen</Authors>
|
<Authors>Michael Chen</Authors>
|
||||||
<Company>$(Authors)</Company>
|
<Company>$(Authors)</Company>
|
||||||
<RepositoryUrl>https://gitlab.com/chenmichael/mcdiscordbot</RepositoryUrl>
|
<RepositoryUrl>https://gitlab.com/chenmichael/mcdiscordbot</RepositoryUrl>
|
||||||
@ -16,19 +16,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="ClientScript.lua" />
|
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||||
</ItemGroup>
|
<PackageReference Include="Discord.Net" Version="3.1.0" />
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
|
||||||
<PackageReference Include="Discord.Net" Version="3.8.1" />
|
|
||||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
</ItemGroup>
|
<PackageReference Include="OneOf" Version="3.0.205" />
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Resource Include="ClientScript.lua" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes a fluid in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class Fluid {
|
|
||||||
[JsonProperty("amount", Required = Required.Always)]
|
|
||||||
public int Amount { get; set; }
|
|
||||||
[JsonProperty("displayName", Required = Required.Always)]
|
|
||||||
public string DisplayName { get; set; } = default!;
|
|
||||||
[JsonProperty("tags", Required = Required.DisallowNull)]
|
|
||||||
public string[]? Tags { get; set; } = default;
|
|
||||||
[JsonProperty("name", Required = Required.Always)]
|
|
||||||
public ModItemId ItemId { get; set; } = default!;
|
|
||||||
public override string ToString() => Amount > 10000
|
|
||||||
? $"{Amount / 1000.0f:n2} B of {DisplayName}"
|
|
||||||
: $"{Amount:n0} mB of {DisplayName}";
|
|
||||||
[JsonIgnore]
|
|
||||||
public string CleanDisplayName => DisplayName[1..^1];
|
|
||||||
[JsonIgnore]
|
|
||||||
public string? TagString => Tags is string[] tags ? string.Join(", ", tags) : null;
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes an item in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class Item : Fluid {
|
|
||||||
[JsonProperty("fingerprint", Required = Required.Always)]
|
|
||||||
public Md5Hash Fingerprint { get; set; } = default!;
|
|
||||||
[JsonProperty("nbt", Required = Required.DisallowNull)]
|
|
||||||
public dynamic? NBT { get; set; }
|
|
||||||
public override string ToString() => $"{AmountString} {CleanDisplayName}";
|
|
||||||
[JsonIgnore]
|
|
||||||
public string DetailString {
|
|
||||||
get {
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
sb.AppendFormat("{0} {1}, fp: {2}", AmountString, CleanDisplayName, Fingerprint);
|
|
||||||
if (TagString is string tags)
|
|
||||||
sb.AppendFormat(", tags: [{0}]", tags);
|
|
||||||
if (NBT is not null)
|
|
||||||
sb.AppendFormat(", NBT: {0}", JsonConvert.SerializeObject(NBT));
|
|
||||||
return sb.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[JsonIgnore]
|
|
||||||
public string AmountString => Amount switch {
|
|
||||||
> 1000000 => $"> {Amount / 1000000:n0}m",
|
|
||||||
> 10000 => $"~ {Amount / 1000.0f:n2}k",
|
|
||||||
_ => Amount.ToString()
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
public class LuaPackedArray {
|
|
||||||
public ref object? this[int i] => ref _items[i];
|
|
||||||
private readonly object?[] _items;
|
|
||||||
public LuaPackedArray(IDictionary<string, object> packedTable) {
|
|
||||||
if (packedTable["n"] is not long n) throw new ArgumentException("No length in packed array!");
|
|
||||||
_items = new object?[n];
|
|
||||||
for (var i = 0; i < _items.Length; i++)
|
|
||||||
_items[i] = packedTable.TryGetValue((i + 1).ToString(), out var val) ? val : null;
|
|
||||||
}
|
|
||||||
public static LuaPackedArray Deserialize(string value) {
|
|
||||||
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
|
|
||||||
return new LuaPackedArray(dict ?? throw new Exception("Not a packed table (empty object)!"));
|
|
||||||
}
|
|
||||||
public override string ToString() => _items is { Length: 0 }
|
|
||||||
? "Empty Array"
|
|
||||||
: string.Join(", ", _items.Select(i => i is null ? "nil" : i.ToString()));
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
[JsonConverter(typeof(Md5JsonConverter))]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class Md5Hash : IEquatable<Md5Hash?> {
|
|
||||||
private readonly byte[] _hash;
|
|
||||||
public Md5Hash(string hash) : this(Convert.FromHexString(hash)) { }
|
|
||||||
public Md5Hash(byte[] hash) {
|
|
||||||
if (hash is not { Length: 16 }) throw new ArgumentException("Invalid digest size!", nameof(hash));
|
|
||||||
_hash = hash;
|
|
||||||
}
|
|
||||||
public override bool Equals(object? obj) => Equals(obj as Md5Hash);
|
|
||||||
public bool Equals(Md5Hash? other) => other != null && _hash.SequenceEqual(other._hash);
|
|
||||||
public override int GetHashCode() {
|
|
||||||
var hashCode = new HashCode();
|
|
||||||
hashCode.AddBytes(_hash);
|
|
||||||
return hashCode.ToHashCode();
|
|
||||||
}
|
|
||||||
public override string ToString() => Convert.ToHexString(_hash).ToLower();
|
|
||||||
|
|
||||||
public class Md5JsonConverter : JsonConverter<Md5Hash> {
|
|
||||||
public override Md5Hash? ReadJson(JsonReader reader, Type objectType, Md5Hash? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
||||||
=> reader.Value is string { Length: 32 } value
|
|
||||||
? new(value)
|
|
||||||
: throw new JsonException($"Could not parse MD5 hash with token '{reader.Value}'");
|
|
||||||
public override void WriteJson(JsonWriter writer, Md5Hash? value, JsonSerializer serializer) {
|
|
||||||
if (value is null) writer.WriteNull();
|
|
||||||
else writer.WriteValue(value.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool TryParse(string itemid, [NotNullWhen(true)] out Md5Hash? fingerprint) {
|
|
||||||
try {
|
|
||||||
fingerprint = new Md5Hash(itemid);
|
|
||||||
return true;
|
|
||||||
} catch (Exception) {
|
|
||||||
fingerprint = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,150 +0,0 @@
|
|||||||
using Discord.WebSocket;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
public abstract class Message {
|
|
||||||
public static Message Deserialize(string strMessage) {
|
|
||||||
var obj = JObject.Parse(strMessage);
|
|
||||||
var typeName = GetKey<string>(obj, "type");
|
|
||||||
if (!Parsers.TryGetValue(typeName, out var type))
|
|
||||||
throw new FormatException($"Unknown message type '{typeName}'!");
|
|
||||||
if (obj.ToObject(type) is not Message message)
|
|
||||||
throw new FormatException($"Message cannot be casted to '{type}'!");
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static readonly Dictionary<string, Type> Parsers = GetMessageTypes();
|
|
||||||
private static Dictionary<string, Type> GetMessageTypes() {
|
|
||||||
var types = new Dictionary<string, Type>();
|
|
||||||
var messageTypes =
|
|
||||||
AppDomain.CurrentDomain.GetAssemblies().SelectMany(domainAssembly => domainAssembly.GetTypes())
|
|
||||||
.Where(typeof(Message).IsAssignableFrom);
|
|
||||||
foreach (var type in messageTypes)
|
|
||||||
if (GetTypeAttribute(type) is MessageTypeAttribute attr)
|
|
||||||
types.Add(attr.Name, type);
|
|
||||||
return types;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MessageTypeAttribute? GetTypeAttribute(Type type)
|
|
||||||
=> type.GetCustomAttributes(typeof(MessageTypeAttribute), false).OfType<MessageTypeAttribute>().FirstOrDefault();
|
|
||||||
|
|
||||||
private static T GetKey<T>(JObject msg, string key)
|
|
||||||
=> (msg.TryGetValue(key, out var type) ? type : throw new FormatException($"Message has no '{key}' param!"))
|
|
||||||
.ToObject<T>() ?? throw new FormatException($"'{key}' param is not of expected type '{typeof(T).Name}'!");
|
|
||||||
|
|
||||||
[JsonProperty("type")]
|
|
||||||
public abstract string Type { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class CapabilityMessage : Message {
|
|
||||||
private const string TYPE = "roles";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonProperty("role", Required = Required.Always)]
|
|
||||||
public string[] Role { get; set; } = default!;
|
|
||||||
public override string ToString() => $"Capabilities: {string.Join(", ", Role)}";
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class ReplyMessage : Message {
|
|
||||||
private const string TYPE = "reply";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonProperty("id", Required = Required.Always)]
|
|
||||||
public int AnswerId { get; set; }
|
|
||||||
[JsonProperty("result", Required = Required.Always)]
|
|
||||||
public string Result { get; set; } = default!;
|
|
||||||
[JsonProperty("chunk", Required = Required.DisallowNull)]
|
|
||||||
public int Chunk { get; set; } = 1;
|
|
||||||
[JsonProperty("total", Required = Required.DisallowNull)]
|
|
||||||
public int Total { get; set; } = 1;
|
|
||||||
[JsonProperty("success", Required = Required.DisallowNull)]
|
|
||||||
public ResultState State { get; set; } = ResultState.Successful;
|
|
||||||
public override string ToString() => $"Reply [{AnswerId}] {State} ({Chunk}/{Total}) Length {Result.Length}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class EventMessage : Message { }
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class PeripheralDetachEvent : EventMessage {
|
|
||||||
private const string TYPE = "peripheral_detach";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonProperty("side", Required = Required.Always)]
|
|
||||||
public string Side { get; set; } = default!;
|
|
||||||
public override string ToString() => $"Detached '{Side}'!";
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class PlayerStatusEvent : EventMessage {
|
|
||||||
private const string TYPE = "playerstatus";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonProperty("player", Required = Required.Always)]
|
|
||||||
public string Player { get; set; } = default!;
|
|
||||||
[JsonProperty("status", Required = Required.Always)]
|
|
||||||
public bool Online { get; set; }
|
|
||||||
public override string ToString() => $"{Player} is now {(Online ? "on" : "off")}line!";
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class PeripheralAttachEvent : EventMessage {
|
|
||||||
private const string TYPE = "peripheral";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonIgnore]
|
|
||||||
public string Side => Peripheral.Side;
|
|
||||||
[JsonProperty("peripheral", Required = Required.Always)]
|
|
||||||
public Peripheral Peripheral { get; set; } = default!;
|
|
||||||
public override string ToString() => $"Attached {Peripheral}!";
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Peripheral {
|
|
||||||
[JsonProperty("side", Required = Required.Always)]
|
|
||||||
public string Side { get; set; } = default!;
|
|
||||||
[JsonProperty("type", Required = Required.Always)]
|
|
||||||
public string Type { get; set; } = default!;
|
|
||||||
[JsonProperty("methods", Required = Required.Always)]
|
|
||||||
public string[] Methods { get; set; } = default!;
|
|
||||||
public override string ToString() => $"{Type} at '{Side}'";
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class ChatEvent : EventMessage {
|
|
||||||
private const string TYPE = "chat";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
[JsonProperty("username", Required = Required.Always)]
|
|
||||||
public string Username { get; set; } = default!;
|
|
||||||
[JsonProperty("message", Required = Required.Always)]
|
|
||||||
public string Message { get; set; } = default!;
|
|
||||||
[JsonProperty("uuid", Required = Required.Always)]
|
|
||||||
public string UUID { get; set; } = default!;
|
|
||||||
[JsonProperty("hidden", Required = Required.Always)]
|
|
||||||
public bool IsHidden { get; set; }
|
|
||||||
public override string ToString() => $"{(IsHidden ? "HIDDEN: " : string.Empty)}[{Username}] {Message} ({UUID})";
|
|
||||||
}
|
|
||||||
|
|
||||||
[MessageType(TYPE)]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class RequestMessage : Message {
|
|
||||||
private const string TYPE = "request";
|
|
||||||
public override string Type => TYPE;
|
|
||||||
public RequestMessage(int answerId, string method, Dictionary<string, object>? parameters = null) {
|
|
||||||
AnswerId = answerId;
|
|
||||||
Method = method;
|
|
||||||
Parameters = (parameters ?? Enumerable.Empty<KeyValuePair<string, object>>())
|
|
||||||
.ToDictionary(i => i.Key, i => i.Value);
|
|
||||||
}
|
|
||||||
[JsonProperty("id")]
|
|
||||||
public int AnswerId { get; set; }
|
|
||||||
[JsonProperty("method")]
|
|
||||||
public string Method { get; set; }
|
|
||||||
[JsonProperty("params")]
|
|
||||||
public Dictionary<string, object> Parameters { get; }
|
|
||||||
public override string ToString() => $"Request [{AnswerId}] {Method}({JsonConvert.SerializeObject(Parameters)})";
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
||||||
public sealed class MessageTypeAttribute : Attribute {
|
|
||||||
public MessageTypeAttribute(string type) => Name = type;
|
|
||||||
public string Name { get; }
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
[JsonConverter(typeof(ModItemIdJsonConverter))]
|
|
||||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
|
||||||
public class ModItemId {
|
|
||||||
public ModItemId(string name) {
|
|
||||||
var colon = name.IndexOf(':');
|
|
||||||
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
|
|
||||||
ModName = name[..colon];
|
|
||||||
ModItem = name[(colon + 1)..];
|
|
||||||
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
|
|
||||||
}
|
|
||||||
public override string ToString() => $"{ModName}:{ModItem}";
|
|
||||||
public string ModName { get; }
|
|
||||||
public string ModItem { get; }
|
|
||||||
|
|
||||||
public class ModItemIdJsonConverter : JsonConverter<ModItemId> {
|
|
||||||
public override ModItemId? ReadJson(JsonReader reader, Type objectType, ModItemId? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
||||||
=> reader.Value is string value
|
|
||||||
? new(value)
|
|
||||||
: throw new JsonException($"Could not parse mod name with token '{reader.Value}'");
|
|
||||||
public override void WriteJson(JsonWriter writer, ModItemId? value, JsonSerializer serializer) {
|
|
||||||
if (value is null) writer.WriteNull();
|
|
||||||
else writer.WriteValue(value.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
public class PlayerPosition {
|
|
||||||
[JsonProperty("dimension", Required = Required.Always)] public string Dimension { get; set; } = default!;
|
|
||||||
[JsonProperty("eyeHeight", Required = Required.Always)] public double EyeHeight { get; set; }
|
|
||||||
[JsonProperty("pitch", Required = Required.Always)] public double Pitch { get; set; }
|
|
||||||
[JsonProperty("yaw", Required = Required.Always)] public double Yaw { get; set; }
|
|
||||||
[JsonProperty("x", Required = Required.Always)] public int X { get; set; }
|
|
||||||
[JsonProperty("y", Required = Required.Always)] public int Y { get; set; }
|
|
||||||
[JsonProperty("z", Required = Required.Always)] public int Z { get; set; }
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
namespace MinecraftDiscordBot.Models;
|
|
||||||
|
|
||||||
public enum ResultState {
|
|
||||||
Successful,
|
|
||||||
Unsuccessful,
|
|
||||||
Fatal
|
|
||||||
}
|
|
@ -2,19 +2,17 @@
|
|||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.Rest;
|
using Discord.Rest;
|
||||||
using Discord.Webhook;
|
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Fleck;
|
using Fleck;
|
||||||
using MinecraftDiscordBot.Commands;
|
using Newtonsoft.Json;
|
||||||
using MinecraftDiscordBot.Models;
|
using OneOf;
|
||||||
using MinecraftDiscordBot.Services;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace MinecraftDiscordBot;
|
namespace MinecraftDiscordBot;
|
||||||
|
|
||||||
public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleManager {
|
public class Program : IDisposable, ICommandHandler<ResponseType> {
|
||||||
public const string WebSocketSource = "WebSocket";
|
public const string WebSocketSource = "WebSocket";
|
||||||
public const string BotSource = "Bot";
|
public const string BotSource = "Bot";
|
||||||
private static readonly object LogLock = new();
|
private static readonly object LogLock = new();
|
||||||
@ -26,47 +24,30 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
private readonly WebSocketServer _wssv;
|
private readonly WebSocketServer _wssv;
|
||||||
private readonly BotConfiguration _config;
|
private readonly BotConfiguration _config;
|
||||||
private readonly HashSet<ulong> _whitelistedChannels;
|
private readonly HashSet<ulong> _whitelistedChannels;
|
||||||
private readonly ConcurrentDictionary<Guid, RootCommandService> _connections = new();
|
private readonly ConcurrentDictionary<Guid, ConnectedComputer> _connections = new();
|
||||||
private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' };
|
private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' };
|
||||||
public IEnumerable<ActiveChannel> Channels => _channels ?? throw new InvalidProgramException("Channels used before verification!");
|
public ITextChannel[] _channels = Array.Empty<ITextChannel>();
|
||||||
public ActiveChannel[]? _channels;
|
private ConnectedComputer? _rsSystem = null;
|
||||||
private bool disposedValue;
|
private bool disposedValue;
|
||||||
private static ITextChannel? LogChannel;
|
public static bool OnlineNotifications => false;
|
||||||
private readonly RootCommandService _computer;
|
|
||||||
|
|
||||||
public static bool OnlineNotifications => true;
|
public ConnectedComputer? Computer {
|
||||||
public const LogSeverity DiscordLogSeverity = LogSeverity.Warning;
|
get => _rsSystem; set {
|
||||||
private const string ClientScriptName = "MinecraftDiscordBot.ClientScript.lua";
|
if (_rsSystem != value) {
|
||||||
private const string WebhookName = "minecraftbot";
|
_rsSystem = value;
|
||||||
public readonly string ClientScript;
|
if (OnlineNotifications)
|
||||||
private readonly ITokenProvider _tokenProvider = new TimeoutTokenProvider(InstanceId, 10);
|
_ = Task.Run(() => Broadcast(i => i.SendMessageAsync(value is null
|
||||||
private static readonly int InstanceId = new Random().Next();
|
? $"The Refined Storage went offline. Please check the server!"
|
||||||
|
: $"The Refined Storage is back online!")));
|
||||||
private string GetVerifiedClientScript() => ClientScript
|
}
|
||||||
.Replace("$TOKEN", _tokenProvider.GenerateToken());
|
}
|
||||||
|
|
||||||
private static string GetClientScript(BotConfiguration config) {
|
|
||||||
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ClientScriptName);
|
|
||||||
if (stream is null) throw new FileNotFoundException("Client script could not be loaded!");
|
|
||||||
using var sr = new StreamReader(stream);
|
|
||||||
return sr.ReadToEnd()
|
|
||||||
.Replace("$HOST", config.Address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Broadcast(Func<ITextChannel, Task<IUserMessage>> message)
|
private async Task Broadcast(Func<ITextChannel, Task<IUserMessage>> message) => _ = await Task.WhenAll(_channels.Select(message));
|
||||||
=> Task.WhenAll(Channels.Select(i => message(i.Channel)));
|
|
||||||
public Program(BotConfiguration config) {
|
public Program(BotConfiguration config) {
|
||||||
_config = config;
|
_config = config;
|
||||||
_computer = new(this);
|
|
||||||
_computer.ChatMessageReceived += MinecraftMessageReceived;
|
|
||||||
_computer.SocketChanged += ComputerConnectedChanged;
|
|
||||||
_computer.PlayerStatusChanged += PlayerStatusChanged;
|
|
||||||
_computer.PeripheralAttached += PeripheralAttached;
|
|
||||||
_computer.PeripheralDetached += PeripheralDetached;
|
|
||||||
_administrators = config.Administrators.ToHashSet();
|
|
||||||
ClientScript = GetClientScript(config);
|
|
||||||
_client.Log += LogAsync;
|
_client.Log += LogAsync;
|
||||||
_client.MessageReceived += (msg) => DiscordMessageReceived(msg, 20 * 1000);
|
_client.MessageReceived += (msg) => DiscordMessageReceived(msg);
|
||||||
_client.ReactionAdded += DiscordReactionAdded;
|
_client.ReactionAdded += DiscordReactionAdded;
|
||||||
_wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") {
|
_wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") {
|
||||||
RestartAfterListenError = true
|
RestartAfterListenError = true
|
||||||
@ -75,17 +56,6 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
_whitelistedChannels = config.Channels.ToHashSet();
|
_whitelistedChannels = config.Channels.ToHashSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PlayerStatusChanged(object? sender, PlayerStatusEvent e)
|
|
||||||
=> _ = Task.Run(() => Broadcast(i => i.SendMessageAsync($"{e.Player} just {(e.Online ? "joined" : "left")} the server!")));
|
|
||||||
private void PeripheralAttached(object? sender, PeripheralAttachEvent e) => LogInfo("Computer", $"Peripheral {e.Peripheral.Type} was attached on side {e.Side}.");
|
|
||||||
private void PeripheralDetached(object? sender, PeripheralDetachEvent e) => LogInfo("Computer", $"Peripheral on side {e.Side} was detached.");
|
|
||||||
private void ComputerConnectedChanged(object? sender, IWebSocketConnection? e)
|
|
||||||
=> _ = Task.Run(() => Broadcast(i => i.SendMessageAsync(e is not null
|
|
||||||
? "The Minecraft client is now available!"
|
|
||||||
: "The Minecraft client disconnected!")));
|
|
||||||
private void MinecraftMessageReceived(object? sender, ChatEvent e)
|
|
||||||
=> Task.Run(() => WebhookBroadcast(i => i.SendMessageAsync(e.Message, username: e.Username, avatarUrl: $"https://crafatar.com/renders/head/{e.UUID}")));
|
|
||||||
private Task<T[]> WebhookBroadcast<T>(Func<DiscordWebhookClient, Task<T>> apply) => Task.WhenAll(Channels.Select(i => apply(new DiscordWebhookClient(i.Webhook))));
|
|
||||||
private void LogWebSocket(LogLevel level, string message, Exception exception) => Log(new(level switch {
|
private void LogWebSocket(LogLevel level, string message, Exception exception) => Log(new(level switch {
|
||||||
LogLevel.Debug => LogSeverity.Debug,
|
LogLevel.Debug => LogSeverity.Debug,
|
||||||
LogLevel.Info => LogSeverity.Info,
|
LogLevel.Info => LogSeverity.Info,
|
||||||
@ -105,11 +75,11 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
private static Task<int> RunWithConfig(IBotConfigurator arg) => new Program(arg.Config).RunAsync();
|
private static Task<int> RunWithConfig(IBotConfigurator arg) => new Program(arg.Config).RunAsync();
|
||||||
|
|
||||||
public async Task<int> RunAsync() {
|
public async Task<int> RunAsync() {
|
||||||
StartWebSocketServer();
|
|
||||||
await _client.LoginAsync(TokenType.Bot, _config.Token);
|
await _client.LoginAsync(TokenType.Bot, _config.Token);
|
||||||
await _client.StartAsync();
|
await _client.StartAsync();
|
||||||
if (!await HasValidChannels())
|
if (!await HasValidChannels())
|
||||||
return 1;
|
return 1;
|
||||||
|
StartWebSocketServer();
|
||||||
|
|
||||||
// Block this task until the program is closed.
|
// Block this task until the program is closed.
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
@ -117,20 +87,11 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> HasValidChannels() {
|
private async Task<bool> HasValidChannels() {
|
||||||
if (_config.LogChannel is ulong logChannelId) {
|
if (await GetValidChannels(_whitelistedChannels).ToArrayAsync() is not { Length: > 0 } channels) {
|
||||||
LogChannel = await IsValidChannel(logChannelId);
|
|
||||||
if (LogChannel is null)
|
|
||||||
await LogWarningAsync(BotSource, $"The given log channel ID is not valid '{logChannelId}'!");
|
|
||||||
}
|
|
||||||
if (await GetValidChannels(_whitelistedChannels) is not { Length: > 0 } channels) {
|
|
||||||
await LogErrorAsync(BotSource, new InvalidOperationException("No valid textchannel was whitelisted!"));
|
await LogErrorAsync(BotSource, new InvalidOperationException("No valid textchannel was whitelisted!"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
_channels = await Task.WhenAll(channels.Select(async i => new ActiveChannel(i, await GetOrCreateWebhook(i))));
|
_channels = channels;
|
||||||
static async Task<IWebhook> GetOrCreateWebhook(ITextChannel i) {
|
|
||||||
var hooks = (await i.GetWebhooksAsync()).Where(i => i.Name == WebhookName).FirstOrDefault();
|
|
||||||
return hooks ?? await i.CreateWebhookAsync(WebhookName);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,14 +101,13 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
socket.OnMessage = async message => await SocketReceived(socket, message);
|
socket.OnMessage = async message => await SocketReceived(socket, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
private async Task<ITextChannel[]> GetValidChannels(IEnumerable<ulong> ids)
|
private async IAsyncEnumerable<ITextChannel> GetValidChannels(IEnumerable<ulong> ids) {
|
||||||
=> (await Task.WhenAll(ids.Select(i => IsValidChannel(i)))).OfType<ITextChannel>().ToArray();
|
foreach (var channelId in ids) {
|
||||||
private async Task<ITextChannel?> IsValidChannel(ulong channelId) {
|
|
||||||
var channel = await _client.GetChannelAsync(channelId);
|
var channel = await _client.GetChannelAsync(channelId);
|
||||||
if (channel is not ITextChannel textChannel) {
|
if (channel is not ITextChannel textChannel) {
|
||||||
if (channel is null) await LogWarningAsync(BotSource, $"Channel with id [{channelId}] does not exist!");
|
if (channel is null) await LogWarningAsync(BotSource, $"Channel with id [{channelId}] does not exist!");
|
||||||
else await LogWarningAsync(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
|
else await LogWarningAsync(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
|
||||||
return null;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textChannel.Guild is RestGuild guild) {
|
if (textChannel.Guild is RestGuild guild) {
|
||||||
@ -156,46 +116,17 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
} else {
|
} else {
|
||||||
await LogWarningAsync(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!");
|
await LogWarningAsync(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!");
|
||||||
}
|
}
|
||||||
return textChannel;
|
yield return textChannel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SocketReceived(IWebSocketConnection socket, string message) {
|
private static async Task SocketReceived(IWebSocketConnection socket, string message)
|
||||||
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Received: {message}");
|
=> await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Received: {message}");
|
||||||
await (message switch {
|
|
||||||
"getcode" => SendClientCode(socket),
|
|
||||||
string s when s.StartsWith("login=") => ClientComputerConnected(socket, s[6..]),
|
|
||||||
string s when s.StartsWith("error=") => ClientComputerError(socket, s[6..]),
|
|
||||||
_ => DisruptClientConnection(socket, "Protocol violation!")
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task ClientComputerError(IWebSocketConnection socket, string message)
|
private void AddComputerSocket(IWebSocketConnection socket, ConnectedComputer pc) => Computer = pc;
|
||||||
=> await LogWarningAsync("Client", $"Computer failed to run the script: {message}");
|
|
||||||
|
|
||||||
private async Task ClientComputerConnected(IWebSocketConnection socket, string token) {
|
|
||||||
if (!_tokenProvider.VerifyToken(token)) {
|
|
||||||
await DisruptClientConnection(socket, "outdated");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client logged in with valid script!");
|
|
||||||
AddComputerSocket(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task DisruptClientConnection(IWebSocketConnection socket, string reason) {
|
|
||||||
await socket.Send(reason);
|
|
||||||
await LogWarningAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client will be terminated, reason: {reason}");
|
|
||||||
socket.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task SendClientCode(IWebSocketConnection socket) {
|
|
||||||
await socket.Send(GetVerifiedClientScript());
|
|
||||||
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Script sent to client!");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddComputerSocket(IWebSocketConnection socket) => _computer.Socket = socket;
|
|
||||||
|
|
||||||
private void RemoveComputerSocket(IWebSocketConnection socket) {
|
private void RemoveComputerSocket(IWebSocketConnection socket) {
|
||||||
if (_computer.Socket is { ConnectionInfo.Id: Guid id } && id == socket.ConnectionInfo.Id) _computer.Socket = null;
|
if (Computer is { ConnectionInfo.Id: Guid id } && id == socket.ConnectionInfo.Id) Computer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SocketClosed(IWebSocketConnection socket) {
|
private async Task SocketClosed(IWebSocketConnection socket) {
|
||||||
@ -203,18 +134,19 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!");
|
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task SocketOpened(IWebSocketConnection socket) => await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!");
|
private async Task SocketOpened(IWebSocketConnection socket) {
|
||||||
|
AddComputerSocket(socket, new(socket));
|
||||||
|
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!");
|
||||||
|
}
|
||||||
|
|
||||||
private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) {
|
private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) {
|
||||||
if (arg is not SocketUserMessage message) return;
|
if (arg is not SocketUserMessage message) return;
|
||||||
if (message.Author.IsBot) return;
|
if (message.Author.IsBot) return;
|
||||||
if (!IsChannelWhitelisted(arg.Channel)) return;
|
if (!IsChannelWhitelisted(arg.Channel)) return;
|
||||||
if (arg.Type is not MessageType.Default) return;
|
|
||||||
|
|
||||||
var cts = new CancellationTokenSource(timeout);
|
var cts = new CancellationTokenSource(timeout);
|
||||||
|
|
||||||
if (IsCommand(message, out var argPos)) {
|
if (IsCommand(message, out var argPos)) {
|
||||||
await arg.Channel.TriggerTypingAsync();
|
|
||||||
var parameters = message.Content[argPos..].Split(WhiteSpace, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
var parameters = message.Content[argPos..].Split(WhiteSpace, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
_ = Task.Run(async () => {
|
_ = Task.Run(async () => {
|
||||||
var response = await HandleCommand(message, parameters, cts.Token);
|
var response = await HandleCommand(message, parameters, cts.Token);
|
||||||
@ -224,18 +156,16 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
}
|
}
|
||||||
|
|
||||||
await LogInfoAsync("Discord", $"[{arg.Author.Username}] {arg.Content}");
|
await LogInfoAsync("Discord", $"[{arg.Author.Username}] {arg.Content}");
|
||||||
_ = Task.Run(() => _computer.Chat.SendMessageAsync(arg.Content, arg.Author.Username, cts.Token));
|
// TODO: Relay Message to Chat Receiver
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task SendResponse(SocketUserMessage message, ResponseType response) => response switch {
|
private Task SendResponse(SocketUserMessage message, ResponseType response) => response switch {
|
||||||
ResponseType.IChoiceResponse res => HandleChoice(message, res),
|
ResponseType.IChoiceResponse res => HandleChoice(message, res),
|
||||||
ResponseType.StringResponse res => message.ReplyAsync(res.Message),
|
ResponseType.StringResponse res => message.ReplyAsync(res.Message),
|
||||||
ResponseType.FileResponse res => message.Channel.SendFileAsync(res.Path, text: res.Message),
|
_ => message.ReplyAsync($"Whoops, someone forgot to implement '{response.GetType()}' responses?"),
|
||||||
_ => message.ReplyAsync($"Whoops, someone forgot to implement '{response.GetType().Name}' responses?"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<ulong, ResponseType.IChoiceResponse> _choiceWait = new();
|
private readonly ConcurrentDictionary<ulong, ResponseType.IChoiceResponse> _choiceWait = new();
|
||||||
private readonly HashSet<ulong> _administrators;
|
|
||||||
|
|
||||||
private async Task DiscordReactionAdded(Cacheable<IUserMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel, SocketReaction reaction) {
|
private async Task DiscordReactionAdded(Cacheable<IUserMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel, SocketReaction reaction) {
|
||||||
var msgObject = await message.GetOrDownloadAsync();
|
var msgObject = await message.GetOrDownloadAsync();
|
||||||
@ -259,14 +189,14 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
public async Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
||||||
if (_computer is ICommandHandler<ResponseType> handler)
|
if (Computer is ICommandHandler<ResponseType> handler)
|
||||||
try {
|
try {
|
||||||
return await handler.HandleCommand(message, parameters, ct);
|
return await handler.HandleCommand(message, parameters, ct);
|
||||||
} catch (TaskCanceledException) {
|
} catch (TaskCanceledException) {
|
||||||
return ResponseType.AsString("Your request could not be processed in time!");
|
return ResponseType.AsString("Your request could not be processed in time!");
|
||||||
} catch (ReplyException e) {
|
} catch (ReplyException e) {
|
||||||
await LogInfoAsync(BotSource, e.Message);
|
await LogWarningAsync(BotSource, e.Message);
|
||||||
return ResponseType.AsString(e.Message);
|
return ResponseType.AsString($"Your request failed: {e.Message}");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
await LogErrorAsync(BotSource, e);
|
await LogErrorAsync(BotSource, e);
|
||||||
return ResponseType.AsString($"Oopsie doopsie, this should not have happened!");
|
return ResponseType.AsString($"Oopsie doopsie, this should not have happened!");
|
||||||
@ -289,29 +219,14 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
public static void LogError(string source, Exception exception) => Log(new(LogSeverity.Error, source, exception?.Message, exception));
|
public static void LogError(string source, Exception exception) => Log(new(LogSeverity.Error, source, exception?.Message, exception));
|
||||||
|
|
||||||
private static async Task LogAsync(LogMessage msg) {
|
private static async Task LogAsync(LogMessage msg) {
|
||||||
lock (LogLock) {
|
Log(msg);
|
||||||
var oldColor = Console.ForegroundColor;
|
await Task.CompletedTask;
|
||||||
try {
|
|
||||||
Console.ForegroundColor = msg.Severity switch {
|
|
||||||
LogSeverity.Critical => ConsoleColor.Magenta,
|
|
||||||
LogSeverity.Error => ConsoleColor.Red,
|
|
||||||
LogSeverity.Warning => ConsoleColor.Yellow,
|
|
||||||
LogSeverity.Info => ConsoleColor.White,
|
|
||||||
LogSeverity.Verbose => ConsoleColor.Blue,
|
|
||||||
LogSeverity.Debug => ConsoleColor.DarkBlue,
|
|
||||||
_ => ConsoleColor.Cyan,
|
|
||||||
};
|
|
||||||
Console.WriteLine(msg.ToString());
|
|
||||||
} finally {
|
|
||||||
Console.ForegroundColor = oldColor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (msg.Severity <= DiscordLogSeverity && LogChannel is ITextChannel log) {
|
|
||||||
await log.SendMessageAsync($"{msg.Severity}: {msg}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(LogMessage msg) => _ = Task.Run(() => LogAsync(msg));
|
public static void Log(LogMessage msg) {
|
||||||
|
lock (LogLock)
|
||||||
|
Console.WriteLine(msg.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing) {
|
protected virtual void Dispose(bool disposing) {
|
||||||
if (!disposedValue) {
|
if (!disposedValue) {
|
||||||
@ -339,29 +254,12 @@ public class Program : IDisposable, ICommandHandler<ResponseType>, IUserRoleMana
|
|||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RequireAdministrator(ulong user, string? message = null) {
|
|
||||||
if (!_administrators.Contains(user))
|
|
||||||
throw new ReplyException(message ?? "User is not authorized to access this command!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ActiveChannel {
|
|
||||||
public ActiveChannel(ITextChannel channel, IWebhook webhook) {
|
|
||||||
Channel = channel;
|
|
||||||
Webhook = webhook;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IWebhook Webhook { get; }
|
|
||||||
public ITextChannel Channel { get; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class ResponseType {
|
public abstract class ResponseType {
|
||||||
private static string DefaultDisplay<T>(T obj) => obj?.ToString() ?? throw new InvalidProgramException("ToString did not yield anything!");
|
private static string DefaultDisplay<T>(T obj) => obj?.ToString() ?? throw new InvalidProgramException("ToString did not yield anything!");
|
||||||
public static ResponseType AsString(string message) => new StringResponse(message);
|
public static ResponseType AsString(string message) => new StringResponse(message);
|
||||||
public static ResponseType FromChoice<T>(string query, IEnumerable<T> choice, Func<T, Task> resultHandler, Func<T, string>? display = null) => new ChoiceResponse<T>(query, choice, resultHandler, display ?? DefaultDisplay);
|
public static ResponseType FromChoice<T>(string query, IEnumerable<T> choice, Func<T, Task> resultHandler, Func<T, string>? display = null) => new ChoiceResponse<T>(query, choice, resultHandler, display ?? DefaultDisplay);
|
||||||
internal static ResponseType File(string path, string message) => new FileResponse(path, message);
|
|
||||||
|
|
||||||
public class StringResponse : ResponseType {
|
public class StringResponse : ResponseType {
|
||||||
public StringResponse(string message) => Message = message;
|
public StringResponse(string message) => Message = message;
|
||||||
public string Message { get; }
|
public string Message { get; }
|
||||||
@ -385,14 +283,4 @@ public abstract class ResponseType {
|
|||||||
_displayer = display;
|
_displayer = display;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileResponse : ResponseType {
|
|
||||||
public FileResponse(string path, string message) {
|
|
||||||
Path = path;
|
|
||||||
Message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Path { get; }
|
|
||||||
public string Message { get; }
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,26 +0,0 @@
|
|||||||
using Discord.WebSocket;
|
|
||||||
using MinecraftDiscordBot.Commands;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
public class ChatBoxService : CommandRouter {
|
|
||||||
private readonly ITaskWaitSource _taskSource;
|
|
||||||
public ChatBoxService(ITaskWaitSource taskSource) => _taskSource = taskSource;
|
|
||||||
|
|
||||||
public override string HelpTextPrefix => "!chat ";
|
|
||||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
|
||||||
=> throw new ReplyException($"The chat box cannot do '{method}'!");
|
|
||||||
|
|
||||||
private Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null)
|
|
||||||
=> RootCommandService.Method(_taskSource, methodName, parser, ct, parameters);
|
|
||||||
|
|
||||||
public Task<bool> SendMessageAsync(string message, string prefix, CancellationToken ct) => Method("send", RootCommandService.Deserialize<bool>(), ct, new() {
|
|
||||||
["message"] = message,
|
|
||||||
["prefix"] = prefix
|
|
||||||
});
|
|
||||||
public Task<bool> SendMessageToPlayerAsync(string message, string username, string prefix, CancellationToken ct) => Method("send", RootCommandService.Deserialize<bool>(), ct, new() {
|
|
||||||
["message"] = message,
|
|
||||||
["username"] = username,
|
|
||||||
["prefix"] = prefix
|
|
||||||
});
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
using Discord.WebSocket;
|
|
||||||
using MinecraftDiscordBot.Commands;
|
|
||||||
using MinecraftDiscordBot.Models;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
public class PlayerDetectorService : CommandRouter {
|
|
||||||
private readonly ITaskWaitSource _taskSource;
|
|
||||||
public PlayerDetectorService(ITaskWaitSource taskSource) => _taskSource = taskSource;
|
|
||||||
|
|
||||||
public override string HelpTextPrefix => "!pd ";
|
|
||||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
|
||||||
=> throw new ReplyException($"The player detector cannot do '{method}'!");
|
|
||||||
|
|
||||||
private Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null)
|
|
||||||
=> RootCommandService.Method(_taskSource, methodName, parser, ct, parameters);
|
|
||||||
|
|
||||||
public Task<string[]> GetOnlinePlayersAsync(CancellationToken ct) => Method("getonline", RootCommandService.Deserialize<string[]>(), ct);
|
|
||||||
public async Task<PlayerPosition> GetPlayerPosition(string username, CancellationToken ct)
|
|
||||||
=> (await FindPlayerAsync(username, ct)) ?? throw new ReplyException($"User '{username}' is not online!");
|
|
||||||
private Task<PlayerPosition?> FindPlayerAsync(string username, CancellationToken ct) => Method("whereis", i => JsonConvert.DeserializeObject<PlayerPosition?>(i), ct, new() {
|
|
||||||
["username"] = username
|
|
||||||
});
|
|
||||||
|
|
||||||
[CommandHandler("getonline", HelpText = "Get a list of online players.")]
|
|
||||||
public async Task<ResponseType> HandleOnlinePlayers(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> ResponseType.AsString($"The following players are currently online:\n{string.Join("\n", await GetOnlinePlayersAsync(ct))}");
|
|
||||||
[CommandHandler("whereis", HelpText = "Find a player in the world.")]
|
|
||||||
public async Task<ResponseType> HandleFindPlayers(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
if (parameters is not { Length: 1 }) throw new ReplyException($"Give me only one username!");
|
|
||||||
var username = parameters[0];
|
|
||||||
var player = await FindPlayerAsync(username, ct);
|
|
||||||
if (player is null) throw new ReplyException($"{username} is currently offline!");
|
|
||||||
return ResponseType.AsString($"{username} is at coordinates {player.X} {player.Y} {player.Z} in dimension {player.Dimension}.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,183 +0,0 @@
|
|||||||
using Discord;
|
|
||||||
using Discord.WebSocket;
|
|
||||||
using MinecraftDiscordBot.Commands;
|
|
||||||
using MinecraftDiscordBot.Models;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
public class RefinedStorageService : CommandRouter {
|
|
||||||
private readonly ITaskWaitSource _taskSource;
|
|
||||||
private readonly IUserRoleManager _roleManager;
|
|
||||||
public override string HelpTextPrefix => "!rs ";
|
|
||||||
public RefinedStorageService(ITaskWaitSource taskSource, IUserRoleManager roleManager) : base() {
|
|
||||||
_taskSource = taskSource;
|
|
||||||
_roleManager = roleManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
|
||||||
=> throw new ReplyException($"The RS system has no command '{method}'!");
|
|
||||||
|
|
||||||
private Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null)
|
|
||||||
=> RootCommandService.Method(_taskSource, methodName, parser, ct, parameters);
|
|
||||||
|
|
||||||
private const string CmdEnergyUsage = "energyusage";
|
|
||||||
private const string CmdEnergyStorage = "energystorage";
|
|
||||||
private const string CmdListItems = "listitems";
|
|
||||||
private const string CmdItemName = "itemname";
|
|
||||||
private const string CmdListFluids = "listfluids";
|
|
||||||
private const string CmdCraftItem = "craft";
|
|
||||||
private const string CmdGetItem = "getitem";
|
|
||||||
private const string CmdCommand = "command";
|
|
||||||
|
|
||||||
public async Task<int> GetEnergyUsageAsync(CancellationToken ct) => await Method(CmdEnergyUsage, int.Parse, ct);
|
|
||||||
public async Task<int> GetEnergyStorageAsync(CancellationToken ct) => await Method(CmdEnergyStorage, int.Parse, ct);
|
|
||||||
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) => await Method(CmdListItems, RootCommandService.Deserialize<IEnumerable<Item>>(), ct);
|
|
||||||
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) => await Method(CmdListFluids, RootCommandService.Deserialize<IEnumerable<Fluid>>(), ct);
|
|
||||||
public async Task<Item> GetItemDataAsync(string itemid, CancellationToken ct) => await Method(CmdGetItem, RootCommandService.Deserialize<Item>(), ct, new() {
|
|
||||||
["name"] = itemid
|
|
||||||
});
|
|
||||||
public async Task<Item> GetItemDataAsync(Md5Hash fingerprint, CancellationToken ct) => await Method(CmdGetItem, RootCommandService.Deserialize<Item>(), ct, new() {
|
|
||||||
["fingerprint"] = fingerprint.ToString()
|
|
||||||
});
|
|
||||||
public async Task<bool> CraftItemAsync(string itemid, int amount, CancellationToken ct) => await Method(CmdCraftItem, RootCommandService.Deserialize<bool>(), ct, new() {
|
|
||||||
["name"] = itemid,
|
|
||||||
["count"] = amount
|
|
||||||
});
|
|
||||||
public async Task<LuaPackedArray> RawCommandAsync(string command, CancellationToken ct) => await Method(CmdCommand, LuaPackedArray.Deserialize, ct, new() {
|
|
||||||
["command"] = command
|
|
||||||
});
|
|
||||||
|
|
||||||
private Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<string> filters, CancellationToken ct)
|
|
||||||
=> FilterItems(message, filters.Select(ItemFilter.Parse), ct);
|
|
||||||
|
|
||||||
private async Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<ItemFilter> filters, CancellationToken ct) {
|
|
||||||
var items = Items?.ToList().AsEnumerable();
|
|
||||||
if (items is null) items = (await RefreshItemList(ct)).ToList();
|
|
||||||
foreach (var filter in filters)
|
|
||||||
items = items.Where(filter.MatchItem);
|
|
||||||
return items.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<List<Item>> RefreshItemList(CancellationToken ct) {
|
|
||||||
var response = await ListItemsAsync(ct);
|
|
||||||
lock (_itemLock) {
|
|
||||||
Items = response.OrderByDescending(i => i.Amount).ToList();
|
|
||||||
return Items;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Item>? Items;
|
|
||||||
private readonly object _itemLock = new();
|
|
||||||
|
|
||||||
[CommandHandler(CmdEnergyStorage, HelpText = "Get the amount of energy stored in the RS system.")]
|
|
||||||
public async Task<ResponseType> HandleEnergyStorage(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> ResponseType.AsString($"Refined Storage system stores {await GetEnergyStorageAsync(ct)} RF/t");
|
|
||||||
[CommandHandler(CmdEnergyUsage, HelpText = "Get the amount of energy used by the RS system.")]
|
|
||||||
public async Task<ResponseType> HandleEnergyUsage(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> ResponseType.AsString($"Refined Storage system currently uses {await GetEnergyUsageAsync(ct)} RF/t");
|
|
||||||
[CommandHandler(CmdCraftItem, HelpText = "Craft a specific item given an item ID and optionally an amount.")]
|
|
||||||
public async Task<ResponseType> HandleCraftItem(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
var amount = 1;
|
|
||||||
string itemid;
|
|
||||||
if (parameters.Length is 1 or 2) {
|
|
||||||
itemid = parameters[0];
|
|
||||||
if (parameters.Length is 2)
|
|
||||||
amount = int.TryParse(parameters[1], out var value)
|
|
||||||
? value
|
|
||||||
: throw new ReplyException($"I expected an amount to craft, not '{parameters[1]}'!");
|
|
||||||
} else return parameters.Length is < 1
|
|
||||||
? throw new ReplyException("You have to give me at least an item name!")
|
|
||||||
: parameters.Length is > 2
|
|
||||||
? throw new ReplyException("Yo, those are way too many arguments! I want only item name and maybe an amount!")
|
|
||||||
: throw new InvalidOperationException($"Forgot to match parameter length {parameters.Length}!");
|
|
||||||
return await CraftItemAsync(itemid, amount, ct)
|
|
||||||
? ResponseType.AsString($"Alright, I'm starting to craft {amount} {itemid}.")
|
|
||||||
: ResponseType.AsString($"Nope, that somehow doesn't work!");
|
|
||||||
}
|
|
||||||
[CommandHandler(CmdGetItem, HelpText = "Get information about a specific item.")]
|
|
||||||
public async Task<ResponseType> HandleGetItemData(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
string itemid;
|
|
||||||
if (parameters.Length is not 1) throw new ReplyException($"I only want one name or fingerprint to search for, you gave me {parameters.Length} arguments!");
|
|
||||||
itemid = parameters[0];
|
|
||||||
var item = await (Md5Hash.TryParse(itemid, out var fingerprint)
|
|
||||||
? GetItemDataAsync(fingerprint, ct)
|
|
||||||
: GetItemDataAsync(itemid, ct));
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
sb.Append($"We currently have {item.Amount:n0} {item.CleanDisplayName}!");
|
|
||||||
if (item.Tags is not null and var tags) {
|
|
||||||
sb.AppendLine("\nThis item has the following tags:");
|
|
||||||
sb.AppendJoin('\n', tags.Select(tag => $"- {tag}"));
|
|
||||||
}
|
|
||||||
sb.Append($"\nRefer to this item with fingerprint {item.Fingerprint}");
|
|
||||||
return ResponseType.AsString(sb.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
[CommandHandler(CmdItemName, HelpText = "Filter items by name.")]
|
|
||||||
public async Task<ResponseType> HandleItemName(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
if (parameters.Length < 2) return ResponseType.AsString($"Usage: {CmdItemName} filters...");
|
|
||||||
else {
|
|
||||||
var items = await FilterItems(message, parameters[1..], ct);
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
sb.AppendLine("Did you mean:");
|
|
||||||
sb.AppendJoin("\n", items.Select(i => i.ToString()));
|
|
||||||
return ResponseType.AsString(sb.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[CommandHandler(CmdListFluids, HelpText = "Gets a list of fluids that are currently stored in the RS system.")]
|
|
||||||
public async Task<ResponseType> HandleFluidListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
sb.Append("The Refined Storage system stores those fluids:");
|
|
||||||
var fluids = await ListFluidsAsync(ct);
|
|
||||||
foreach (var fluid in fluids.OrderByDescending(i => i.Amount))
|
|
||||||
if (fluid.Amount > 10000) sb.AppendFormat("\n{0:n2} B of {1}", fluid.Amount / 1000.0f, fluid.DisplayName);
|
|
||||||
else sb.AppendFormat("\n{0:n0} mB of {1}", fluid.Amount, fluid.DisplayName);
|
|
||||||
return ResponseType.AsString(sb.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
[CommandHandler(CmdCommand, HelpText = "Runs a raw command on the RS system.")]
|
|
||||||
public async Task<ResponseType> HandleRawCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
_roleManager.RequireAdministrator(message.Author.Id, "You are not authorized to run raw commands on this instance!");
|
|
||||||
var command = string.Join(' ', parameters);
|
|
||||||
var response = await RawCommandAsync(command, ct);
|
|
||||||
return ResponseType.AsString(response.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
[CommandHandler(CmdListItems, HelpText = "Gets a list of items that are currently stored in the RS system.")]
|
|
||||||
public async Task<ResponseType> HandleItemListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
|
||||||
if (parameters.Length is 1 && parameters[0] == "full") return await SendFullItemList(message, ct);
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
sb.Append("The Refined Storage system currently stores these items:");
|
|
||||||
var items = await RefreshItemList(ct);
|
|
||||||
lock (_itemLock) {
|
|
||||||
var taken = 0;
|
|
||||||
foreach (var item in items) {
|
|
||||||
if (sb.Length > 500) break;
|
|
||||||
sb.Append('\n');
|
|
||||||
sb.Append(item.ToString());
|
|
||||||
taken++;
|
|
||||||
}
|
|
||||||
if (items.Count > taken) sb.AppendFormat("\nand {0:n0} more items.", items.Skip(taken).Sum(i => i.Amount));
|
|
||||||
}
|
|
||||||
return ResponseType.AsString(sb.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<ResponseType> SendFullItemList(SocketUserMessage message, CancellationToken ct) {
|
|
||||||
var path = await GetItemListFile(ct);
|
|
||||||
return ResponseType.File(path, $"{message.Author.Mention} Here you go:");
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<string> GetItemListFile(CancellationToken ct) {
|
|
||||||
var items = await RefreshItemList(ct);
|
|
||||||
var file = Path.Combine(Path.GetTempPath(), "itemlist.txt");
|
|
||||||
var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
|
|
||||||
using (var sw = new StreamWriter(fs, Encoding.UTF8)) {
|
|
||||||
await sw.WriteLineAsync("The RS System stores the following items:");
|
|
||||||
foreach (var item in items)
|
|
||||||
await sw.WriteLineAsync(item.DetailString);
|
|
||||||
};
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
using System.Runtime.Serialization;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
public class ReplyException : Exception {
|
|
||||||
public ReplyException() { }
|
|
||||||
public ReplyException(string message) : base(message) { }
|
|
||||||
public ReplyException(string message, Exception inner) : base(message, inner) { }
|
|
||||||
protected ReplyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
|
||||||
}
|
|
@ -1,128 +0,0 @@
|
|||||||
using Discord.WebSocket;
|
|
||||||
using Fleck;
|
|
||||||
using MinecraftDiscordBot.Commands;
|
|
||||||
using MinecraftDiscordBot.Models;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MinecraftDiscordBot.Services;
|
|
||||||
|
|
||||||
public delegate Task<TResponse> HandleCommandDelegate<TResponse>(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
|
||||||
public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
|
||||||
|
|
||||||
public class RootCommandService : CommandRouter, ITaskWaitSource {
|
|
||||||
protected IWebSocketConnection? _socketField;
|
|
||||||
public override string HelpTextPrefix => "!";
|
|
||||||
public RootCommandService(IUserRoleManager roleManager) : base() {
|
|
||||||
RefinedStorage = new RefinedStorageService(this, roleManager);
|
|
||||||
Players = new PlayerDetectorService(this);
|
|
||||||
Chat = new ChatBoxService(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<T> Method<T>(ITaskWaitSource taskSource, string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null) {
|
|
||||||
var waiter = taskSource.GetWaiter(parser, ct);
|
|
||||||
await taskSource.Send(new RequestMessage(waiter.ID, methodName, parameters));
|
|
||||||
return await waiter.Task;
|
|
||||||
}
|
|
||||||
|
|
||||||
public event EventHandler<ChatEvent>? ChatMessageReceived;
|
|
||||||
public event EventHandler<PlayerStatusEvent>? PlayerStatusChanged;
|
|
||||||
public event EventHandler<PeripheralAttachEvent>? PeripheralAttached;
|
|
||||||
public event EventHandler<PeripheralDetachEvent>? PeripheralDetached;
|
|
||||||
public event EventHandler<IWebSocketConnection?>? SocketChanged;
|
|
||||||
|
|
||||||
public IWebSocketConnection? Socket {
|
|
||||||
get => _socketField; set {
|
|
||||||
if (_socketField != value) {
|
|
||||||
_socketField = value;
|
|
||||||
if (value is not null) value.OnMessage = OnMessage;
|
|
||||||
SocketChanged?.Invoke(this, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public RefinedStorageService RefinedStorage { get; }
|
|
||||||
public PlayerDetectorService Players { get; }
|
|
||||||
public ChatBoxService Chat { get; }
|
|
||||||
|
|
||||||
private void OnMessage(string message) {
|
|
||||||
switch (Message.Deserialize(message)) {
|
|
||||||
case ChatEvent msg:
|
|
||||||
ChatMessageReceived?.Invoke(this, msg);
|
|
||||||
break;
|
|
||||||
case PlayerStatusEvent msg:
|
|
||||||
PlayerStatusChanged?.Invoke(this, msg);
|
|
||||||
break;
|
|
||||||
case PeripheralAttachEvent msg:
|
|
||||||
PeripheralAttached?.Invoke(this, msg);
|
|
||||||
break;
|
|
||||||
case PeripheralDetachEvent msg:
|
|
||||||
PeripheralDetached?.Invoke(this, msg);
|
|
||||||
break;
|
|
||||||
case ReplyMessage msg:
|
|
||||||
IChunkWaiter? waiter;
|
|
||||||
lock (_syncRoot) if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
|
||||||
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
waiter.SetResultState(msg.State);
|
|
||||||
waiter.AddChunk(msg.Chunk, msg.Total, msg.Result);
|
|
||||||
if (waiter.Finished || waiter.IsCancellationRequested)
|
|
||||||
lock (_syncRoot)
|
|
||||||
_waits.Remove(waiter.ID);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Program.LogInfo(Program.WebSocketSource, $"Received unhandled message: {message}!");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task Send(string message) => (Socket ?? throw new ReplyException("Minecraft server is not available!")).Send(message);
|
|
||||||
public Task Send(Message message) => Send(JsonConvert.SerializeObject(message));
|
|
||||||
private readonly object _syncRoot = new();
|
|
||||||
private readonly Dictionary<int, IChunkWaiter> _waits = new();
|
|
||||||
private readonly Random _rnd = new();
|
|
||||||
|
|
||||||
private int GetFreeId() {
|
|
||||||
var attempts = 0;
|
|
||||||
while (true) {
|
|
||||||
var id = _rnd.Next();
|
|
||||||
if (!_waits.ContainsKey(id))
|
|
||||||
return id;
|
|
||||||
Program.LogWarningAsync(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
|
|
||||||
ChunkWaiter<T> waiter;
|
|
||||||
lock (_syncRoot) {
|
|
||||||
waiter = new ChunkWaiter<T>(GetFreeId(), resultParser, ct);
|
|
||||||
_waits.Add(waiter.ID, waiter);
|
|
||||||
}
|
|
||||||
return waiter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<Dictionary<string, Peripheral>> GetPeripherals(CancellationToken ct) => Method(this, "peripherals", Deserialize<Dictionary<string, Peripheral>>(), ct);
|
|
||||||
[CommandHandler("rs", HelpText = "Provides some commands for interacting with the Refined Storage system.")]
|
|
||||||
public Task<ResponseType> RefinedStorageHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> RefinedStorage.HandleCommand(message, parameters, ct);
|
|
||||||
[CommandHandler("peripherals", HelpText = "Gets a list of peripherals that are attached.")]
|
|
||||||
public async Task<ResponseType> HandleGetPeripherals(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> ResponseType.AsString(string.Join("\n", (await GetPeripherals(ct)).Values.Select(i => $"On side {i.Side}: {i.Type}")));
|
|
||||||
[CommandHandler("pd", HelpText = "Provides some commands for interacting with the Player Detector.")]
|
|
||||||
public Task<ResponseType> PlayerDetectorHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> Players.HandleCommand(message, parameters, ct);
|
|
||||||
[CommandHandler("chat", HelpText = "Provides some commands for chatting.")]
|
|
||||||
public Task<ResponseType> ChatBoxHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
||||||
=> Chat.HandleCommand(message, parameters, ct);
|
|
||||||
|
|
||||||
public static Func<string, T> Deserialize<T>() => msg
|
|
||||||
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
|
||||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
|
||||||
=> throw new ReplyException($"What the fuck do you mean by '{method}'?");
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ITaskWaitSource {
|
|
||||||
ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct);
|
|
||||||
Task Send(Message requestMessage);
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
namespace MinecraftDiscordBot;
|
|
||||||
|
|
||||||
public class TimeoutTokenProvider : ITokenProvider {
|
|
||||||
public TimeoutTokenProvider(int instanceId, int timeoutSeconds, ICipher? cipher = null) {
|
|
||||||
InstancePrefix = Convert.ToHexString(BitConverter.GetBytes(instanceId));
|
|
||||||
_timeout = timeoutSeconds;
|
|
||||||
_cipher = cipher ?? new AesCipher();
|
|
||||||
}
|
|
||||||
private readonly ICipher _cipher;
|
|
||||||
private readonly int _timeout;
|
|
||||||
public string InstancePrefix { get; }
|
|
||||||
public bool VerifyToken(string token) {
|
|
||||||
if (!token.StartsWith(InstancePrefix)) return false;
|
|
||||||
token = token[InstancePrefix.Length..];
|
|
||||||
byte[] data;
|
|
||||||
try {
|
|
||||||
data = _cipher.Decrypt(Convert.FromHexString(token));
|
|
||||||
} catch (Exception e) {
|
|
||||||
Program.LogError("TokenProvider", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));
|
|
||||||
return when >= DateTime.UtcNow.AddSeconds(-_timeout);
|
|
||||||
}
|
|
||||||
public string GenerateToken() {
|
|
||||||
var time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
|
|
||||||
var key = Guid.NewGuid().ToByteArray();
|
|
||||||
var token = InstancePrefix + Convert.ToHexString(_cipher.Encrypt(time.Concat(key).ToArray()));
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ITokenProvider {
|
|
||||||
string GenerateToken();
|
|
||||||
bool VerifyToken(string token);
|
|
||||||
}
|
|
32
build.py
32
build.py
@ -2,12 +2,11 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import argparse
|
import argparse
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
import re
|
|
||||||
|
|
||||||
dockercmd = 'docker'
|
dockercmd = 'docker'
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Create custom recumock images.')
|
parser = argparse.ArgumentParser(description='Create custom recumock images.')
|
||||||
parser.add_argument('tags', metavar='TAG', nargs='*', help='Version tags to build.')
|
parser.add_argument('tags', metavar='TAG', nargs='+', help='Version tags to build.')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -16,31 +15,20 @@ platforms = ['linux/amd64', 'linux/arm64', 'linux/arm/v7']
|
|||||||
def pull(image):
|
def pull(image):
|
||||||
subprocess.run([dockercmd, 'pull', baseimage], check=True)
|
subprocess.run([dockercmd, 'pull', baseimage], check=True)
|
||||||
|
|
||||||
def build(images, directory, platforms, build_args = None):
|
def build(image, directory, platforms, build_args = None):
|
||||||
if build_args is None:
|
if build_args is None:
|
||||||
build_args = []
|
build_args = []
|
||||||
build_args = list(chain.from_iterable(['--build-arg', f'{arg}={val}'] for (arg, val) in build_args))
|
build_args = list(chain.from_iterable(['--build-arg', f'{arg}={val}'] for (arg, val) in build_args))
|
||||||
tags = list(chain.from_iterable(['-t', image] for image in images))
|
|
||||||
platformlist = ','.join(platforms)
|
platformlist = ','.join(platforms)
|
||||||
command = [dockercmd, 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platformlist, *tags] + build_args + ['--push', directory]
|
subprocess.run([dockercmd, 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platformlist, '-t', image] + build_args + ['--push', directory], check=True)
|
||||||
print(' '.join(command))
|
|
||||||
subprocess.run(command, check=True)
|
|
||||||
|
|
||||||
def version_from_project():
|
|
||||||
with open(r'MinecraftDiscordBot\MinecraftDiscordBot.csproj', 'r') as f:
|
|
||||||
project = f.read()
|
|
||||||
|
|
||||||
regex = r"<Version>\s*([^<]*?)\s*<\/Version>"
|
for tag in args.tags:
|
||||||
matches = re.search(regex, project, re.IGNORECASE)
|
targetimage = f'chenio/mcdiscordbot:{tag}'
|
||||||
if not matches:
|
baseimage = f'mcr.microsoft.com/dotnet/runtime:6.0'
|
||||||
raise Exception("Could not read version from project file!")
|
|
||||||
return matches.group(1)
|
|
||||||
|
|
||||||
if len(args.tags) == 0:
|
#print(f'Pulling base image {baseimage}')
|
||||||
args.tags.append(version_from_project())
|
#pull(baseimage)
|
||||||
|
print(f'Building image {targetimage} from {baseimage}.')
|
||||||
|
build(targetimage, '.', platforms, [('TAG', tag)])
|
||||||
|
|
||||||
for version in args.tags:
|
|
||||||
parts = version.split('.')
|
|
||||||
tags = list('.'.join(parts[:i]) for i in range(1, len(parts) + 1))
|
|
||||||
tags.append('latest')
|
|
||||||
build([f'chenio/mcdiscordbot:{tag}' for tag in tags], '.', platforms)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user