Skip to content

Commit

Permalink
1.2.5 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueTree242 committed Nov 20, 2021
2 parents 7e4e7fe + 76b7769 commit e7a999f
Show file tree
Hide file tree
Showing 36 changed files with 475 additions and 269 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# DiscordSRVUtils

Here is the source code! feel free to make pull requests!
DiscordSRVUtils is a plugin that adds more features to the DiscordSRV bot

Download: https://www.spigotmc.org/resources/discordsrvutils.85958/
[Spigot Page](https://www.spigotmc.org/resources/discordsrvutils.85958/)

# API

Expand Down Expand Up @@ -40,7 +40,11 @@ For gradle

You also need to add DiscordSRV's dependecy and repository, which you can find here:
https://github.com/DiscordSRV/DiscordSRV/wiki#developers


# Contributing

To contribute you can fork this repo and make changes on the `develop` branch. And you can Pull Request to the `develop` branch. Pull requests on `master` will be denied and closed.



Expand Down
20 changes: 17 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ shadowJar {
relocate 'com.squareup.okhttp3', "tk.bluetree242.discordsrvutils.dependencies.okhttp"
relocate 'org.flywaydb', "tk.bluetree242.discordsrvutils.dependencies.flywaydb"
relocate 'org.bstats', "tk.bluetree242.discordsrvutils.dependencies.bstats"
relocate 'com.github.ben-manes.caffeine', 'tk.bluetree242.discordsrvutils.dependencies.caffeine'
relocate 'com.github.benmanes.caffeine', 'tk.bluetree242.discordsrvutils.dependencies.caffeine'
}
build.finalizedBy shadowJar
repositories {
Expand Down Expand Up @@ -92,7 +92,18 @@ repositories {
maven { url "https://mvn-repo.arim.space/gpl3" }
maven { url "https://mvn-repo.arim.space/lesser-gpl3" }
}

def urlFile = { url, name ->
File file = new File("$buildDir/download/${name}.jar")
file.parentFile.mkdirs()
if (!file.exists()) {
new URL(url).withInputStream { downloadStream ->
file.withOutputStream { fileOut ->
fileOut << downloadStream
}
}
}
files(file.absolutePath)
}
dependencies {
implementation 'com.vdurmont:emoji-java:5.1.1'
implementation 'org.yaml:snakeyaml:1.27'
Expand All @@ -111,10 +122,11 @@ dependencies {
implementation group: 'org.json', name: 'json', version: '20210307'
implementation 'com.github.ben-manes.caffeine:caffeine:3.0.4'
compileOnly 'space.arim.libertybans:bans-api:0.8.0'
compileOnly urlFile("https://github.com/Zrips/CMI-API/releases/download/8.7.8.2/CMIAPI8.7.8.2.jar", "CMI-API")
}

group = 'me.bluetree.discordsrvutils'
version = '1.2.4'
version = '1.2.5'

description = 'DiscordSRVUtils'
compileJava.options.encoding 'UTF-8'
Expand All @@ -133,3 +145,5 @@ processResources {
filter ReplaceTokens, tokens: [version: version]
}
}


51 changes: 29 additions & 22 deletions src/main/java/tk/bluetree242/discordsrvutils/DiscordSRVUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import tk.bluetree242.discordsrvutils.commandmanagement.CommandManager;
import tk.bluetree242.discordsrvutils.commands.bukkit.DiscordSRVUtilsCommand;
import tk.bluetree242.discordsrvutils.commands.bukkit.tabcompleters.DiscordSRVUtilsTabCompleter;
import tk.bluetree242.discordsrvutils.commands.discord.*;
import tk.bluetree242.discordsrvutils.commands.discord.HelpCommand;
import tk.bluetree242.discordsrvutils.commands.discord.admin.TestMessageCommand;
import tk.bluetree242.discordsrvutils.commands.discord.leveling.LeaderboardCommand;
import tk.bluetree242.discordsrvutils.commands.discord.leveling.LevelCommand;
Expand All @@ -72,6 +72,7 @@
import tk.bluetree242.discordsrvutils.leveling.LevelingManager;
import tk.bluetree242.discordsrvutils.leveling.listeners.bukkit.BukkitLevelingListener;
import tk.bluetree242.discordsrvutils.leveling.listeners.jda.DiscordLevelingListener;
import tk.bluetree242.discordsrvutils.listeners.afk.CMIAfkListener;
import tk.bluetree242.discordsrvutils.listeners.afk.EssentialsAFKListener;
import tk.bluetree242.discordsrvutils.listeners.bukkit.JoinUpdateChecker;
import tk.bluetree242.discordsrvutils.listeners.discordsrv.DiscordSRVListener;
Expand Down Expand Up @@ -151,6 +152,7 @@ public class DiscordSRVUtils extends JavaPlugin {
private ConfManager<SuggestionsConfig> suggestionsConfigManager = ConfManager.create(getDataFolder().toPath(), "suggestions.yml", SuggestionsConfig.class);
private SuggestionsConfig suggestionsConfig;


//Thread Pool
private ThreadPoolExecutor pool;
//Our DiscordSRV Listener
Expand All @@ -159,6 +161,7 @@ public class DiscordSRVUtils extends JavaPlugin {
private HikariDataSource sql;
//listeners that should be registered
private List<ListenerAdapter> listeners = new ArrayList<>();
private long lastErrorTime = 0;

public static DiscordSRVUtils get() {
return instance;
Expand Down Expand Up @@ -210,6 +213,11 @@ public void onLoad() {
init();
//require intents and cacheflags
if (getServer().getPluginManager().getPlugin("DiscordSRV") != null) {
if (DiscordSRV.isReady) {
//Oh no, they are using a plugin manager to reload the plugin, give them a warn
logger.warning("It seems like you are using a Plugin Manager to reload the plugin. This is not a good practice. If you see problems. Please restart");
return;
}
DiscordSRV.api.requireIntent(GatewayIntent.GUILD_MESSAGE_REACTIONS);
DiscordSRV.api.requireCacheFlag(CacheFlag.EMOTE);
}
Expand Down Expand Up @@ -301,7 +309,7 @@ public void onEnable() {
return;
}
//initialize pool
pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(config.pool_size(), new ThreadFactory() {
pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(config.pool_size(), new ThreadFactory() {
@Override
public Thread newThread(@NotNull Runnable r) {

Expand Down Expand Up @@ -355,7 +363,7 @@ public void registerBukkitCommands() {
getCommand("discordsrvutils").setTabCompleter(new DiscordSRVUtilsTabCompleter());
}

private void startupError(Throwable ex,@NotNull String msg) {
private void startupError(Throwable ex, @NotNull String msg) {
setEnabled(false);
logger.warning(msg);
try {
Expand All @@ -365,7 +373,7 @@ private void startupError(Throwable ex,@NotNull String msg) {
e.printStackTrace();
}
//tell them where to report
logger.severe( "Send this to support at https://discordsrvutils.xyz/support");
logger.severe("Send this to support at https://discordsrvutils.xyz/support");
ex.printStackTrace();
}

Expand Down Expand Up @@ -442,13 +450,13 @@ private void initDefaultMessages() {
}
}


public void onDisable() {
if (dsrvlistener != null) DiscordSRV.api.unsubscribe(dsrvlistener);
if (isReady()) {
getJDA().removeEventListener(listeners.toArray(new Object[0]));
}
pool.shutdown();
if (pool != null)
pool.shutdown();
if (WaiterManager.get() != null) WaiterManager.get().timer.cancel();
if (sql != null) sql.close();
}
Expand All @@ -474,7 +482,7 @@ private void whenStarted() {
if (!levelingRoles.exists()) {
levelingRoles.createNewFile();
FileWriter writer = new FileWriter(levelingRoles);
writer.write("{}");
writer.write("{\n\n}");
writer.close();
levelingRolesRaw = new JSONObject();
} else {
Expand All @@ -486,14 +494,14 @@ private void whenStarted() {
logger.severe("Error creating leveling-roles.json");
}

//Register Expansion
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new PAPIExpansion().register();
}


}


public void registerListeners() {
getJDA().addEventListener(listeners.toArray(new Object[0]));
Bukkit.getServer().getPluginManager().registerEvents(new BukkitLevelingListener(), this);
Expand All @@ -517,9 +525,7 @@ public void registerCommands() {
CommandManager.get().registerCommand(new DenySuggestionCommand());
}


/**
*
* @return `DiscordSRV.isReady`. This may change any time soon
*/
public boolean isReady() {
Expand All @@ -544,7 +550,7 @@ public void reloadConfigs() throws IOException, InvalidConfigException {
if (!levelingRoles.exists()) {
levelingRoles.createNewFile();
FileWriter writer = new FileWriter(levelingRoles);
writer.write("{/n/n}");
writer.write("{\n\n}");
writer.close();
levelingRolesRaw = new JSONObject();
} else {
Expand Down Expand Up @@ -591,6 +597,10 @@ public void whenReady() {
getServer().getPluginManager().registerEvents(new EssentialsAFKListener(), this);
hookedPlugins.add(getServer().getPluginManager().getPlugin("Essentials"));
}
if (getServer().getPluginManager().isPluginEnabled("CMI")) {
getServer().getPluginManager().registerEvents(new CMIAfkListener(), this);
hookedPlugins.add(getServer().getPluginManager().getPlugin("CMI"));
}
if (getServer().getPluginManager().isPluginEnabled("AdvancedBan")) {
getServer().getPluginManager().registerEvents(new AdvancedBanPunishmentListener(), this);
hookedPlugins.add(getServer().getPluginManager().getPlugin("AdvancedBan"));
Expand Down Expand Up @@ -708,7 +718,6 @@ private void doSuggestions() {
}
}


public void setSettings() {
if (!isReady()) return;
OnlineStatus onlineStatus = getMainConfig().onlinestatus().equalsIgnoreCase("DND") ? OnlineStatus.DO_NOT_DISTURB : OnlineStatus.valueOf(getMainConfig().onlinestatus().toUpperCase());
Expand Down Expand Up @@ -805,7 +814,6 @@ public <U> void handleCF(CompletableFuture<U> cf, Consumer<U> success, Consumer<
});
}


/**
* For doing a cf inside another one
*/
Expand All @@ -826,7 +834,6 @@ public void defaultHandle(Throwable ex, MessageChannel channel) {
ex.printStackTrace();
}

private long lastErrorTime = 0;
public void defaultHandle(Throwable ex) {
//handle error on thread pool
if (!config.minimize_errors()) {
Expand All @@ -836,15 +843,15 @@ public void defaultHandle(Throwable ex) {
logger.warning("Read the note above the error Please.");
//don't spam errors
if ((System.currentTimeMillis() - lastErrorTime) >= 180000)
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.hasPermission("discordsrvutils.errornotifications")) {
//tell admins that something was wrong
TextComponent msg = new TextComponent(Utils.colors("&7[&eDSU&7] Plugin had an error. Check console for details."));
msg.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://discordsrvutils.xyz/support"));
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(net.md_5.bungee.api.ChatColor.GREEN + "" + net.md_5.bungee.api.ChatColor.BOLD + "Join Support Discord").create()));
p.spigot().sendMessage(msg);
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.hasPermission("discordsrvutils.errornotifications")) {
//tell admins that something was wrong
TextComponent msg = new TextComponent(Utils.colors("&7[&eDSU&7] Plugin had an error. Check console for details."));
msg.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://discordsrvutils.xyz/support"));
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(net.md_5.bungee.api.ChatColor.GREEN + "" + net.md_5.bungee.api.ChatColor.BOLD + "Join Support Discord").create()));
p.spigot().sendMessage(msg);
}
}
}
lastErrorTime = System.currentTimeMillis();

} else {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/tk/bluetree242/discordsrvutils/PAPIExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.bukkit.entity.Player;
import tk.bluetree242.discordsrvutils.leveling.LevelingManager;

import java.util.List;

public class PAPIExpansion extends PlaceholderExpansion {
private DiscordSRVUtils core = DiscordSRVUtils.get();

Expand All @@ -49,6 +51,16 @@ public String getVersion() {
return core.getDescription().getVersion();
}

@Override
public List<String> getPlaceholders() {
return List.of("level", "rank", "xp");
}

@Override
public String getRequiredPlugin() {
return "DiscordSRVUtils";
}

@Override
public String onPlaceholderRequest(Player p, String identifier) {
identifier = identifier.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public abstract class BukkitCommand implements CommandExecutor {

@Override
public final boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(DiscordSRVUtils.get(), () -> {
try {
onRunAsync(sender, command, label, args);
} catch (Throwable ex) {
ex.printStackTrace();
sender.sendMessage(Utils.colors("&cAn internal error occurred while executing this command"));
}
});
Bukkit.getScheduler().runTaskAsynchronously(DiscordSRVUtils.get(), () -> {
try {
onRunAsync(sender, command, label, args);
} catch (Throwable ex) {
ex.printStackTrace();
sender.sendMessage(Utils.colors("&cAn internal error occurred while executing this command"));
}
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@ public int compare(String o1, String o2) {
return null;
}

public List<Role> getRolesToRemove() {
public List<Role> getRolesToRemove(Integer level) {
List<Role> roles = new ArrayList<>();
Map<String, Object> map = core.levelingRolesRaw.toMap();
List<Object> values = new ArrayList<>(map.values());
for (Object value : values) {
Long id = (Long) value;
roles.add(core.getGuild().getRoleById(id));
}
if (level != null)
roles.remove(getRoleForLevel(level));
return roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.jda.api.entities.Member;
import github.scarsz.discordsrv.dependencies.jda.api.entities.Role;
import github.scarsz.discordsrv.dependencies.jda.api.requests.RestAction;
import tk.bluetree242.discordsrvutils.DiscordSRVUtils;
import tk.bluetree242.discordsrvutils.exceptions.UnCheckedSQLException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -103,14 +106,16 @@ public CompletableFuture<Boolean> setXP(int xp) {
LevelingManager manager = LevelingManager.get();
Member member = core.getGuild().retrieveMemberById(id).complete();
if (member == null) return true;
for (Role role : manager.getRolesToRemove()) {
Collection actions = new ArrayList<>();
for (Role role : manager.getRolesToRemove(level)) {
if (member.getRoles().contains(role))
core.getGuild().removeRoleFromMember(member, role).queue();
actions.add(core.getGuild().removeRoleFromMember(member, role).reason("User Leveled Up"));
}
Role toAdd = manager.getRoleForLevel(level);
if (toAdd != null) {
core.getGuild().addRoleToMember(member, toAdd).queue();
actions.add(core.getGuild().addRoleToMember(member, toAdd).reason("User Leveled Up"));
}
RestAction.allOf(actions).queue();
return true;
}
PreparedStatement p1 = conn.prepareStatement("UPDATE leveling SET XP=? WHERE UUID=?");
Expand Down
Loading

0 comments on commit e7a999f

Please sign in to comment.