mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Add basic discord bridging and sanity checks
This commit is contained in:
1 parent
5a5c721209
commit
8a9606261b
6 files changed
+99
-53
No files matched your search
@@ -216,19 +216,6 @@
|
|||||||
<version>2.1.115-SNAPSHOT</version>
|
<version>2.1.115-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Kotlin, because I'm too lazy to port a method to java -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
|
||||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
|
||||||
<version>${kotlin.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
|
||||||
<artifactId>kotlin-test</artifactId>
|
|
||||||
<version>${kotlin.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Vault for permissions / groups -->
|
<!-- Vault for permissions / groups -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.MilkBowl</groupId>
|
<groupId>com.github.MilkBowl</groupId>
|
||||||
@@ -236,5 +223,18 @@
|
|||||||
<version>1.7</version>
|
<version>1.7</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Discord bot and webhooks -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.javacord</groupId>
|
||||||
|
<artifactId>javacord</artifactId>
|
||||||
|
<version>3.0.5</version>
|
||||||
|
<type>pom</type>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>club.minnced</groupId>
|
||||||
|
<artifactId>discord-webhooks</artifactId>
|
||||||
|
<version>0.1.8</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -9,9 +9,12 @@ import net.draycia.simplechat.commands.ToggleCommand;
|
|||||||
import net.draycia.simplechat.listeners.PlayerChatListener;
|
import net.draycia.simplechat.listeners.PlayerChatListener;
|
||||||
import net.kyori.text.format.TextColor;
|
import net.kyori.text.format.TextColor;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.javacord.api.DiscordApi;
|
||||||
|
import org.javacord.api.DiscordApiBuilder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -26,12 +29,18 @@ public final class SimpleChat extends JavaPlugin {
|
|||||||
|
|
||||||
private Permission permission;
|
private Permission permission;
|
||||||
|
|
||||||
|
private DiscordApi discordAPI = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
||||||
|
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
|
|
||||||
|
if (getConfig().contains("bot-token")) {
|
||||||
|
discordAPI = new DiscordApiBuilder().setToken(getConfig().getString("bot-token")).login().join();
|
||||||
|
}
|
||||||
|
|
||||||
BukkitCommandManager manager = new BukkitCommandManager(this);
|
BukkitCommandManager manager = new BukkitCommandManager(this);
|
||||||
|
|
||||||
manager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
manager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
||||||
@@ -139,6 +148,20 @@ public final class SimpleChat extends JavaPlugin {
|
|||||||
|
|
||||||
ChatChannel channel = builder.build(this);
|
ChatChannel channel = builder.build(this);
|
||||||
|
|
||||||
|
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
|
||||||
|
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||||
|
getLogger().warning("Towny related channel with name [" + channel.getName() + "] found, but Towny isn't installed. Skipping channel.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel.isPartyChat()) {
|
||||||
|
if (!Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
|
||||||
|
getLogger().warning("mcMMO related channel with name [" + channel.getName() + "] found, but mcMMO isn't installed. Skipping channel.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: register command for each channel
|
// TODO: register command for each channel
|
||||||
channels.add(channel);
|
channels.add(channel);
|
||||||
}
|
}
|
||||||
@@ -220,4 +243,8 @@ public final class SimpleChat extends JavaPlugin {
|
|||||||
public Permission getPermission() {
|
public Permission getPermission() {
|
||||||
return permission;
|
return permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DiscordApi getDiscordAPI() {
|
||||||
|
return discordAPI;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package net.draycia.simplechat
|
|
||||||
|
|
||||||
import net.kyori.text.Component
|
|
||||||
import net.kyori.text.TextComponent
|
|
||||||
import net.kyori.text.event.ClickEvent
|
|
||||||
import net.kyori.text.event.HoverEvent
|
|
||||||
import net.kyori.text.event.HoverEvent.Action.SHOW_TEXT
|
|
||||||
|
|
||||||
fun Component.removeEscape(escape: Char = '\\'): Component {
|
|
||||||
if (this is TextComponent) {
|
|
||||||
return content(content().filterNot { it == escape })
|
|
||||||
.children(children().map { it.removeEscape(escape) })
|
|
||||||
.clickEvent(clickEvent()?.run {
|
|
||||||
ClickEvent.of(action(), value().filterNot { it == escape })
|
|
||||||
})
|
|
||||||
.hoverEvent(hoverEvent()?.run {
|
|
||||||
if (action() == SHOW_TEXT) {
|
|
||||||
HoverEvent.of(action(), value().removeEscape(escape))
|
|
||||||
} else {
|
|
||||||
this
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@ package net.draycia.simplechat.channels;
|
|||||||
|
|
||||||
import net.kyori.text.format.TextColor;
|
import net.kyori.text.format.TextColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.javacord.api.event.message.MessageCreateEvent;
|
||||||
|
|
||||||
public abstract class ChatChannel {
|
public abstract class ChatChannel {
|
||||||
|
|
||||||
@@ -94,4 +95,8 @@ public abstract class ChatChannel {
|
|||||||
*/
|
*/
|
||||||
public abstract void sendMessage(Player player, String message);
|
public abstract void sendMessage(Player player, String message);
|
||||||
|
|
||||||
|
public abstract void processDiscordMessage(MessageCreateEvent event);
|
||||||
|
|
||||||
|
//public abstract void sendMessageToMinecraft(String message);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ import com.palmergames.bukkit.towny.object.Town;
|
|||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||||
import net.draycia.simplechat.SimpleChat;
|
import net.draycia.simplechat.SimpleChat;
|
||||||
import net.draycia.simplechat.TextUtilsKt;
|
|
||||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||||
import net.kyori.text.Component;
|
import net.kyori.text.Component;
|
||||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||||
@@ -19,8 +18,13 @@ import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.javacord.api.entity.channel.ServerTextChannel;
|
||||||
|
import org.javacord.api.entity.message.MessageAuthor;
|
||||||
|
import org.javacord.api.entity.permission.Role;
|
||||||
|
import org.javacord.api.event.message.MessageCreateEvent;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class SimpleChatChannel extends ChatChannel {
|
public class SimpleChatChannel extends ChatChannel {
|
||||||
@@ -66,16 +70,48 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
this.isPartyChat = isPartyChat;
|
this.isPartyChat = isPartyChat;
|
||||||
|
|
||||||
this.simpleChat = simpleChat;
|
this.simpleChat = simpleChat;
|
||||||
|
|
||||||
|
if (getChannelId() > 0 && simpleChat.getDiscordAPI() != null) {
|
||||||
|
simpleChat.getDiscordAPI().addMessageCreateListener(this::processDiscordMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: webhooks
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getDiscordFormatting() {
|
||||||
|
return formats.getOrDefault("discord-to-mc", "<gray>[<blue>Discord<gray>] <username>: <white><message>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canPlayerUse(Player player) {
|
public boolean canPlayerUse(Player player) {
|
||||||
if (player.hasPermission("simplechat.channels." + getName())) {
|
return player.hasPermission("simplechat.channels." + getName());
|
||||||
return true;
|
// TODO: other use conditions
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processDiscordMessage(MessageCreateEvent event) {
|
||||||
|
if (event.getChannel().getId() != getChannelId() || event.getMessageAuthor().isBotUser()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: other use conditions
|
String message = MiniMessageParser.escapeTokens(event.getMessageContent()).replace("~", "\\~")
|
||||||
return false;
|
.replace("_", "\\_").replace("*", "\\*").replace("\n", "");
|
||||||
|
|
||||||
|
MessageAuthor author = event.getMessageAuthor();
|
||||||
|
ServerTextChannel channel = (ServerTextChannel)event.getChannel();
|
||||||
|
|
||||||
|
List<Role> roles = author.asUser().get().getRoles(event.getServer().get());
|
||||||
|
|
||||||
|
String role = roles.get(roles.size() - 1).getName();
|
||||||
|
|
||||||
|
// Placeholders: username, displayname, channel, server, message, primaryrole
|
||||||
|
Component component = MiniMessageParser.parseFormat(getDiscordFormatting(), "message", message,
|
||||||
|
"username", author.getName(), "displayname", author.getDisplayName(), "channel", channel.getName(),
|
||||||
|
"server", event.getServer().get().getName(), "primaryrole", role);
|
||||||
|
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
TextAdapter.sendMessage(player, component);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -96,7 +132,7 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "color", "<" + color.toString() + ">");
|
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "color", "<" + color.toString() + ">");
|
||||||
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "message", event.getMessage());
|
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "message", event.getMessage());
|
||||||
|
|
||||||
Component formattedMessage = TextUtilsKt.removeEscape(MiniMessageParser.parseFormat(messageFormat), '\\');
|
Component formattedMessage = /*TextUtilsKt.removeEscape(*/MiniMessageParser.parseFormat(messageFormat)/*, '\\')*/;
|
||||||
|
|
||||||
if (isTownChat()) {
|
if (isTownChat()) {
|
||||||
try {
|
try {
|
||||||
@@ -190,17 +226,14 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||||
if (onlinePlayer.hasPermission("simplechat.see." + getName().toLowerCase())) {
|
if (!canUserSeeMessage(player, onlinePlayer)) {
|
||||||
if (!canUserSeeMessage(player, onlinePlayer)) {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,6 +250,10 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean canUserSeeMessage(Player sender, Player target) {
|
public boolean canUserSeeMessage(Player sender, Player target) {
|
||||||
|
if (!target.hasPermission("simplechat.see." + getName().toLowerCase())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (getDistance() > 0 && target.getLocation().distance(sender.getLocation()) > getDistance()) {
|
if (getDistance() > 0 && target.getLocation().distance(sender.getLocation()) > getDistance()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ channels:
|
|||||||
toggle-on-message: '<gray>You can now see <color><channel> <gray>chat!'
|
toggle-on-message: '<gray>You can now see <color><channel> <gray>chat!'
|
||||||
toggle-off-message: '<gray>You can no longer see <color><channel> <gray>chat!'
|
toggle-off-message: '<gray>You can no longer see <color><channel> <gray>chat!'
|
||||||
formats:
|
formats:
|
||||||
|
discord-to-mc: '<gray>[<blue>Discord<gray>] <username>: <white><message>' # Placeholders: username, displayname, channel, server, message, primaryrole
|
||||||
staff: '<gray>{[%deluxetags_tag%<gray>]} %player_displayname% <gray>» <white><message>'
|
staff: '<gray>{[%deluxetags_tag%<gray>]} %player_displayname% <gray>» <white><message>'
|
||||||
default: '<gray>[%deluxetags_tag%<gray>] %player_displayname%<gray>: <white><message>'
|
default: '<gray>[%deluxetags_tag%<gray>] %player_displayname%<gray>: <white><message>'
|
||||||
webhook: 'URL_HERE' # Comment out / remove to disable webhook
|
webhook: 'URL_HERE' # Comment out / remove to disable webhook
|
||||||
color: WHITE
|
color: WHITE
|
||||||
default: true
|
default: true
|
||||||
ignorable: true
|
ignorable: true
|
||||||
distance: -1 # Set to -1, comment out, or remove to disable distance checking
|
distance: -1 # Set to -1, comment out, or remove to disable distance checking
|
||||||
|
|
||||||
|
bot-token: -1
|
||||||
Reference in new issue
Block a user