Skip to content

Commit

Permalink
Jda update
Browse files Browse the repository at this point in the history
  • Loading branch information
YFrite committed Dec 30, 2022
1 parent d1df533 commit 45cf79c
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 122 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
/.idea
/.gradlegit
build
.idea
.gradlegit
.gradle
Binary file not shown.
Empty file.
Binary file removed .gradle/7.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file removed .gradle/7.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file removed .gradle/7.1/fileChanges/last-build.bin
Binary file not shown.
Binary file removed .gradle/7.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file removed .gradle/7.1/fileHashes/fileHashes.lock
Binary file not shown.
Empty file removed .gradle/7.1/gc.properties
Empty file.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 2 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed Jan 26 12:17:35 MSK 2022
gradle.version=7.1
#Fri Dec 30 13:14:29 YEKT 2022
gradle.version=7.2
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Very simple bridge between Minecraft and Discord chats!

TODO: Refactor

Documentation coming soon...
32 changes: 13 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,34 @@ plugins {
}

group 'com.yfrite'
version '1.0-SNAPSHOT'
version '1.1'

repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots'}
maven{ url "https://repo.papermc.io/repository/maven-public/" }

}



dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

compileOnly "org.spigotmc:spigot-api:1.18.1-R0.1-SNAPSHOT"
compileOnly "io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT"

implementation("net.dv8tion:JDA:5.0.0-alpha.4")
implementation "net.dv8tion:JDA:5.0.0-beta.2"
}
test {
useJUnitPlatform()
}

//compileJava.options.encoding = 'UTF-8'

javadoc.options.encoding = 'UTF-8'

// 2nd approach
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

shadowJar {

dependencies {
include dependency("net.dv8tion:JDA:5.0.0-alpha.4")
}
}
//shadowJar {
//
// dependencies {
// include dependency("net.dv8tion:JDA:5.0.0-beta.2")
// include dependency("org.apache.commons:commons-collections4:4.4")
// }
//}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 1 addition & 2 deletions src/main/java/com/yfrite/discordchat/Constants.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.yfrite.discordchat;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel;

import java.util.List;

public class Constants {

public static JDA jda = null;
public static List<TextChannel> channels = null;
public static List<String> channels = null;
public static String DISCORD_API_KEY = "";
}
56 changes: 21 additions & 35 deletions src/main/java/com/yfrite/discordchat/Main.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package com.yfrite.discordchat;

import com.yfrite.discordchat.discord.ChannelListener;
import com.yfrite.discordchat.server.*;

import com.yfrite.discordchat.server.ChatListener;
import com.yfrite.discordchat.server.Utils;
import com.yfrite.discordchat.server.command.*;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.bukkit.plugin.java.JavaPlugin;

import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.List;

public class Main extends JavaPlugin {

private static Main plugin;
Expand All @@ -36,44 +29,30 @@ public void onEnable() {
try{
Main plugin = Main.getPlugin();
String apiKey = plugin.getConfig().getString("discordApiKey");
JDA jda;

Constants.DISCORD_API_KEY = apiKey;

try {
Constants.jda = JDABuilder.createDefault(apiKey)
.setActivity(Activity.playing("ZeroWorld"))
.build();

jda = Constants.jda;

jda.awaitReady();
List<String> channelIds = plugin.getConfig().getStringList("channels");
List<TextChannel> channels = new ArrayList<>();

plugin.getConfig().set("channels", channelIds);
var jda = JDABuilder.createDefault(apiKey)
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.build()
.awaitReady();

for (String id : channelIds){
channels.add(jda.getTextChannelById(Long.parseLong(id)));
//server.getLogger().info(id);
Constants.jda = jda;

}

Constants.channels = channels;
Constants.channels = plugin.getConfig().getStringList("channels");

plugin.saveConfig();

plugin.getLogger().info("Channels updated!");

jda.addEventListener(new ChannelListener());

CommandListUpdateAction commands = jda.updateCommands();
commands.addCommands(
new CommandData("add", "Add channel for bot.")
);
jda.updateCommands().addCommands(
Commands.slash("add", "Add channel for bot.")
).queue();

commands.queue();
} catch (LoginException | InterruptedException e) {
} catch (InterruptedException e) {
plugin.getLogger().throwing("CommandSetup", "onCommand", e);
}
} catch (Exception exception){
Expand All @@ -91,4 +70,11 @@ public void onEnable() {
this.getCommand("setup").setExecutor(new CommandSetup());

}

@Override
public void onDisable() {

Constants.jda.cancelRequests();
Constants.jda.shutdownNow();
}
}
46 changes: 14 additions & 32 deletions src/main/java/com/yfrite/discordchat/discord/ChannelListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import com.yfrite.discordchat.Constants;
import com.yfrite.discordchat.Main;
import com.yfrite.discordchat.server.Sender;

import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import org.bukkit.ChatColor;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ChannelListener extends ListenerAdapter {
Expand All @@ -25,48 +21,34 @@ public class ChannelListener extends ListenerAdapter {
}

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if (event.getGuild() == null) return;
if (!event.getName().equals("add")) return;

if (event.getChannelType().isMessage()) {
if (true) {
List<String> channelIds = plugin.getConfig().getStringList("channels");
List<TextChannel> channels = new ArrayList<>();

channelIds.add(event.getTextChannel().getId());

plugin.getConfig().set("channels", channelIds);

for (String id : channelIds){
channels.add(Constants.jda.getTextChannelById(Long.parseLong(id)));
//server.getLogger().info(id);
if (event.getChannelType() == ChannelType.TEXT) {
Constants.channels.add(event.getChannel().getId());

}
plugin.getConfig().set("channels", Constants.channels);
plugin.saveConfig();

Constants.channels = channels;

plugin.saveConfig();

plugin.getLogger().info("Channels updated!");
event.reply("Чат добавлен.").queue();

}
}
plugin.getLogger().info("Channels updated!");
event.reply("Чат добавлен.").queue();
}

}

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (Constants.channels.contains(event.getTextChannel())) {
var channel = event.getChannel();

if (Constants.channels.contains(channel.getId())) {
if(event.getAuthor().isBot()) return;
String message = event.getMessage().getContentDisplay();
if (message.equals("")) return;

String userName = event.getAuthor().getName();

//server.getLogger().info(event.getAuthor().getAsTag());
String content = ChatColor.BLUE + "<" + userName + "> " + ChatColor.WHITE + message;
String content = ChatColor.BLUE + userName + ": " + ChatColor.WHITE + message;

Sender.sendMessage(false, content);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/yfrite/discordchat/discord/Sender.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.yfrite.discordchat.discord;

import com.yfrite.discordchat.Constants;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;

public class Sender {

public static void send(String message, String fromUser){
for(TextChannel channel: Constants.channels)
channel.sendMessage("<" + fromUser + "> " + message).queue();
for(String channelId: Constants.channels)
Constants.jda.getTextChannelById(channelId).sendMessage("<" + fromUser + "> " + message).queue();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/yfrite/discordchat/server/Sender.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.yfrite.discordchat.server;

import com.yfrite.discordchat.Main;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand All @@ -14,7 +14,7 @@ public static void sendMessage(boolean isGlobal, String text) {
Main plugin = Main.getPlugin();

if (isGlobal)
plugin.getServer().broadcastMessage(text);
plugin.getServer().broadcast(Component.text(text));
else {
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.List;

public class CommandSetup implements CommandExecutor {
@Override
Expand All @@ -37,32 +32,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
jda = Constants.jda;

jda.awaitReady();
List<String> channelIds = plugin.getConfig().getStringList("channels");
List<TextChannel> channels = new ArrayList<>();

plugin.getConfig().set("channels", channelIds);

for (String id : channelIds){
channels.add(jda.getTextChannelById(Long.parseLong(id)));
//server.getLogger().info(id);

}

Constants.channels = channels;
Constants.channels = plugin.getConfig().getStringList("channels");

plugin.saveConfig();

plugin.getLogger().info("Channels updated!");

jda.addEventListener(new ChannelListener());

CommandListUpdateAction commands = jda.updateCommands();
commands.addCommands(
new CommandData("add", "Add channel for bot.")
);
jda.updateCommands().addCommands(
Commands.slash("add", "Add channel for bot.")
).queue();

commands.queue();
} catch (LoginException | InterruptedException e) {
} catch (InterruptedException e) {
plugin.getLogger().throwing("CommandSetup", "onCommand", e);
}

Expand Down

0 comments on commit 45cf79c

Please sign in to comment.