From ef038c5b5bb06bd14da21bf2ff2b331f9243b8dc Mon Sep 17 00:00:00 2001 From: anthorob Date: Mon, 21 Mar 2022 19:28:20 -0400 Subject: [PATCH] Add ability to limit music commands to a certain channel --- .../command/AbstractCommand.java | 6 +++ .../command/AbstractMusicCommand.java | 32 +++++++++++----- .../java/netneutrality/command/ICommand.java | 3 ++ .../command/PermissionManager.java | 26 ------------- .../command/bot/ConfigureCommand.java | 37 ++++++++++++++++++- .../java/netneutrality/core/GuildManager.java | 4 +- .../core/context/CommandContext.java | 10 ++--- .../java/netneutrality/utils/JpaUtils.java | 36 ++++++++++++++---- 8 files changed, 102 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/netneutrality/command/PermissionManager.java diff --git a/src/main/java/netneutrality/command/AbstractCommand.java b/src/main/java/netneutrality/command/AbstractCommand.java index 071264a..252a597 100644 --- a/src/main/java/netneutrality/command/AbstractCommand.java +++ b/src/main/java/netneutrality/command/AbstractCommand.java @@ -2,6 +2,7 @@ package netneutrality.command; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; public abstract class AbstractCommand implements ICommand { @@ -9,4 +10,9 @@ public abstract class AbstractCommand implements ICommand { public boolean canBeExecutedBy(Guild guild, Member member) { return true; } + + @Override + public boolean canBeExecutedFrom(Guild guild, TextChannel channel) { + return true; + } } diff --git a/src/main/java/netneutrality/command/AbstractMusicCommand.java b/src/main/java/netneutrality/command/AbstractMusicCommand.java index 4614326..afcf418 100644 --- a/src/main/java/netneutrality/command/AbstractMusicCommand.java +++ b/src/main/java/netneutrality/command/AbstractMusicCommand.java @@ -6,9 +6,7 @@ package netneutrality.command; * https://discord.gg/CdFcyQ */ -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.*; import netneutrality.core.GuildManager; import netneutrality.utils.UserFunction; @@ -27,17 +25,33 @@ public abstract class AbstractMusicCommand extends AbstractCommand { @Override public boolean canBeExecutedBy(Guild guild, Member member) { String roleDb = GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_ROLE); - boolean can = true; - //System.out.println(roleDb); if (roleDb != null && !roleDb.isEmpty()) { + if (UserFunction.isAdmin(member)) { + return true; + } + Role neededRole = guild.getRoleById(roleDb); - can = member.getRoles().contains(neededRole); + if (neededRole == null) { + // Role doesn't exist on guild anymore, remove from db + GuildManager.getManager(guild).setSettingValue(GuildManager.Settings.MUSIC_ROLE, null); + return true; + } + + return member.getRoles().contains(neededRole); } - if (UserFunction.isAdmin(member)) - can = true; + return true; + } - return can; + @Override + public boolean canBeExecutedFrom(Guild guild, TextChannel channel) { + String channelDb = GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_CHANNEL); + + if (channelDb != null && !channelDb.isEmpty()) { + return channelDb.equals(channel.getId()); + } + + return true; } } diff --git a/src/main/java/netneutrality/command/ICommand.java b/src/main/java/netneutrality/command/ICommand.java index 848bc5a..91fed50 100644 --- a/src/main/java/netneutrality/command/ICommand.java +++ b/src/main/java/netneutrality/command/ICommand.java @@ -8,6 +8,7 @@ package netneutrality.command; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; import netneutrality.core.context.CommandContext; import netneutrality.exceptions.CommandErrorException; import netneutrality.exceptions.CommandExecutionException; @@ -44,6 +45,8 @@ public interface ICommand { boolean canBeExecutedBy(Guild guild, Member member); + boolean canBeExecutedFrom(Guild guild, TextChannel channel); + /** * Command is executed with this function. * diff --git a/src/main/java/netneutrality/command/PermissionManager.java b/src/main/java/netneutrality/command/PermissionManager.java deleted file mode 100644 index 43c4983..0000000 --- a/src/main/java/netneutrality/command/PermissionManager.java +++ /dev/null @@ -1,26 +0,0 @@ -package netneutrality.command; -/* - * Copyright (c) anthorob - * - * https://xtremcommunity.com/ - * https://discord.gg/CdFcyQ - */ - -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; - -import java.util.List; - -public class PermissionManager { - - public static boolean CheckPermissionForUser(Guild guild, Member member, AbstractCommand command) { - - //Permission permissions = DBController.get().prepareAndExecuteAsync("SELECT * FROM permissions WHERE guild_id = ?").blocking(); - - List roles = member.getRoles(); - - return false; - } - -} diff --git a/src/main/java/netneutrality/command/bot/ConfigureCommand.java b/src/main/java/netneutrality/command/bot/ConfigureCommand.java index 0a0bb79..8254d3d 100644 --- a/src/main/java/netneutrality/command/bot/ConfigureCommand.java +++ b/src/main/java/netneutrality/command/bot/ConfigureCommand.java @@ -7,12 +7,12 @@ package netneutrality.command.bot; */ import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.TextChannel; import netneutrality.command.AbstractCommand; import netneutrality.command.CommandType; import netneutrality.command.CommandUsage; import netneutrality.core.GuildManager; import netneutrality.core.context.CommandContext; -import netneutrality.exceptions.CommandErrorException; import netneutrality.music.MusicManager; import java.util.List; @@ -41,7 +41,7 @@ public class ConfigureCommand extends AbstractCommand { @Override public CommandUsage getUsage() { return CommandUsage.create(this, - new CommandUsage.Argument(CommandUsage.Argument.Type.REQUIRED, new String[] {"musicrole", "musicvolume", "debug"}, "") + new CommandUsage.Argument(CommandUsage.Argument.Type.REQUIRED, new String[] {"musicrole", "music-channel", "musicvolume", "debug"}, "") ); } @@ -95,6 +95,39 @@ public class ConfigureCommand extends AbstractCommand { } } + // Role for music commands + if (context.getArg(0).equals("music-channel")) { + if (args.length == 1) { + String channelDb = guildManager.getSetting(GuildManager.Settings.MUSIC_CHANNEL); + TextChannel channel = null; + if (!channelDb.isEmpty()) { + channel = context.getGuild().getTextChannelById(channelDb); + } + + context.reply("Current channel where music commands can be executed (default is everywhere): %s", (channel == null ? "Everywhere" : channel.getAsMention())); + context.reply("Reset channel using 'conf music-channel reset' or set it using 'conf music-channel #'"); + } + + if (args.length == 2) { + if (context.getArg(1).equals("reset")) { + if (!guildManager.getSetting(GuildManager.Settings.MUSIC_CHANNEL).isEmpty()) { + guildManager.setSettingValue(GuildManager.Settings.MUSIC_CHANNEL, null); + } + + context.reply("Successful reset of the channel"); + } + + List channels = context.getMessage().getMentionedChannels(); + if (channels.size() == 1) { + TextChannel channel = channels.get(0); + if (channel.getGuild().getId().equals(context.getGuild().getId())) { + guildManager.setSettingValue(GuildManager.Settings.MUSIC_CHANNEL, channel.getId()); + context.reply("Successfully set %s as the channel required to execute music command", channel.getAsMention()); + } + } + } + } + // Default volume for music player if (context.getArg(0).equals("musicvolume")) { if (args.length == 1) { diff --git a/src/main/java/netneutrality/core/GuildManager.java b/src/main/java/netneutrality/core/GuildManager.java index c2fdea3..91b6697 100644 --- a/src/main/java/netneutrality/core/GuildManager.java +++ b/src/main/java/netneutrality/core/GuildManager.java @@ -191,7 +191,7 @@ public class GuildManager { logger.debug("Reloading settings cache for guild: " + guild.getId()); settingsCache.clear(); - JpaUtils.getWithSession(guildEntity, close, () -> { + JpaUtils.getWithSession(guildEntity, close, session -> { Set settingSet = guildEntity.getGuildSettings(); for (OGuildSetting setting : settingSet) { @@ -210,7 +210,7 @@ public class GuildManager { logger.debug("Reloading blacklist cache for guild: " + guild.getId()); blacklistCache.clear(); - JpaUtils.getWithSession(guildEntity, close, () -> { + JpaUtils.getWithSession(guildEntity, close, session -> { Set oBlacklistSet = guildEntity.getBlacklists(); for (OBlacklist bl : oBlacklistSet) { diff --git a/src/main/java/netneutrality/core/context/CommandContext.java b/src/main/java/netneutrality/core/context/CommandContext.java index ce3ebff..0b82b40 100644 --- a/src/main/java/netneutrality/core/context/CommandContext.java +++ b/src/main/java/netneutrality/core/context/CommandContext.java @@ -179,14 +179,14 @@ public class CommandContext { }); try { - if (command.needAdmin()) { - if (!UserFunction.isAdmin(member)) { - return; - } + if (command.needAdmin() && !UserFunction.isAdmin(member)) { + return; } if (command.canBeExecutedBy(guild, member)) { - command.execute(this); + if (command.canBeExecutedFrom(guild, channel)) { + command.execute(this); + } } } catch (CommandErrorException | CommandExecutionException e) { LogHandler.log(e); diff --git a/src/main/java/netneutrality/utils/JpaUtils.java b/src/main/java/netneutrality/utils/JpaUtils.java index 4d3aebc..6ad9d58 100644 --- a/src/main/java/netneutrality/utils/JpaUtils.java +++ b/src/main/java/netneutrality/utils/JpaUtils.java @@ -16,9 +16,22 @@ import org.hibernate.ObjectNotFoundException; import org.hibernate.Session; import java.io.Serializable; +import java.util.function.Consumer; public class JpaUtils { + /** + * Tries to read the persistent state into the object. + * + * If the object is not found in the database and is of type {@link OGuild}, tries to recreate entity and throws + * an exception to block execution. + * + * The session is not closed + * + * @param session The session in which to load the persistent object + * @param object The persistent object + * @param id Identifier + */ public static void tryLoad(Session session, Object object, Serializable id) { try { session.load(object, id); @@ -53,23 +66,30 @@ public class JpaUtils { session.close(); } - public static void getWithSession(OGuild oGuild, Runnable runnable) { - getWithSession(oGuild, oGuild.getGuildId(), runnable); + public static void getWithSession(OGuild oGuild, Consumer consumer) { + getWithSession(oGuild, oGuild.getGuildId(), consumer); } - public static void getWithSession(OGuild oGuild, boolean close, Runnable runnable) { - getWithSession(oGuild, oGuild.getGuildId(), close, runnable); + public static void getWithSession(OGuild oGuild, boolean close, Consumer consumer) { + getWithSession(oGuild, oGuild.getGuildId(), close, consumer); } - public static void getWithSession(Object persistantEntity, Serializable id, Runnable runnable) { - getWithSession(persistantEntity, id, true, runnable); + public static void getWithSession(Object persistantEntity, Serializable id, Consumer consumer) { + getWithSession(persistantEntity, id, true, consumer); } - public static void getWithSession(Object persistantEntity, Serializable id, boolean close, Runnable runnable) { + + /** + * @param persistantEntity The entity to load + * @param id The identifier for the entity + * @param close If the session should be closed after consumer is executed + * @param consumer The comsumer, which exposes the session + */ + public static void getWithSession(Object persistantEntity, Serializable id, boolean close, Consumer consumer) { Session session = SessionManager.getSessionFactory().openSession(); tryLoad(session, persistantEntity, id); - runnable.run(); + consumer.accept(session); if (close) session.close();