游戏内Markdown支持,Discord新增/log命令

This commit is contained in:
Xujiayao 2022-03-27 00:55:15 +08:00
parent 3c48462d10
commit 3222c93fcc
5 changed files with 66 additions and 7 deletions

View file

@ -39,7 +39,6 @@ public class Config {
public List<String> adminsIds = new ArrayList<>();
// TODO Link players to Discord accounts
// TODO Send latest log
// TODO Multi Server
// Permissions=183296
}

View file

@ -9,6 +9,7 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.utils.MarkdownSanitizer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TextColor;
@ -18,8 +19,10 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import top.xujiayao.mcdiscordchat.utils.ConfigManager;
import top.xujiayao.mcdiscordchat.utils.MarkdownParser;
import top.xujiayao.mcdiscordchat.utils.Utils;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@ -96,6 +99,7 @@ public class DiscordEventListener extends ListenerAdapter {
/update | Check for update
/reload | Reload MCDiscordChat config file (admin only)
/console <command> | Execute a command in the server console (admin only)
/log | Get the latest server log (admin only)
/stop | Stop the server (admin only)
```""" : """
```
@ -105,6 +109,7 @@ public class DiscordEventListener extends ListenerAdapter {
/update | 检查更新
/reload | 重新加载 MCDiscordChat 配置文件仅限管理员
/console <command> | 在服务器控制台中执行命令仅限管理员
/log | 获取服务器最新日志仅限管理员
/stop | 停止服务器仅限管理员
```""").queue();
case "update" -> e.getHook().sendMessage(Utils.checkUpdate(true)).queue();
@ -148,6 +153,13 @@ public class DiscordEventListener extends ListenerAdapter {
e.getHook().sendMessage(CONFIG.generic.useEngInsteadOfChin ? "**You do not have permission to use this command!**" : "**你没有权限使用此命令!**").queue();
}
}
case "log" -> {
if (CONFIG.generic.adminsIds.contains(Objects.requireNonNull(e.getMember()).getId())) {
e.getHook().sendFile(new File(FabricLoader.getInstance().getGameDir().toFile(), "logs/latest.log")).queue();
} else {
e.getHook().sendMessage(CONFIG.generic.useEngInsteadOfChin ? "**You do not have permission to use this command!**" : "**你没有权限使用此命令!**").queue();
}
}
case "stop" -> {
if (CONFIG.generic.adminsIds.contains(Objects.requireNonNull(e.getMember()).getId())) {
e.getHook().sendMessage(CONFIG.generic.useEngInsteadOfChin ? "**Stopping the server!**" : "**正在停止服务器!**")
@ -192,8 +204,6 @@ public class DiscordEventListener extends ListenerAdapter {
}
}
// TODO 处理Markdownmessage
if (!e.getMessage().getAttachments().isEmpty()) {
if (!e.getMessage().getContentDisplay().isBlank()) {
message.append(" ");
@ -247,6 +257,6 @@ public class DiscordEventListener extends ListenerAdapter {
player -> player.sendMessage(new LiteralText("")
.append(mcdcText)
.append(roleText)
.append(Formatting.GRAY + finalMessage.toString()), false));
.append(Formatting.GRAY + MarkdownParser.parseMarkdown(finalMessage.toString())), false));
}
}

View file

@ -31,6 +31,7 @@ import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.xujiayao.mcdiscordchat.utils.MarkdownParser;
import java.util.ArrayList;
import java.util.List;
@ -81,8 +82,6 @@ public abstract class MixinServerPlayNetworkHandler {
String contentToDiscord = message.getRaw();
String contentToMinecraft = message.getRaw();
// TODO 处理MarkdowncontentToMinecraft
Text text = new TranslatableText("chat.type.text", player.getDisplayName(), contentToDiscord);
JsonObject json = new Gson().fromJson(Text.Serializer.toJson(text), JsonObject.class);
json.getAsJsonArray("with").remove(1);
@ -116,7 +115,7 @@ public abstract class MixinServerPlayNetworkHandler {
}
}
json.getAsJsonArray("with").add(contentToMinecraft);
json.getAsJsonArray("with").add(MarkdownParser.parseMarkdown(contentToMinecraft));
Text finalText = Text.Serializer.fromJson(json.toString());
server.getPlayerManager().broadcast(finalText, MessageType.CHAT, player.getUuid());
ci.cancel();

View file

@ -0,0 +1,50 @@
package top.xujiayao.mcdiscordchat.utils;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Xujiayao
*/
public class MarkdownParser {
// TODO Rewrite MarkdownParser
public static String parseMarkdown(String message) {
message = replaceWith(message, "(?<!\\\\)\\*\\*", Formatting.BOLD.toString(), Formatting.RESET.toString());
message = replaceWith(message, "(?<!\\\\)\\*", Formatting.ITALIC.toString(), Formatting.RESET.toString());
message = replaceWith(message, "(?<!\\\\)__", Formatting.UNDERLINE.toString(), Formatting.RESET.toString());
message = replaceWith(message, "(?<!\\\\)_", Formatting.ITALIC.toString(), Formatting.RESET.toString());
message = replaceWith(message, "(?<!\\\\)~~", Formatting.STRIKETHROUGH.toString(), Formatting.RESET.toString());
message = message.replaceAll("\\*", "*").replaceAll("\\_", "_").replaceAll("\\~", "~");
return message;
}
private static String replaceWith(String message, String quot, String pre, String suf) {
String part = message;
for (String str : getMatches(message, quot + "(.+?)" + quot)) {
part = part.replaceFirst(quot + Pattern.quote(str) + quot, pre + str + suf);
}
return part;
}
private static List<String> getMatches(String string, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
List<String> matches = new ArrayList<>();
while (matcher.find()) {
matches.add(matcher.group(1));
}
return matches;
}
}

View file

@ -134,6 +134,7 @@ public class Utils {
.addCommands(Commands.slash("reload", CONFIG.generic.useEngInsteadOfChin ? "Reload MCDiscordChat config file (admin only)" : "重新加载 MCDiscordChat 配置文件(仅限管理员)"))
.addCommands(Commands.slash("console", CONFIG.generic.useEngInsteadOfChin ? "Execute a command in the server console (admin only)" : "在服务器控制台中执行指令(仅限管理员)")
.addOption(OptionType.STRING, "command", CONFIG.generic.useEngInsteadOfChin ? "Command to execute" : "要执行的命令", true))
.addCommands(Commands.slash("log", CONFIG.generic.useEngInsteadOfChin ? "Get the latest server log (admin only)" : "获取服务器最新日志(仅限管理员)"))
.addCommands(Commands.slash("stop", CONFIG.generic.useEngInsteadOfChin ? "Stop the server (admin only)" : "停止服务器(仅限管理员)"))
// TODO !scoreboard <type> <id>: Query the player scoreboard for this statistic
.queue();