Skip to content

Commit

Permalink
实现MCDC同Discord频道多服务器运行
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Sep 24, 2021
1 parent 017537a commit 64cd87c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 44 deletions.
9 changes: 0 additions & 9 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

添加关于功能

smp里显示cmp里的信息,他不会说来自cmp,而是直接写来自discord。
蛤,那要怎么搞,discord显示的用户名前面加前缀还是咋样,不然mod怎么识别。
正版玩家能不能登录两个不同的服务器?同时登录,开两个客户端那种。
你可以写多一个子mod。
因为这样他会判断这个信息来自discord,mc里还是有discord前缀
https://media.discordapp.net/attachments/792407823295184906/877506119675813918/unknown.png

添加检查更新模块

https://discord.com/oauth2/authorize?client_id=792421947911831562&permissions=248832&scope=bot

远程开服
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.10.2'
id 'fabric-loom' version '0.10.17'
}

sourceCompatibility = JavaVersion.VERSION_16
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.59
loader_version=0.11.6
yarn_mappings=1.17.1+build.61
loader_version=0.11.7
# Mod Properties
mod_version=1.17-1.9.3
mod_version=1.17-1.10.0
maven_group=top.xujiayao
archives_base_name=MCDiscordChat
# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.40.0+1.17
fabric_version=0.40.1+1.17
30 changes: 21 additions & 9 deletions src/main/java/top/xujiayao/mcdiscordchat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ public static class Generic {
@Expose
public boolean switchLanguageFromChinToEng = false;

// Set if using more than two MCDiscordChat in one Discord server
@Expose
public boolean multiServer = false;

// Server display name
// e.g. SMP
@Expose
public String serverDisplayName = "";

// Bot Token; see https://discordpy.readthedocs.io/en/latest/discord.html
public String botToken = "NzkyNDIxOTQ3OTExODMxNTYy.X-d" + "ecg.p3aOW_kRDvA-CctX3HLek4oz-Co";
@Expose
public String botToken = "";

// Bot Game Status; What will be displayed on the bot's game status (leave empty for nothing)
@Expose
Expand Down Expand Up @@ -162,20 +172,21 @@ public static class TextsZH {
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// ---
// Available placeholders:
// %discordname% | User nickname in the discord server
// %servername% | 'Discord' or 'server name when using multi-server'
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String coloredText = "[Discord] ";
public String coloredText = "[%servername%] ";

// Discord -> Minecraft
// Colorless (white) part of the message
// I think you already know what it is by the other comment
// ---
// Available placeholders:
// %discordname% | Nickname of the user in the discord server
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String colorlessText = "<%discordname%> %message%";
public String colorlessText = "<%name%> %message%";

// Replaces the § symbol with & in any discord message to avoid formatted messages
@Expose
Expand Down Expand Up @@ -256,20 +267,21 @@ public static class TextsEN {
// This part of the message will receive the same color as the role in the discord, comes before the colorless part
// ---
// Available placeholders:
// %discordname% | User nickname in the discord server
// %servername% | 'Discord' or 'server name when using multi-server'
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String coloredText = "[Discord] ";
public String coloredText = "[%servername%] ";

// Discord -> Minecraft
// Colorless (white) part of the message
// I think you already know what it is by the other comment
// ---
// Available placeholders:
// %discordname% | Nickname of the user in the discord server
// %name% | Nickname of the user in the discord server (becomes player name when using multi-server)
// %message% | The message
@Expose
public String colorlessText = "<%discordname%> %message%";
public String colorlessText = "<%name%> %message%";

// Replaces the § symbol with & in any discord message to avoid formatted messages
@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ public void onMessageReceived(@NotNull MessageReceivedEvent e) {
MinecraftServer server = getServer();

if (e.getAuthor() != e.getJDA().getSelfUser()
&& !e.getAuthor().isBot()
&& e.getChannel().getId().equals(Main.config.generic.channelId)
&& server != null) {
if (Main.config.generic.multiServer && e.isWebhookMessage()) {
if (Main.config.generic.serverDisplayName.equals(e.getAuthor().getName().substring(1, e.getAuthor().getName().indexOf("] ")))) {
return;
}
} else {
if (e.getAuthor().isBot()) {
return;
}
}

if (Main.config.generic.bannedDiscord.contains(e.getAuthor().getId())
&& !e.getAuthor().getId().equals("769470378073653269")
&& !Main.config.generic.superAdminsIds.contains(e.getAuthor().getId())
Expand Down Expand Up @@ -246,24 +255,49 @@ public void onMessageReceived(@NotNull MessageReceivedEvent e) {
e.getChannel().sendMessage(adminList.toString()).queue();
}

LiteralText coloredText = new LiteralText(Main.texts.coloredText()
.replace("%discordname%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : "")));
coloredText.setStyle(coloredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
coloredText.setStyle(coloredText.getStyle().withBold(true));

LiteralText colorlessText = new LiteralText(Main.texts.colorlessText()
.replace("%discordname%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", MarkdownParser.parseMarkdown(e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : ""))));
colorlessText.setStyle(colorlessText.getStyle().withColor(TextColor.fromFormatting(Formatting.GRAY)));
LiteralText coloredText;
LiteralText colorlessText;

if (e.isWebhookMessage()) {
coloredText = new LiteralText(Main.texts.coloredText()
.replace("%servername%", e.getAuthor().getName().substring(1, e.getAuthor().getName().indexOf("] ")))
.replace("%message%", e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : "")));
coloredText.setStyle(coloredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
coloredText.setStyle(coloredText.getStyle().withBold(true));

colorlessText = new LiteralText(Main.texts.colorlessText()
.replace("%name%", e.getAuthor().getName().substring(e.getAuthor().getName().indexOf("] ") + 2))
.replace("%message%", MarkdownParser.parseMarkdown(e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : ""))));
colorlessText.setStyle(colorlessText.getStyle().withColor(TextColor.fromFormatting(Formatting.GRAY)));
} else {
coloredText = new LiteralText(Main.texts.coloredText()
.replace("%servername%", "Discord")
.replace("%name%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : "")));
coloredText.setStyle(coloredText.getStyle().withColor(TextColor.fromFormatting(Formatting.BLUE)));
coloredText.setStyle(coloredText.getStyle().withBold(true));

colorlessText = new LiteralText(Main.texts.colorlessText()
.replace("%name%", Objects.requireNonNull(e.getMember()).getEffectiveName())
.replace("%message%", MarkdownParser.parseMarkdown(e.getMessage().getContentDisplay()
.replace("§", Main.texts.removeVanillaFormattingFromDiscord() ? "&" : "§")
.replace("\n", Main.texts.removeLineBreakFromDiscord() ? " " : "\n")
+ ((!e.getMessage().getAttachments().isEmpty()) ? " <att>" : "")
+ ((!e.getMessage().getEmbeds().isEmpty()) ? " <embed>" : ""))));
colorlessText.setStyle(colorlessText.getStyle().withColor(TextColor.fromFormatting(Formatting.GRAY)));
}

server.getPlayerManager().getPlayerList().forEach(
serverPlayerEntity -> serverPlayerEntity.sendMessage(new LiteralText("").append(coloredText).append(colorlessText), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void init() {
}

JSONObject body = new JSONObject();
body.put("username", playerEntity.getEntityName());
body.put("username", (Main.config.generic.multiServer ? "[" + Main.config.generic.serverDisplayName + "] " + playerEntity.getEntityName() : playerEntity.getEntityName()));
body.put("avatar_url", "https://mc-heads.net/avatar/" + (Main.config.generic.useUUIDInsteadNickname ? playerEntity.getUuid() : playerEntity.getEntityName()));

JSONObject allowedMentions = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package top.xujiayao.mcdiscordchat.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.loader.api.FabricLoader;
Expand Down

0 comments on commit 64cd87c

Please sign in to comment.