-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Minecraft ↔ Discord Chat Bridge | ||
|
||
Bridges Minecraft and Discord chats. | ||
|
||
I wrote this in like one hour, it works, don't judge code pls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/DiscordListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package edu.berkeley.bmt.minecraft_discord_bridge; | ||
|
||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import net.kyori.adventure.text.Component; | ||
|
||
public class DiscordListener extends ListenerAdapter { | ||
private Plugin plugin; | ||
private String channelId; | ||
|
||
public DiscordListener(Plugin plugin, String channelId) { | ||
this.plugin = plugin; | ||
this.channelId = channelId; | ||
} | ||
|
||
@Override | ||
public void onMessageReceived(MessageReceivedEvent event) { | ||
plugin.getLogger().info("Received message: " + event.getMessage().getContentDisplay()); | ||
|
||
if (event.getAuthor().isBot()) { | ||
return; | ||
} | ||
if (!event.getGuildChannel().getId().equals(channelId)) { | ||
return; | ||
} | ||
|
||
String msg = String.format("《%s》 %s", event.getAuthor().getAsTag(), event.getMessage().getContentDisplay()); | ||
plugin.getServer().broadcast(Component.text(msg)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/MinecraftListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package edu.berkeley.bmt.minecraft_discord_bridge; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.PlayerDeathEvent; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
|
||
import club.minnced.discord.webhook.WebhookClient; | ||
import club.minnced.discord.webhook.send.WebhookMessageBuilder; | ||
import io.papermc.paper.event.player.AsyncChatEvent; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; | ||
|
||
public class MinecraftListener implements Listener { | ||
private WebhookClient webhookClient; | ||
|
||
public MinecraftListener(JDA jda, WebhookClient webhookClient) { | ||
this.webhookClient = webhookClient; | ||
} | ||
|
||
private String componentToString(Component component) { | ||
PlainTextComponentSerializer plainSerializer = PlainTextComponentSerializer.plainText(); | ||
return plainSerializer.serialize(component); | ||
} | ||
|
||
@EventHandler | ||
public void onChat(AsyncChatEvent event) { | ||
Player player = event.getPlayer(); | ||
WebhookMessageBuilder builder = new WebhookMessageBuilder(); | ||
builder.setUsername(player.getName()); | ||
builder.setAvatarUrl(String.format("https://crafatar.com/avatars/%s?overlay", player.getUniqueId().toString())); | ||
builder.setContent(componentToString(event.message())); | ||
webhookClient.send(builder.build()); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinEvent event) { | ||
webhookClient.send(componentToString(event.joinMessage())); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerLeave(PlayerQuitEvent event) { | ||
webhookClient.send(componentToString(event.quitMessage())); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerDeath(PlayerDeathEvent event) { | ||
webhookClient.send(componentToString(event.deathMessage())); | ||
} | ||
} |
31 changes: 25 additions & 6 deletions
31
src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/Plugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,34 @@ | ||
package edu.berkeley.bmt.minecraft_discord_bridge; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import io.papermc.paper.event.player.AsyncChatEvent; | ||
import club.minnced.discord.webhook.WebhookClient; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.JDABuilder; | ||
import net.dv8tion.jda.api.requests.GatewayIntent; | ||
|
||
public class Plugin extends JavaPlugin { | ||
private FileConfiguration config = getConfig(); | ||
private JDA jda; | ||
private DiscordListener discordListener; | ||
private MinecraftListener minecraftListener; | ||
|
||
public class Plugin extends JavaPlugin implements Listener { | ||
@Override | ||
public void onEnable() { | ||
Bukkit.getPluginManager().registerEvents(this, this); | ||
saveDefaultConfig(); | ||
discordListener = new DiscordListener(this, config.getString("channel-id")); | ||
jda = JDABuilder.createDefault(config.getString("bot-token")) | ||
.enableIntents(GatewayIntent.MESSAGE_CONTENT) | ||
.addEventListeners(discordListener) | ||
.build(); | ||
jda.getRestPing().queue(ping -> getLogger().info("Connected to Discord with ping: " + ping)); | ||
minecraftListener = new MinecraftListener(jda, WebhookClient.withUrl(config.getString("webhook-url"))); | ||
getServer().getPluginManager().registerEvents(minecraftListener, this); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
jda.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
bot-token: "" | ||
channel-id: "" | ||
webhook-url: "" |