Various things and fixes
This commit is contained in:
1 parent
38bd0cd4e2
commit
93437ab8aa
22 files changed
+217
-192
No files matched your search
@@ -108,7 +108,7 @@
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<version>5.0.0-beta.12</version>
|
||||
<version>5.0.0-beta.18</version>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.sedmelluq</groupId>-->
|
||||
|
||||
@@ -34,7 +34,7 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
|
||||
|
||||
@Override
|
||||
public boolean canBeExecutedBy(Guild guild, Member member) {
|
||||
String roleDb = GuildManager.getManager(guild).getSetting(GSettings.MUSIC_ROLE);
|
||||
String roleDb = GuildManager.getManager(guild).getGuildConfigManager().getSetting(GSettings.MUSIC_ROLE);
|
||||
|
||||
if (roleDb != null && !roleDb.isEmpty()) {
|
||||
if (UserFunction.isAdmin(member)) {
|
||||
@@ -44,7 +44,7 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
|
||||
Role neededRole = guild.getRoleById(roleDb);
|
||||
if (neededRole == null) {
|
||||
// Role doesn't exist on guild anymore, remove from db
|
||||
GuildManager.getManager(guild).setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
GuildManager.getManager(guild).getGuildConfigManager().setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
|
||||
if (channel.getType() != ChannelType.TEXT)
|
||||
return false;
|
||||
|
||||
String channelDb = GuildManager.getManager(guild).getSetting(GSettings.MUSIC_CHANNEL);
|
||||
String channelDb = GuildManager.getManager(guild).getGuildConfigManager().getSetting(GSettings.MUSIC_CHANNEL);
|
||||
|
||||
if (channelDb != null && !channelDb.isEmpty()) {
|
||||
return channelDb.equals(channel.getId());
|
||||
|
||||
@@ -38,6 +38,7 @@ public interface ICommand {
|
||||
|
||||
boolean canBeExecutedFrom(Guild guild, MessageChannel channel);
|
||||
|
||||
@Deprecated
|
||||
void execute(MessageCommandContext context);
|
||||
|
||||
void execute(SlashCommandContext context);
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
// Role for music commands
|
||||
if (context.getArgument(0).equals("music-role")) {
|
||||
if (args.length == 1) {
|
||||
String roleDb = guildManager.getSetting(GSettings.MUSIC_ROLE);
|
||||
String roleDb = guildManager.getGuildConfigManager().getSetting(GSettings.MUSIC_ROLE);
|
||||
Role role = null;
|
||||
if (!roleDb.isEmpty()) {
|
||||
role = context.getGuild().getRoleById(roleDb);
|
||||
@@ -101,8 +101,8 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
|
||||
if (args.length == 2) {
|
||||
if (context.getArgument(1).equals("reset")) {
|
||||
if (!guildManager.getSetting(GSettings.MUSIC_ROLE).isEmpty()) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
if (!guildManager.getGuildConfigManager().getSetting(GSettings.MUSIC_ROLE).isEmpty()) {
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
}
|
||||
|
||||
context.reply("Successful reset of the role");
|
||||
@@ -112,7 +112,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
if (roles.size() == 1) {
|
||||
Role role = roles.get(0);
|
||||
if (role.getGuild().getId().equals(context.getGuild().getId())) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_ROLE, role.getId());
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_ROLE, role.getId());
|
||||
context.reply("Successfully set %s as the role needed for music command", role.getAsMention());
|
||||
}
|
||||
}
|
||||
@@ -122,7 +122,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
// Role for music commands
|
||||
if (context.getArgument(0).equals("music-channel")) {
|
||||
if (args.length == 1) {
|
||||
String channelDb = guildManager.getSetting(GSettings.MUSIC_CHANNEL);
|
||||
String channelDb = guildManager.getGuildConfigManager().getSetting(GSettings.MUSIC_CHANNEL);
|
||||
TextChannel channel = null;
|
||||
if (!channelDb.isEmpty()) {
|
||||
channel = context.getGuild().getTextChannelById(channelDb);
|
||||
@@ -134,8 +134,8 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
|
||||
if (args.length == 2) {
|
||||
if (context.getArgument(1).equals("reset")) {
|
||||
if (!guildManager.getSetting(GSettings.MUSIC_CHANNEL).isEmpty()) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_CHANNEL, null);
|
||||
if (!guildManager.getGuildConfigManager().getSetting(GSettings.MUSIC_CHANNEL).isEmpty()) {
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_CHANNEL, null);
|
||||
}
|
||||
|
||||
context.reply("Successful reset of the channel");
|
||||
@@ -145,7 +145,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
if (channels.size() == 1) {
|
||||
TextChannel channel = channels.get(0);
|
||||
if (channel.getGuild().getId().equals(context.getGuild().getId())) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_CHANNEL, channel.getId());
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_CHANNEL, channel.getId());
|
||||
context.reply("Successfully set %s as the channel required to execute music command", channel.getAsMention());
|
||||
}
|
||||
}
|
||||
@@ -155,7 +155,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
// Default volume for music player
|
||||
if (context.getArgument(0).equals("music-volume")) {
|
||||
if (args.length == 1) {
|
||||
int volume = Integer.parseInt(guildManager.getSetting(GSettings.MUSIC_VOLUME));
|
||||
int volume = Integer.parseInt(guildManager.getGuildConfigManager().getSetting(GSettings.MUSIC_VOLUME));
|
||||
|
||||
context.reply("Default volume for music player: %d", volume);
|
||||
context.reply("Set using 'conf music-volume <value between 0 and 100>'");
|
||||
@@ -166,7 +166,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
int volume = Integer.parseInt(args[1]);
|
||||
|
||||
if (volume > 0 && volume <= 100) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_VOLUME, String.valueOf(volume));
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_VOLUME, String.valueOf(volume));
|
||||
MusicManager.get(context.getGuild()).setVolume(volume);
|
||||
|
||||
context.reply("Default volume for music player is now %d", volume);
|
||||
@@ -218,13 +218,13 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
assert role != null;
|
||||
|
||||
if (role.getPosition() == -1) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_ROLE, null);
|
||||
context.replyEphemeral("Everyone can now use music commands");
|
||||
return;
|
||||
}
|
||||
|
||||
if (role.getGuild().getId().equals(context.getGuild().getId())) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_ROLE, role.getId());
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_ROLE, role.getId());
|
||||
context.replyEphemeral("Successfully set %s as the role needed for music command", role.getAsMention());
|
||||
return;
|
||||
}
|
||||
@@ -234,14 +234,14 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
GuildChannelUnion channel = context.getEvent().getOption("channel", OptionMapping::getAsChannel);
|
||||
|
||||
if (channel == null) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_CHANNEL, null);
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_CHANNEL, null);
|
||||
context.replyEphemeral("No channel restrictions for music commands");
|
||||
return;
|
||||
}
|
||||
|
||||
if (channel instanceof TextChannel) {
|
||||
if (channel.getGuild().getId().equals(context.getGuild().getId())) {
|
||||
guildManager.setSettingValue(GSettings.MUSIC_CHANNEL, channel.getId());
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_CHANNEL, channel.getId());
|
||||
context.replyEphemeral("Successfully set %s as the channel required to execute music command", channel.getAsMention());
|
||||
return;
|
||||
}
|
||||
@@ -251,7 +251,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
|
||||
if ("music-volume".equals(sub)) {
|
||||
int volume = context.getEvent().getOption("volume", 30, OptionMapping::getAsInt);
|
||||
guildManager.setSettingValue(GSettings.MUSIC_VOLUME, String.valueOf(volume));
|
||||
guildManager.getGuildConfigManager().setSettingValue(GSettings.MUSIC_VOLUME, String.valueOf(volume));
|
||||
MusicManager.get(context.getGuild()).setVolume(volume);
|
||||
|
||||
context.replyEphemeral("Default volume for music player is now %d", volume);
|
||||
@@ -267,7 +267,7 @@ public class ConfigureCommand extends AbstractCommand {
|
||||
}
|
||||
|
||||
if ("show-all".equals(sub)) {
|
||||
Map<String, String> settings = guildManager.getSettings();
|
||||
Map<String, String> settings = guildManager.getGuildConfigManager().getSettings();
|
||||
MessageCreateBuilder builder = new MessageCreateBuilder();
|
||||
|
||||
for (GSettings o : GSettings.values()) {
|
||||
|
||||
@@ -6,7 +6,7 @@ package netneutrality.core;
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.music.MusicManager;
|
||||
import netneutrality.music.TrackScheduler;
|
||||
import org.json.JSONArray;
|
||||
@@ -35,7 +35,7 @@ public class BotState {
|
||||
JSONObject music = new JSONObject();
|
||||
guild.put("id", manager.getGuild().getId());
|
||||
|
||||
TextChannel currentChannel = trackScheduler.getTextChannel();
|
||||
MessageChannel currentChannel = trackScheduler.getTextChannel();
|
||||
if (currentChannel == null) {
|
||||
music.put("current_channel", "null");
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package netneutrality.core;/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
* https://xtremcommunity.com/
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import netneutrality.db.GSettings;
|
||||
import netneutrality.db.entities.OGuild;
|
||||
import netneutrality.db.entities.OGuildSetting;
|
||||
import netneutrality.utils.JpaUtils;
|
||||
import org.hibernate.Session;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class GuildConfigManager {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GuildConfigManager.class);
|
||||
private final Guild guild;
|
||||
private final OGuild guildEntity;
|
||||
|
||||
private final Map<String, String> settingsCache = new HashMap<>();
|
||||
|
||||
public GuildConfigManager(Guild guild, OGuild guildEntity) {
|
||||
this.guild = guild;
|
||||
this.guildEntity = guildEntity;
|
||||
|
||||
reloadSettingsCache();
|
||||
}
|
||||
|
||||
public Map<String, String> getSettings() {
|
||||
return settingsCache;
|
||||
}
|
||||
|
||||
public String getSetting(GSettings setting) {
|
||||
String value = settingsCache.get(setting.getName());
|
||||
|
||||
if (value == null) {
|
||||
value = setting.getDefaultValue();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
public void setSettingValue(GSettings setting, String value) {
|
||||
Session session = SessionManager.getSessionFactory().openSession();
|
||||
JpaUtils.tryLoad(session, guildEntity, guildEntity.getGuildId());
|
||||
|
||||
OGuildSetting oGuildSetting = guildEntity.getSettingByName(setting.getName());
|
||||
|
||||
session.beginTransaction();
|
||||
if (value == null || setting.getDefaultValue().equals(value)) {
|
||||
if (oGuildSetting != null) {
|
||||
session.delete(oGuildSetting);
|
||||
}
|
||||
} else {
|
||||
if (oGuildSetting == null) {
|
||||
oGuildSetting = new OGuildSetting();
|
||||
oGuildSetting.setGuild(guildEntity);
|
||||
oGuildSetting.setName(setting.getName());
|
||||
oGuildSetting.setValue(value);
|
||||
|
||||
session.save(oGuildSetting);
|
||||
} else {
|
||||
oGuildSetting.setValue(value);
|
||||
|
||||
session.update(oGuildSetting);
|
||||
}
|
||||
}
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
reloadSettingsCache();
|
||||
}
|
||||
|
||||
public void reloadSettingsCache() {
|
||||
logger.debug("Reloading settings cache for guild: " + guild.getId());
|
||||
settingsCache.clear();
|
||||
|
||||
JpaUtils.getWithSession(guildEntity, session -> {
|
||||
Set<OGuildSetting> settingSet = guildEntity.getGuildSettings();
|
||||
|
||||
for (OGuildSetting setting : settingSet) {
|
||||
settingsCache.put(setting.getName(), setting.getValue());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,14 +9,12 @@ package netneutrality.core;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.NetNeutrality;
|
||||
import netneutrality.db.DBController;
|
||||
import netneutrality.db.GSettings;
|
||||
import netneutrality.db.controllers.DBMessage;
|
||||
import netneutrality.db.entities.OBlacklist;
|
||||
import netneutrality.db.entities.OGuild;
|
||||
import netneutrality.db.entities.OGuildSetting;
|
||||
import netneutrality.thread.ThreadManager;
|
||||
import netneutrality.utils.JpaUtils;
|
||||
import netneutrality.utils.MessageFunction;
|
||||
@@ -25,7 +23,6 @@ import org.hibernate.SessionFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -36,18 +33,18 @@ public class GuildManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GuildManager.class);
|
||||
private static final Map<String, GuildManager> managers = new ConcurrentHashMap<>();
|
||||
private static final SessionFactory sessionFactory = SessionManager.getSessionFactory();
|
||||
private final Map<String, String> settingsCache = new HashMap<>();
|
||||
private final Set<String> blacklistCache = new HashSet<>();
|
||||
private final GuildConfigManager guildConfigManager;
|
||||
private final Guild guild;
|
||||
private OGuild guildEntity;
|
||||
|
||||
private GuildManager(Guild guild) {
|
||||
this.guild = guild;
|
||||
this.guildEntity = loadGuildFromDb(guild.getId());
|
||||
this.guildConfigManager = new GuildConfigManager(guild, this.guildEntity);
|
||||
|
||||
managers.put(guild.getId(), this);
|
||||
reloadBlacklistCache();
|
||||
reloadSettingsCache();
|
||||
}
|
||||
|
||||
private GuildManager(String guildId) {
|
||||
@@ -57,10 +54,10 @@ public class GuildManager {
|
||||
|
||||
this.guild = temp;
|
||||
this.guildEntity = loadGuildFromDb(guild.getId());
|
||||
this.guildConfigManager = new GuildConfigManager(guild, this.guildEntity);
|
||||
|
||||
managers.put(guild.getId(), this);
|
||||
reloadBlacklistCache();
|
||||
reloadSettingsCache();
|
||||
}
|
||||
|
||||
public static GuildManager getManager(Guild guild) {
|
||||
@@ -96,8 +93,8 @@ public class GuildManager {
|
||||
return blacklistCache;
|
||||
}
|
||||
|
||||
public Map<String, String> getSettings() {
|
||||
return settingsCache;
|
||||
public GuildConfigManager getGuildConfigManager() {
|
||||
return guildConfigManager;
|
||||
}
|
||||
|
||||
// Methods
|
||||
@@ -109,47 +106,6 @@ public class GuildManager {
|
||||
return guildEntity.getDebug();
|
||||
}
|
||||
|
||||
public String getSetting(GSettings setting) {
|
||||
String value = settingsCache.get(setting.getName());
|
||||
|
||||
if (value == null) {
|
||||
value = setting.getDefaultValue();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setSettingValue(GSettings setting, String value) {
|
||||
Session session = sessionFactory.openSession();
|
||||
JpaUtils.tryLoad(session, guildEntity, guildEntity.getGuildId());
|
||||
|
||||
OGuildSetting oGuildSetting = guildEntity.getSettingByName(setting.getName());
|
||||
|
||||
session.beginTransaction();
|
||||
if (value == null || setting.getDefaultValue().equals(value)) {
|
||||
if (oGuildSetting != null) {
|
||||
session.delete(oGuildSetting);
|
||||
}
|
||||
} else {
|
||||
if (oGuildSetting == null) {
|
||||
oGuildSetting = new OGuildSetting();
|
||||
oGuildSetting.setGuild(guildEntity);
|
||||
oGuildSetting.setName(setting.getName());
|
||||
oGuildSetting.setValue(value);
|
||||
|
||||
session.save(oGuildSetting);
|
||||
} else {
|
||||
oGuildSetting.setValue(value);
|
||||
|
||||
session.update(oGuildSetting);
|
||||
}
|
||||
}
|
||||
|
||||
session.getTransaction().commit();
|
||||
reloadSettingsCache(false);
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void setDebug(boolean toggle) {
|
||||
if (toggle == isDebugEnabled())
|
||||
return;
|
||||
@@ -157,30 +113,11 @@ public class GuildManager {
|
||||
JpaUtils.updateWithTransaction(guildEntity, () -> guildEntity.setDebug(toggle));
|
||||
}
|
||||
|
||||
public boolean reloadSettingsCache(boolean close) {
|
||||
logger.debug("Reloading settings cache for guild: " + guild.getId());
|
||||
settingsCache.clear();
|
||||
|
||||
JpaUtils.getWithSession(guildEntity, close, session -> {
|
||||
Set<OGuildSetting> settingSet = guildEntity.getGuildSettings();
|
||||
|
||||
for (OGuildSetting setting : settingSet) {
|
||||
settingsCache.put(setting.getName(), setting.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean reloadSettingsCache() {
|
||||
return reloadSettingsCache(true);
|
||||
}
|
||||
|
||||
public void reloadBlacklistCache(boolean close) {
|
||||
public void reloadBlacklistCache() {
|
||||
logger.debug("Reloading blacklist cache for guild: " + guild.getId());
|
||||
blacklistCache.clear();
|
||||
|
||||
JpaUtils.getWithSession(guildEntity, close, session -> {
|
||||
JpaUtils.getWithSession(guildEntity, session -> {
|
||||
Set<OBlacklist> oBlacklistSet = guildEntity.getBlacklists();
|
||||
|
||||
for (OBlacklist bl : oBlacklistSet) {
|
||||
@@ -191,17 +128,13 @@ public class GuildManager {
|
||||
});
|
||||
}
|
||||
|
||||
public void reloadBlacklistCache() {
|
||||
reloadBlacklistCache(true);
|
||||
}
|
||||
|
||||
public void checkAndLogMessage(Message message, Member member, TextChannel channel) {
|
||||
public void checkAndLogMessage(Message message, Member member, MessageChannel channel) {
|
||||
if (isLoggingEnabled()) {
|
||||
ThreadManager.getExecutor().submit(() -> DBController.get(DBMessage.class).log(this, channel, member, message));
|
||||
}
|
||||
}
|
||||
|
||||
public void checkBlacklist(Message message, TextChannel channel) {
|
||||
public void checkBlacklist(Message message, MessageChannel channel) {
|
||||
MessageFunction.testMessageAgainstBlacklist(message, channel, blacklistCache);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.sentry.Sentry;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.core.context.SlashCommandContext;
|
||||
import netneutrality.exceptions.CommandException;
|
||||
import netneutrality.exceptions.CommandExecutionException;
|
||||
import netneutrality.utils.MessageFunction;
|
||||
@@ -30,6 +31,8 @@ public class LogHandler {
|
||||
Sentry.captureException(e);
|
||||
logInternally((CommandExecutionException) e);
|
||||
tryLogToDebugChannel((CommandExecutionException) e);
|
||||
tryGracefulSlashContextError((CommandExecutionException) e);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -75,4 +78,12 @@ public class LogHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void tryGracefulSlashContextError(CommandExecutionException error) {
|
||||
CommandContext context = error.getCommandContext();
|
||||
|
||||
if (context instanceof SlashCommandContext slashCommandContext) {
|
||||
slashCommandContext.replyEphemeral("Error during command execution");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,6 @@ public interface CommandContext {
|
||||
default void replyToMessage(String format, Object... arguments) {
|
||||
replyToMessage(String.format(format, arguments));
|
||||
}
|
||||
|
||||
void dispatchToCommand(String commandName);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.core.GuildManager;
|
||||
import netneutrality.core.LogHandler;
|
||||
import netneutrality.exceptions.CommandExecutionException;
|
||||
@@ -193,7 +194,12 @@ public class MessageCommandContext implements CommandContext {
|
||||
return creatorId.equals(member.getId());
|
||||
}
|
||||
|
||||
public void dispatch(AbstractCommand command) {
|
||||
public void dispatchToCommand(String commandName) {
|
||||
if (!CommandManager.isValidCommand(commandName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
AbstractCommand command = CommandManager.getCommand(commandName);
|
||||
this.command = command;
|
||||
//TODO: Add usage check
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.core.LogHandler;
|
||||
import netneutrality.exceptions.CommandExecutionException;
|
||||
import netneutrality.utils.UserFunction;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -180,7 +179,6 @@ public class SlashCommandContext implements CommandContext {
|
||||
|
||||
Sentry.configureScope(scope -> {
|
||||
scope.setContexts("command", command.getCommandName());
|
||||
scope.setContexts("isAdmin", UserFunction.isAdmin(member));
|
||||
scope.setContexts("canBeExecuted", command.canBeExecutedBy(guild, member));
|
||||
scope.setTag("channel", channel.getId());
|
||||
scope.setTag("interactionId", event.getInteraction().getId());
|
||||
|
||||
@@ -22,11 +22,13 @@ public class DBBlacklist extends AbstractController {
|
||||
|
||||
public void add(String word, CommandContext context) {
|
||||
GuildManager guildManager = GuildManager.getManager(context.getGuild());
|
||||
Session session = getSessionFactory().openSession();
|
||||
|
||||
|
||||
OBlacklist bl = getWordFromDb(word, context.getGuild().getId());
|
||||
|
||||
if (bl == null) {
|
||||
Session session = getSessionFactory().openSession();
|
||||
|
||||
session.beginTransaction();
|
||||
bl = new OBlacklist();
|
||||
bl.setGuild(guildManager.getGuildEntity());
|
||||
@@ -36,73 +38,81 @@ public class DBBlacklist extends AbstractController {
|
||||
|
||||
session.save(bl);
|
||||
session.getTransaction().commit();
|
||||
guildManager.reloadBlacklistCache(false);
|
||||
session.close();
|
||||
|
||||
guildManager.reloadBlacklistCache();
|
||||
context.reply(String.format("Added the word [%s] to the blacklist.", word));
|
||||
} else {
|
||||
context.reply("Word is already blacklisted");
|
||||
}
|
||||
|
||||
session.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void delete(String word, CommandContext context) {
|
||||
GuildManager guildManager = GuildManager.getManager(context.getGuild());
|
||||
Session session = getSessionFactory().openSession();
|
||||
|
||||
OBlacklist bl = getWordFromDb(word, context.getGuild().getId());
|
||||
|
||||
if (bl != null) {
|
||||
Session session = getSessionFactory().openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
session.delete(bl);
|
||||
session.getTransaction().commit();
|
||||
guildManager.reloadBlacklistCache(false);
|
||||
session.close();
|
||||
|
||||
guildManager.reloadBlacklistCache();
|
||||
context.reply("Removed the word [%s] from the blacklist.", word);
|
||||
} else {
|
||||
context.reply("Can't delete word because it doesn't exist");
|
||||
}
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void enable(String word, CommandContext context) {
|
||||
GuildManager guildManager = GuildManager.getManager(context.getGuild());
|
||||
Session session = getSessionFactory().openSession();
|
||||
|
||||
|
||||
OBlacklist bl = getWordFromDb(word, context.getGuild().getId());
|
||||
if (bl != null && !bl.getEnabled()) {
|
||||
Session session = getSessionFactory().openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
bl.setEnabled(true);
|
||||
|
||||
session.update(bl);
|
||||
session.getTransaction().commit();
|
||||
guildManager.reloadBlacklistCache(false);
|
||||
session.close();
|
||||
|
||||
guildManager.reloadBlacklistCache();
|
||||
context.reply("Enabled the word [%s] for the blacklist.", word);
|
||||
} else {
|
||||
context.reply("Either word doesn't exist or is already enabled");
|
||||
}
|
||||
|
||||
session.close();
|
||||
|
||||
}
|
||||
|
||||
public void disable(String word, CommandContext context) {
|
||||
GuildManager guildManager = GuildManager.getManager(context.getGuild());
|
||||
Session session = getSessionFactory().openSession();
|
||||
|
||||
|
||||
OBlacklist bl = getWordFromDb(word, context.getGuild().getId());
|
||||
if (bl != null && bl.getEnabled()) {
|
||||
Session session = getSessionFactory().openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
bl.setEnabled(false);
|
||||
|
||||
session.update(bl);
|
||||
session.getTransaction().commit();
|
||||
guildManager.reloadBlacklistCache(false);
|
||||
session.close();
|
||||
|
||||
guildManager.reloadBlacklistCache();
|
||||
context.reply("Disabled the word [%s] for the blacklist.", word);
|
||||
} else {
|
||||
context.reply("Either word doesn't exist or is already disabled");
|
||||
}
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
public ArrayList<OBlacklist> getBlacklistForGuild(Guild guild) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
package netneutrality.db.controllers;
|
||||
|
||||
/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
@@ -9,7 +8,7 @@ package netneutrality.db.controllers;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.core.GuildManager;
|
||||
import netneutrality.db.AbstractController;
|
||||
import netneutrality.db.entities.OLogMessage;
|
||||
@@ -24,7 +23,7 @@ public class DBMessage extends AbstractController {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(DBMessage.class);
|
||||
|
||||
public void log(GuildManager guildManager, TextChannel channel, Member author, Message message) {
|
||||
public void log(GuildManager guildManager, MessageChannel channel, Member author, Message message) {
|
||||
Session session = getSessionFactory().openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import netneutrality.core.SessionManager;
|
||||
import netneutrality.db.DBController;
|
||||
import netneutrality.db.controllers.DBMessage;
|
||||
import netneutrality.db.entities.OGuild;
|
||||
import netneutrality.utils.JpaUtils;
|
||||
import netneutrality.utils.MessageFunction;
|
||||
import org.hibernate.Session;
|
||||
|
||||
@@ -34,7 +35,7 @@ public class EditHandler extends ListenerAdapter {
|
||||
|
||||
if (guildEntity.getLogChannel() != null && !guildEntity.getLogChannel().isEmpty()) {
|
||||
Session session = SessionManager.getSessionFactory().openSession();
|
||||
session.load(guildEntity, guildEntity.getGuildId());
|
||||
JpaUtils.tryLoad(session, guildEntity, guildEntity.getGuildId());
|
||||
|
||||
int size = guildEntity.getLoggings()
|
||||
.stream()
|
||||
|
||||
@@ -8,14 +8,9 @@ package netneutrality.hooks.message;
|
||||
|
||||
import io.sentry.Breadcrumb;
|
||||
import io.sentry.Sentry;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.core.GuildManager;
|
||||
import netneutrality.core.context.MessageCommandContext;
|
||||
import netneutrality.utils.Settings;
|
||||
@@ -32,15 +27,11 @@ public class MessageReceivedHandler extends ListenerAdapter {
|
||||
return;
|
||||
|
||||
Message message = event.getMessage();
|
||||
Member member = event.getMember();
|
||||
Guild guild = event.getGuild();
|
||||
TextChannel channel = (TextChannel) event.getChannel();
|
||||
|
||||
Sentry.addBreadcrumb(Breadcrumb.info("New message from " + member.getEffectiveName()));
|
||||
|
||||
MessageCommandContext context = MessageCommandContext.createContext(event);
|
||||
Sentry.addBreadcrumb(Breadcrumb.info("New message from " + context.getGuildUser().getEffectiveName()));
|
||||
|
||||
GuildManager.getManager(guild).checkAndLogMessage(message, member, channel);
|
||||
GuildManager.getManager(context.getGuild()).checkAndLogMessage(message, context.getGuildUser(), context.getChannel());
|
||||
|
||||
String rawMessage = message.getContentDisplay().trim();
|
||||
String prefix = Settings.getInstance().get("BOT_PREFIX", "$");
|
||||
@@ -50,13 +41,7 @@ public class MessageReceivedHandler extends ListenerAdapter {
|
||||
// rawMessage = rawMessage.substring(prefix.length()).trim();
|
||||
String commandString = rawMessage.split(" ", 2)[0];
|
||||
|
||||
|
||||
if (CommandManager.isValidCommand(commandString)) {
|
||||
AbstractCommand command = CommandManager.getCommand(commandString);
|
||||
|
||||
context.dispatch(command);
|
||||
}
|
||||
|
||||
context.dispatchToCommand(commandString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class ActionHandler extends AbstractHandler {
|
||||
Guild guild = NetNeutrality.getJDA().getGuildById(guildId);
|
||||
if (guild != null) {
|
||||
GuildManager manager = GuildManager.getManager(guild);
|
||||
Map<String, String> settings = manager.getSettings();
|
||||
Map<String, String> settings = manager.getGuildConfigManager().getSettings();
|
||||
JSONObject setting = new JSONObject(settings);
|
||||
|
||||
json.put("status", "ok");
|
||||
@@ -68,11 +68,11 @@ public class ActionHandler extends AbstractHandler {
|
||||
String guildId = args[args.length - 1];
|
||||
|
||||
Guild guild = NetNeutrality.getJDA().getGuildById(guildId);
|
||||
boolean ok;
|
||||
boolean ok = true; //TODO: add bool
|
||||
if (guild == null)
|
||||
ok = GuildManager.getManager(guildId).reloadSettingsCache();
|
||||
GuildManager.getManager(guildId).getGuildConfigManager().reloadSettingsCache();
|
||||
else
|
||||
ok = GuildManager.getManager(guild).reloadSettingsCache();
|
||||
GuildManager.getManager(guild).getGuildConfigManager().reloadSettingsCache();
|
||||
|
||||
|
||||
json.put("status", String.valueOf(ok));
|
||||
|
||||
@@ -17,6 +17,7 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.core.GuildManager;
|
||||
import netneutrality.core.LogHandler;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
@@ -55,7 +56,7 @@ public class MusicManager {
|
||||
this.guild = guild;
|
||||
this.player = playerManager.createPlayer();
|
||||
this.scheduler = new TrackScheduler(this);
|
||||
this.baseVolume = Integer.parseInt(GuildManager.getManager(guild).getSetting(GSettings.MUSIC_VOLUME));
|
||||
this.baseVolume = Integer.parseInt(GuildManager.getManager(guild).getGuildConfigManager().getSetting(GSettings.MUSIC_VOLUME));
|
||||
|
||||
guild.getAudioManager().setSendingHandler(new AudioPlayerSendHandler(player));
|
||||
player.addListener(scheduler);
|
||||
@@ -92,11 +93,10 @@ public class MusicManager {
|
||||
player.setVolume(baseVolume);
|
||||
|
||||
if (autoDisconnectThread == null)
|
||||
autoDisconnectThread = executor.scheduleWithFixedDelay(new MusicPlayerAutoDisconnectThread(this), 120, 120, TimeUnit.SECONDS);
|
||||
autoDisconnectThread = executor.scheduleWithFixedDelay(new MusicPlayerAutoDisconnectThread(this), 240, 240, TimeUnit.SECONDS);
|
||||
} else {
|
||||
throw new RuntimeException("Error connecting to voice channel");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
@@ -165,11 +165,11 @@ public class MusicManager {
|
||||
|
||||
public void play(List<LoadableSong> songs, CommandContext context, boolean needToBeConnected, boolean silent) {
|
||||
Member member = context.getGuildUser();
|
||||
TextChannel channel = (TextChannel) context.getChannel();
|
||||
MessageChannel channel = context.getChannel();
|
||||
|
||||
if (member.getVoiceState() == null) {
|
||||
logger.error("Guild {}: Voice state is null", guild.getId());
|
||||
context.replyToMessage("Voice state is null, something has gone wrong.");
|
||||
context.replyToMessage("Voice state is null, something has gone terribly wrong.");
|
||||
LogHandler.log(new CommandExecutionException("Voice state is null", context, null));
|
||||
|
||||
return;
|
||||
|
||||
@@ -119,6 +119,7 @@ public class TrackLoader implements Runnable {
|
||||
}
|
||||
} catch (InterruptedException exception) {
|
||||
stopLoading();
|
||||
context.replyToMessage("Loading of tracks was interrupted due to an exception");
|
||||
LogHandler.log(exception);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.core.LogHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -24,7 +25,7 @@ public class TrackScheduler extends AudioEventAdapter {
|
||||
private final QueueManager queueManager;
|
||||
private final MusicManager manager;
|
||||
private final AudioPlayer player;
|
||||
private TextChannel currentChannel;
|
||||
private MessageChannel currentChannel;
|
||||
private final Guild guild;
|
||||
|
||||
public TrackScheduler(MusicManager manager) {
|
||||
@@ -50,11 +51,11 @@ public class TrackScheduler extends AudioEventAdapter {
|
||||
return guild;
|
||||
}
|
||||
|
||||
public TextChannel getTextChannel() {
|
||||
public MessageChannel getTextChannel() {
|
||||
return currentChannel;
|
||||
}
|
||||
|
||||
public synchronized void attachTextChannel(TextChannel channel) {
|
||||
public synchronized void attachTextChannel(MessageChannel channel) {
|
||||
this.currentChannel = channel;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
package netneutrality.thread;/*
|
||||
package netneutrality.thread;
|
||||
/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
* https://xtremcommunity.com/
|
||||
@@ -18,6 +19,7 @@ public class MusicPlayerAutoDisconnectThread implements Runnable {
|
||||
|
||||
final MusicManager manager;
|
||||
|
||||
//TODO: move to scheduler
|
||||
public MusicPlayerAutoDisconnectThread(MusicManager musicManager) {
|
||||
manager = musicManager;
|
||||
}
|
||||
@@ -27,13 +29,26 @@ public class MusicPlayerAutoDisconnectThread implements Runnable {
|
||||
Guild guild = manager.getGuild();
|
||||
VoiceChannel channel = (VoiceChannel) guild.getAudioManager().getConnectedChannel();
|
||||
|
||||
try {
|
||||
if (channel != null) {
|
||||
List<Member> members = channel.getMembers().stream().filter(member -> !member.getId().equals(NetNeutrality.getSelf().getId())).toList();
|
||||
if (conditionsMetToDisconnect(channel)) {
|
||||
// Sleep 30 seconds then check again (prevents disconnecting right after a track ends)
|
||||
Thread.sleep(30000);
|
||||
|
||||
if (members.size() == 0 || manager.getScheduler().getQueueManager().isEmpty()) {
|
||||
if (conditionsMetToDisconnect(channel)) {
|
||||
LoggerFactory.getLogger(MusicPlayerAutoDisconnectThread.class).debug("Guild {}: auto-disconnect player from voice channel: {}", guild.getId(), channel.getId());
|
||||
manager.leave();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean conditionsMetToDisconnect(VoiceChannel channel) {
|
||||
List<Member> members = channel.getMembers().stream().filter(member -> !member.getId().equals(NetNeutrality.getSelf().getId())).toList();
|
||||
|
||||
return members.size() == 0 || manager.getScheduler().getQueueManager().isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -70,28 +70,17 @@ public class JpaUtils {
|
||||
getWithSession(oGuild, oGuild.getGuildId(), consumer);
|
||||
}
|
||||
|
||||
public static void getWithSession(OGuild oGuild, boolean close, Consumer<Session> consumer) {
|
||||
getWithSession(oGuild, oGuild.getGuildId(), close, consumer);
|
||||
}
|
||||
|
||||
public static void getWithSession(Object persistantEntity, Serializable id, Consumer<Session> consumer) {
|
||||
getWithSession(persistantEntity, id, true, consumer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
public static void getWithSession(Object persistantEntity, Serializable id, Consumer<Session> consumer) {
|
||||
Session session = SessionManager.getSessionFactory().openSession();
|
||||
tryLoad(session, persistantEntity, id);
|
||||
|
||||
consumer.accept(session);
|
||||
|
||||
if (close)
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,7 @@ import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.channel.ChannelType;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.command.CommandType;
|
||||
import netneutrality.core.CommandManager;
|
||||
@@ -39,28 +38,6 @@ public class MessageFunction {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MessageFunction.class);
|
||||
|
||||
public static Member getStartingMention(Message message) {
|
||||
if (isStartingWithMention(message)) {
|
||||
return message.getMentions().getMembers().get(0);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isStartingWithMention(Message message) {
|
||||
if (!message.isFromType(ChannelType.TEXT))
|
||||
return false;
|
||||
|
||||
List<Member> mentions = message.getMentions().getMembers();
|
||||
|
||||
for (Member member : mentions) {
|
||||
if (message.getContentDisplay().startsWith("@" + member.getUser().getName()))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static MessageEmbed buildCurrentlyPlayingMessage(MusicManager manager) {
|
||||
AudioTrack current = manager.getCurrentlyPlaying();
|
||||
|
||||
@@ -81,7 +58,7 @@ public class MessageFunction {
|
||||
String url = track.getIdentifier();
|
||||
String thumbnail = null;
|
||||
|
||||
if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch) {
|
||||
if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch || type == SourceFinder.AudioSourceType.YTMusic) {
|
||||
url = String.format("https://youtu.be/%s", track.getIdentifier());
|
||||
thumbnail = String.format("https://i3.ytimg.com/vi/%s/hqdefault.jpg", track.getIdentifier());
|
||||
}
|
||||
@@ -275,7 +252,7 @@ public class MessageFunction {
|
||||
return editEmbed.build();
|
||||
}
|
||||
|
||||
public static void testMessageAgainstBlacklist(Message message, TextChannel channel, Set<String> cache) {
|
||||
public static void testMessageAgainstBlacklist(Message message, MessageChannel channel, Set<String> cache) {
|
||||
boolean found = false;
|
||||
for (String word : cache) {
|
||||
if (message.getContentRaw().toLowerCase().contains(word)) {
|
||||
|
||||
Reference in new issue
Block a user