Add ability to limit music commands to a certain channel

This commit is contained in:
anthorob committed 2022-03-21 19:28:20 -04:00
1 parent 832fc0ff3d
commit ef038c5b5b
8 files changed
+100 -50

No files matched your search

@@ -2,6 +2,7 @@ package netneutrality.command;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
public abstract class AbstractCommand implements ICommand { public abstract class AbstractCommand implements ICommand {
@@ -9,4 +10,9 @@ public abstract class AbstractCommand implements ICommand {
public boolean canBeExecutedBy(Guild guild, Member member) { public boolean canBeExecutedBy(Guild guild, Member member) {
return true; return true;
} }
@Override
public boolean canBeExecutedFrom(Guild guild, TextChannel channel) {
return true;
}
} }
@@ -6,9 +6,7 @@ package netneutrality.command;
* https://discord.gg/CdFcyQ * https://discord.gg/CdFcyQ
*/ */
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import netneutrality.core.GuildManager; import netneutrality.core.GuildManager;
import netneutrality.utils.UserFunction; import netneutrality.utils.UserFunction;
@@ -27,17 +25,33 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
@Override @Override
public boolean canBeExecutedBy(Guild guild, Member member) { public boolean canBeExecutedBy(Guild guild, Member member) {
String roleDb = GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_ROLE); String roleDb = GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_ROLE);
boolean can = true;
//System.out.println(roleDb);
if (roleDb != null && !roleDb.isEmpty()) { if (roleDb != null && !roleDb.isEmpty()) {
if (UserFunction.isAdmin(member)) {
return true;
}
Role neededRole = guild.getRoleById(roleDb); 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;
} }
if (UserFunction.isAdmin(member)) return member.getRoles().contains(neededRole);
can = true; }
return can; return true;
}
@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;
} }
} }
@@ -8,6 +8,7 @@ package netneutrality.command;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import netneutrality.core.context.CommandContext; import netneutrality.core.context.CommandContext;
import netneutrality.exceptions.CommandErrorException; import netneutrality.exceptions.CommandErrorException;
import netneutrality.exceptions.CommandExecutionException; import netneutrality.exceptions.CommandExecutionException;
@@ -44,6 +45,8 @@ public interface ICommand {
boolean canBeExecutedBy(Guild guild, Member member); boolean canBeExecutedBy(Guild guild, Member member);
boolean canBeExecutedFrom(Guild guild, TextChannel channel);
/** /**
* Command is executed with this function. * Command is executed with this function.
* *
@@ -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<Role> roles = member.getRoles();
return false;
}
}
@@ -7,12 +7,12 @@ package netneutrality.command.bot;
*/ */
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import netneutrality.command.AbstractCommand; import netneutrality.command.AbstractCommand;
import netneutrality.command.CommandType; import netneutrality.command.CommandType;
import netneutrality.command.CommandUsage; import netneutrality.command.CommandUsage;
import netneutrality.core.GuildManager; import netneutrality.core.GuildManager;
import netneutrality.core.context.CommandContext; import netneutrality.core.context.CommandContext;
import netneutrality.exceptions.CommandErrorException;
import netneutrality.music.MusicManager; import netneutrality.music.MusicManager;
import java.util.List; import java.util.List;
@@ -41,7 +41,7 @@ public class ConfigureCommand extends AbstractCommand {
@Override @Override
public CommandUsage getUsage() { public CommandUsage getUsage() {
return CommandUsage.create(this, 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<TextChannel> 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 // Default volume for music player
if (context.getArg(0).equals("musicvolume")) { if (context.getArg(0).equals("musicvolume")) {
if (args.length == 1) { if (args.length == 1) {
@@ -191,7 +191,7 @@ public class GuildManager {
logger.debug("Reloading settings cache for guild: " + guild.getId()); logger.debug("Reloading settings cache for guild: " + guild.getId());
settingsCache.clear(); settingsCache.clear();
JpaUtils.getWithSession(guildEntity, close, () -> { JpaUtils.getWithSession(guildEntity, close, session -> {
Set<OGuildSetting> settingSet = guildEntity.getGuildSettings(); Set<OGuildSetting> settingSet = guildEntity.getGuildSettings();
for (OGuildSetting setting : settingSet) { for (OGuildSetting setting : settingSet) {
@@ -210,7 +210,7 @@ public class GuildManager {
logger.debug("Reloading blacklist cache for guild: " + guild.getId()); logger.debug("Reloading blacklist cache for guild: " + guild.getId());
blacklistCache.clear(); blacklistCache.clear();
JpaUtils.getWithSession(guildEntity, close, () -> { JpaUtils.getWithSession(guildEntity, close, session -> {
Set<OBlacklist> oBlacklistSet = guildEntity.getBlacklists(); Set<OBlacklist> oBlacklistSet = guildEntity.getBlacklists();
for (OBlacklist bl : oBlacklistSet) { for (OBlacklist bl : oBlacklistSet) {
@@ -179,15 +179,15 @@ public class CommandContext {
}); });
try { try {
if (command.needAdmin()) { if (command.needAdmin() && !UserFunction.isAdmin(member)) {
if (!UserFunction.isAdmin(member)) {
return; return;
} }
}
if (command.canBeExecutedBy(guild, member)) { if (command.canBeExecutedBy(guild, member)) {
if (command.canBeExecutedFrom(guild, channel)) {
command.execute(this); command.execute(this);
} }
}
} catch (CommandErrorException | CommandExecutionException e) { } catch (CommandErrorException | CommandExecutionException e) {
LogHandler.log(e); LogHandler.log(e);
} catch (Exception e) { } catch (Exception e) {
@@ -16,9 +16,22 @@ import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session; import org.hibernate.Session;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Consumer;
public class JpaUtils { 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) { public static void tryLoad(Session session, Object object, Serializable id) {
try { try {
session.load(object, id); session.load(object, id);
@@ -53,23 +66,30 @@ public class JpaUtils {
session.close(); session.close();
} }
public static void getWithSession(OGuild oGuild, Runnable runnable) { public static void getWithSession(OGuild oGuild, Consumer<Session> consumer) {
getWithSession(oGuild, oGuild.getGuildId(), runnable); getWithSession(oGuild, oGuild.getGuildId(), consumer);
} }
public static void getWithSession(OGuild oGuild, boolean close, Runnable runnable) { public static void getWithSession(OGuild oGuild, boolean close, Consumer<Session> consumer) {
getWithSession(oGuild, oGuild.getGuildId(), close, runnable); getWithSession(oGuild, oGuild.getGuildId(), close, consumer);
} }
public static void getWithSession(Object persistantEntity, Serializable id, Runnable runnable) { public static void getWithSession(Object persistantEntity, Serializable id, Consumer<Session> consumer) {
getWithSession(persistantEntity, id, true, runnable); 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<Session> consumer) {
Session session = SessionManager.getSessionFactory().openSession(); Session session = SessionManager.getSessionFactory().openSession();
tryLoad(session, persistantEntity, id); tryLoad(session, persistantEntity, id);
runnable.run(); consumer.accept(session);
if (close) if (close)
session.close(); session.close();