Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Lqrs committed Nov 24, 2023
1 parent 3a6f826 commit 0ebe782
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 37 deletions.
54 changes: 41 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
# Fabricord
A modern message style like DiscordSRV will be reproduced as a Fabric version mod.

![Discord](https://img.shields.io/discord/1138813465688031283?logo=discord&logoColor=white&style=plastic) ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Elysium-7/Fabricord/build.yml?style=plastic&logo=github&logoColor=white) ![GitHub release (with filter)](https://img.shields.io/github/v/release/Elysium-7/Fabricord?style=plastic) ![GitHub commit activity (branch)](https://img.shields.io/github/commit-activity/t/Elysium-7/Fabricord?style=plastic)



# How to read the version
## Example Version 1.0+1-1.20.1
### 1.0 -> Major version
Integer is used for major or special updates,
and first decimal place is used for minor bug fixes or adjustments.
### +1 -> Minor version
The number goes up with each commit. Has a role comparable to that of a build number.
### -1.20.1 -> MC version
Easy to understand!
![Discord](https://img.shields.io/discord/1177249059623411742?logo=discord&logoColor=white&style=plastic) ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Elysium-7/Fabricord/build.yml?style=plastic&logo=github&logoColor=white) ![GitHub release (with filter)](https://img.shields.io/github/v/release/Elysium-7/Fabricord?style=plastic) ![GitHub commit activity (branch)](https://img.shields.io/github/commit-activity/t/Elysium-7/Fabricord?style=plastic)


## Overview
Fabricord is a mod for Fabric servers that bridges the gap between Minecraft and Discord. It enables seamless integration, allowing messages from Discord to be read directly within the Minecraft server. This mod enhances the communication experience, making it more interactive and engaging.

## Features
- Read Discord messages in Minecraft server chat.
- Modern and visually appealing message presentation.
- Easy referencing of Discord user IDs within Minecraft.

## Installation
It is very easy to set up!
The data folder and configuration file will be generated upon initial startup. After that, the server is stopped,
make sure that the "fabricord" folder is in the server root directory. It contains the configuration file (config.yml).

<config.yml>
```yml
# Thank you for introducing the mod.
# You can bridge Discord and Minecraft chat!

# Click here for various links↓.
# GitHub -> https://github.com/Elysium-7/Fabricord
# Discord -> https://discord.gg/s6wzAHJKA3

# BotToken for use with Discord.
# To get a BotToken -> https://discord.com/developers/applications
BotToken: ""

# Channel for sending Minecraft logs.
Log_Channel_ID: ""

# The bot's online status can be changed.
# "ONLINE", "IDLE" and "DO_NOT_DISTURB" are available.
# If blank, "ONLINE" is automatically select.
Bot_Online_Status: ""

```
## License
Fabricord is available under the [MIT License](https://github.com/Elysium-7/Fabricord/blob/main/LICENSE).
25 changes: 20 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ base {

repositories {
mavenCentral()
maven {
name = "CodeMC"
url = uri("https://repo.codemc.io/repository/maven-public/")
}
}

loom {
Expand Down Expand Up @@ -46,13 +50,24 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
includeInJar 'org.yaml:snakeyaml:2.0'
includeInJar 'net.dv8tion:JDA:5.0.0-beta.12'
implementation 'net.dv8tion:JDA:5.0.0-beta.12'

implementation 'org.yaml:snakeyaml:2.0'
implementation 'net.java.dev.jna:jna:4.5.0'
includeInJar 'net.java.dev.jna:jna:4.5.0'
includeInJar 'org.yaml:snakeyaml:2.0'

implementation ('net.dv8tion:JDA:5.0.0-beta.12'){
exclude(group: 'net.java.dev.jna', module: 'jna')
}
includeInJar ('net.dv8tion:JDA:5.0.0-beta.12'){
exclude(group: 'net.java.dev.jna', module: 'jna')
}

implementation "net.kyori:adventure-api:4.14.0"
includeInJar "net.kyori:adventure-api:4.14.0"

implementation "net.kyori:adventure-text-serializer-gson:4.14.0"
includeInJar "net.kyori:adventure-text-serializer-gson:4.14.0"

implementation 'com.google.code.gson:gson:2.8.9'
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.23

# Mod Properties
mod_version=1.0+1-1.20.1
mod_version=2.0-1.20.1
maven_group=net.elysium.mod.fabricord
archives_base_name=fabricord

Expand Down
56 changes: 40 additions & 16 deletions src/main/java/net/elysium/mod/fabricord/discord/DiscordBot.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package net.elysium.mod.fabricord.discord;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import net.dv8tion.jda.api.*;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.elysium.mod.fabricord.ConfigManager;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Comparator;

public class DiscordBot {

private final ListenerAdapter discordListener = new ListenerAdapter() {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
handleDiscordMessage(event);
handleDiscordMessage(event);
}
};

Expand Down Expand Up @@ -79,21 +88,36 @@ private void sendToDiscord(String message) {

public void handleDiscordMessage(MessageReceivedEvent event) {
String channelId = ConfigManager.getLogChannelID();

// Check if the message is from the specified channel
if (!event.getChannel().getId().equals(channelId)) return;

if (event.getAuthor().isBot()) return;
if (!event.getChannel().getId().equals(channelId) || event.getAuthor().isBot()) {
return;
}

var member = event.getMember();
String memberName = member != null ? member.getUser().getName() : "Unknown Name";

Text textMessage = Text.of(("[Discord" + memberName + "]" + " » " + event.getMessage().getContentDisplay()));

// Sending the message to all players on the server
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
player.sendMessage(textMessage, false);
}
var memberName = member != null ? member.getUser().getName() : "Unknown Name";
var memberId = member != null ? member.getUser().getId() : "00000000000000000000";
var idSuggest = "<@" + memberId + ">";
var highestRole = member != null ? member.getRoles().stream().max(Comparator.comparingInt(Role::getPosition)).orElse(null) : null;
var roleName = highestRole != null ? highestRole.getName() : "Unknown";
var roleColor = highestRole != null && highestRole.getColor() != null ? highestRole.getColor() : Color.WHITE;
var kyoriRoleColor = TextColor.color(roleColor.getRed(), roleColor.getGreen(), roleColor.getBlue());

var componentMessage = Component.text("[", TextColor.color(0xFFFFFF))
.append(Component.text("Discord", TextColor.color(0x55CDFC)))
.append(Component.text(" | ", TextColor.color(0xFFFFFF)))
.append(Component.text(roleName, kyoriRoleColor))
.append(Component.text("]", TextColor.color(0xFFFFFF)))
.append(Component.text(" "))
.append(Component.text(memberName)
.clickEvent(ClickEvent.suggestCommand(idSuggest)))
.append(Component.text(" » " + event.getMessage().getContentDisplay()));

String json = GsonComponentSerializer.gson().serialize(componentMessage);

net.minecraft.text.Text textMessage = net.minecraft.text.Text.Serializer.fromJson(json);

server.getPlayerManager().getPlayerList().forEach(player ->
player.sendMessage(textMessage, false)
);
}

public void registerEventListeners() {
Expand All @@ -113,7 +137,7 @@ public void registerEventListeners() {
ServerMessageEvents.CHAT_MESSAGE.register((Chatmessage, sender, parameters) -> {
String user = sender.getDisplayName().getString();
String message = Chatmessage.getContent().getString();
String chatMessage = String.format("**%s** » %s", user, message);
String chatMessage = String.format("%s » %s", user, message);
sendToDiscord(chatMessage);
});

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# You can bridge Discord and Minecraft chat!

# Click here for various links↓.
# GitHub -> https://github.com/Elysium-7/VariousPlugin
# Discord -> https://discord.gg/cWd3Cz6dkV
# GitHub -> https://github.com/Elysium-7/Fabricord
# Discord -> https://discord.gg/s6wzAHJKA3

# BotToken for use with Discord.
# To get a BotToken -> https://discord.com/developers/applications
Expand Down

0 comments on commit 0ebe782

Please sign in to comment.