Add discord bot support
This commit is contained in:
parent
9447ca5887
commit
80cf17d14a
9 changed files with 201 additions and 1 deletions
16
pom.xml
16
pom.xml
|
@ -24,9 +24,25 @@
|
|||
<id>papermc</id>
|
||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<version>5.0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>club.minnced</groupId>
|
||||
<artifactId>discord-webhooks</artifactId>
|
||||
<version>0.1.6</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
|
|
|
@ -4,18 +4,28 @@ import java.sql.SQLException;
|
|||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import rip.iwakura.civilcore.commands.TeamCommand;
|
||||
import rip.iwakura.civilcore.discord.Discord;
|
||||
|
||||
public class CivilCore extends JavaPlugin {
|
||||
public Database db = new Database();
|
||||
public Discord bot;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.saveDefaultConfig();
|
||||
|
||||
this.bot = new Discord(getConfig().getString("discord.token"), getConfig().getString("discord.channel"), this);
|
||||
this.bot.initialize();
|
||||
|
||||
if (Bukkit.getWorld("court") == null) {
|
||||
new WorldCreator("court").createWorld();
|
||||
}
|
||||
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
|
@ -28,6 +38,8 @@ public class CivilCore extends JavaPlugin {
|
|||
e.printStackTrace();
|
||||
}
|
||||
getServer().getPluginManager().registerEvents(new PlayerHandler(this), this);
|
||||
getServer().getPluginManager().registerEvents(new Court(), this);
|
||||
this.getCommand("team").setExecutor(new TeamCommand(this));
|
||||
this.getCommand("court").setExecutor(new Court());
|
||||
}
|
||||
}
|
||||
|
|
64
src/main/java/rip/iwakura/civilcore/Court.java
Normal file
64
src/main/java/rip/iwakura/civilcore/Court.java
Normal file
|
@ -0,0 +1,64 @@
|
|||
package rip.iwakura.civilcore;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockBurnEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.BlockSpreadEvent;
|
||||
|
||||
public class Court implements Listener,CommandExecutor{
|
||||
private World court_world = Bukkit.getWorld("court");
|
||||
|
||||
private Location court_location = new Location(court_world, 4.5, 70.0, 0.5, -90f, 1.0f);
|
||||
|
||||
@EventHandler
|
||||
public void PlayerBreak(BlockBreakEvent e) {
|
||||
if (e.getPlayer().isOp()) return;
|
||||
if (e.getBlock().getWorld().getName().equals(court_world.getName())) e.setCancelled(true);
|
||||
}
|
||||
@EventHandler
|
||||
public void PlayerPlace(BlockPlaceEvent e) {
|
||||
if (e.getPlayer().isOp()) return;
|
||||
if (e.getBlock().getWorld().getName().equals(court_world.getName())) e.setCancelled(true);
|
||||
}
|
||||
@EventHandler
|
||||
public void PlayerBurn(BlockBurnEvent e) {
|
||||
if (e.getBlock().getWorld().getName().equals(court_world.getName())) e.setCancelled(true);
|
||||
}
|
||||
@EventHandler
|
||||
public void FireSpread(BlockSpreadEvent e) {
|
||||
if (e.getBlock().getWorld().getName().equals(court_world.getName())) e.setCancelled(true);
|
||||
}
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("Non-player type.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) sender;
|
||||
|
||||
switch (args[0]) {
|
||||
case "enter":
|
||||
p.teleport(court_location);
|
||||
break;
|
||||
case "leave":
|
||||
Location loc = p.getBedSpawnLocation();
|
||||
if (loc == null) {
|
||||
loc = Bukkit.getWorld("world").getSpawnLocation();
|
||||
}
|
||||
p.teleport(loc);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -31,6 +31,8 @@ public class PlayerHandler implements Listener {
|
|||
String prefix = dbPlayer.team.prefix == null ? "" : ChatColor.translateAlternateColorCodes('&', dbPlayer.team.prefix) + " ";
|
||||
|
||||
Bukkit.broadcastMessage(String.format("%s%s: %s", prefix, player_name, e.getMessage()));
|
||||
|
||||
civilCore.bot.sendMessage(e);
|
||||
}
|
||||
@EventHandler
|
||||
public void PlayerJoinEvent(PlayerJoinEvent e) {
|
||||
|
|
|
@ -22,7 +22,7 @@ public class TeamCommand implements CommandExecutor {
|
|||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
args = Utils.parser(args).toArray(new String[args.length]);
|
||||
args = Utils.parser(args).toArray(new String[0]);
|
||||
|
||||
if(args.length == 0) {
|
||||
sender.sendMessage(ChatColor.RED + "Usage: /team <create | destroy | add | remove | list> [options]");
|
||||
|
|
73
src/main/java/rip/iwakura/civilcore/discord/Discord.java
Normal file
73
src/main/java/rip/iwakura/civilcore/discord/Discord.java
Normal file
|
@ -0,0 +1,73 @@
|
|||
package rip.iwakura.civilcore.discord;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.checkerframework.common.reflection.qual.GetConstructor;
|
||||
|
||||
import club.minnced.discord.webhook.WebhookClient;
|
||||
import club.minnced.discord.webhook.WebhookClientBuilder;
|
||||
import club.minnced.discord.webhook.send.WebhookMessage;
|
||||
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import rip.iwakura.civilcore.CivilCore;
|
||||
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
||||
|
||||
public class Discord extends ListenerAdapter {
|
||||
private String token;
|
||||
private CivilCore civilCore;
|
||||
|
||||
public String channelId;
|
||||
|
||||
private TextChannel channel;
|
||||
private WebhookClient webhook;
|
||||
|
||||
public JDA bot;
|
||||
|
||||
private EnumSet<GatewayIntent> intents = EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_WEBHOOKS);
|
||||
|
||||
public Discord(String token, String channelId, CivilCore civilCore){
|
||||
this.civilCore = civilCore;
|
||||
this.channelId = channelId;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
this.bot = JDABuilder.createLight(token, intents)
|
||||
.addEventListeners(new MessageReceive(civilCore))
|
||||
.addEventListeners(this)
|
||||
.build();
|
||||
}
|
||||
|
||||
public void sendMessage(AsyncPlayerChatEvent e) {
|
||||
String player_name = e.getPlayer().getName();
|
||||
WebhookMessageBuilder builder = new WebhookMessageBuilder()
|
||||
.setUsername(player_name)
|
||||
.setAvatarUrl(String.format("https://mc-heads.net/avatar/%s", player_name))
|
||||
.setContent(e.getMessage());
|
||||
webhook.send(builder.build());
|
||||
//channel.sendMessage(String.format("%s: %s", e.getPlayer().getName(), e.getMessage())).complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady(ReadyEvent e) {
|
||||
this.channel = bot.getTextChannelById(channelId);
|
||||
String webhook_token = civilCore.getConfig().getString("discord.webhook.token");
|
||||
Long webhook_id = Long.parseLong(civilCore.getConfig().getString("discord.webhook.id"));
|
||||
if (webhook_token != null) {
|
||||
this.webhook = new WebhookClientBuilder(webhook_id, webhook_token).build();
|
||||
} else {
|
||||
channel.createWebhook("CivilBot").queue((hook) -> {
|
||||
civilCore.getConfig().set("discord.webhook.id", hook.getId());
|
||||
civilCore.getConfig().set("discord.webhook.token", hook.getToken());
|
||||
civilCore.saveConfig();
|
||||
this.webhook = new WebhookClientBuilder(Long.parseLong(hook.getId()), hook.getToken()).build();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package rip.iwakura.civilcore.discord;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import rip.iwakura.civilcore.CivilCore;
|
||||
|
||||
public class MessageReceive extends ListenerAdapter {
|
||||
private CivilCore civilCore;
|
||||
|
||||
public MessageReceive (CivilCore civilCore) {
|
||||
this.civilCore = civilCore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(MessageReceivedEvent e) {
|
||||
if (e.getAuthor().isBot()) return;
|
||||
if(e.getChannel().getId().equals(civilCore.bot.channelId)) Bukkit.broadcastMessage(String.format("%sDiscord%s %s: %s", ChatColor.BLUE, ChatColor.RESET, e.getAuthor().getName(), e.getMessage()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package rip.iwakura.civilcore.types;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface CoreCommand {
|
||||
public boolean run(CommandSender sender);
|
||||
}
|
|
@ -8,3 +8,7 @@ commands:
|
|||
team:
|
||||
description: Team Manager
|
||||
usage: /<command> <create|destroy|add|remove>
|
||||
permission: "civil.teams"
|
||||
court:
|
||||
description: Court Manager
|
||||
usage: /<command> <enter|leave>
|
||||
|
|
Loading…
Reference in a new issue