per-guild prefix
This commit is contained in:
1 parent
6f5451f37d
commit
054bff8c63
7 files changed
+72
-41
No files matched your search
@@ -27,7 +27,7 @@ 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.GSettings.MUSIC_ROLE);
|
||||||
|
|
||||||
if (roleDb != null && !roleDb.isEmpty()) {
|
if (roleDb != null && !roleDb.isEmpty()) {
|
||||||
if (UserFunction.isAdmin(member)) {
|
if (UserFunction.isAdmin(member)) {
|
||||||
@@ -37,7 +37,7 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
|
|||||||
Role neededRole = guild.getRoleById(roleDb);
|
Role neededRole = guild.getRoleById(roleDb);
|
||||||
if (neededRole == null) {
|
if (neededRole == null) {
|
||||||
// Role doesn't exist on guild anymore, remove from db
|
// Role doesn't exist on guild anymore, remove from db
|
||||||
GuildManager.getManager(guild).setSettingValue(GuildManager.Settings.MUSIC_ROLE, null);
|
GuildManager.getManager(guild).setSettingValue(GuildManager.GSettings.MUSIC_ROLE, null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ public abstract class AbstractMusicCommand extends AbstractCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canBeExecutedFrom(Guild guild, TextChannel channel) {
|
public boolean canBeExecutedFrom(Guild guild, TextChannel channel) {
|
||||||
String channelDb = GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_CHANNEL);
|
String channelDb = GuildManager.getManager(guild).getSetting(GuildManager.GSettings.MUSIC_CHANNEL);
|
||||||
|
|
||||||
if (channelDb != null && !channelDb.isEmpty()) {
|
if (channelDb != null && !channelDb.isEmpty()) {
|
||||||
return channelDb.equals(channel.getId());
|
return channelDb.equals(channel.getId());
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import netneutrality.core.GuildManager;
|
|||||||
import netneutrality.core.context.CommandContext;
|
import netneutrality.core.context.CommandContext;
|
||||||
import netneutrality.music.MusicManager;
|
import netneutrality.music.MusicManager;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ConfigureCommand extends AbstractCommand {
|
public class ConfigureCommand extends AbstractCommand {
|
||||||
@@ -65,7 +66,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
// Role for music commands
|
// Role for music commands
|
||||||
if (context.getArgument(0).equals("music-role")) {
|
if (context.getArgument(0).equals("music-role")) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
String roleDb = guildManager.getSetting(GuildManager.Settings.MUSIC_ROLE);
|
String roleDb = guildManager.getSetting(GuildManager.GSettings.MUSIC_ROLE);
|
||||||
Role role = null;
|
Role role = null;
|
||||||
if (!roleDb.isEmpty()) {
|
if (!roleDb.isEmpty()) {
|
||||||
role = context.getGuild().getRoleById(roleDb);
|
role = context.getGuild().getRoleById(roleDb);
|
||||||
@@ -77,8 +78,8 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
|
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
if (context.getArgument(1).equals("reset")) {
|
if (context.getArgument(1).equals("reset")) {
|
||||||
if (!guildManager.getSetting(GuildManager.Settings.MUSIC_ROLE).isEmpty()) {
|
if (!guildManager.getSetting(GuildManager.GSettings.MUSIC_ROLE).isEmpty()) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_ROLE, null);
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_ROLE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.reply("Successful reset of the role");
|
context.reply("Successful reset of the role");
|
||||||
@@ -88,7 +89,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
if (roles.size() == 1) {
|
if (roles.size() == 1) {
|
||||||
Role role = roles.get(0);
|
Role role = roles.get(0);
|
||||||
if (role.getGuild().getId().equals(context.getGuild().getId())) {
|
if (role.getGuild().getId().equals(context.getGuild().getId())) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_ROLE, role.getId());
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_ROLE, role.getId());
|
||||||
context.reply("Successfully set %s as the role needed for music command", role.getAsMention());
|
context.reply("Successfully set %s as the role needed for music command", role.getAsMention());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,7 +99,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
// Role for music commands
|
// Role for music commands
|
||||||
if (context.getArgument(0).equals("music-channel")) {
|
if (context.getArgument(0).equals("music-channel")) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
String channelDb = guildManager.getSetting(GuildManager.Settings.MUSIC_CHANNEL);
|
String channelDb = guildManager.getSetting(GuildManager.GSettings.MUSIC_CHANNEL);
|
||||||
TextChannel channel = null;
|
TextChannel channel = null;
|
||||||
if (!channelDb.isEmpty()) {
|
if (!channelDb.isEmpty()) {
|
||||||
channel = context.getGuild().getTextChannelById(channelDb);
|
channel = context.getGuild().getTextChannelById(channelDb);
|
||||||
@@ -110,8 +111,8 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
|
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
if (context.getArgument(1).equals("reset")) {
|
if (context.getArgument(1).equals("reset")) {
|
||||||
if (!guildManager.getSetting(GuildManager.Settings.MUSIC_CHANNEL).isEmpty()) {
|
if (!guildManager.getSetting(GuildManager.GSettings.MUSIC_CHANNEL).isEmpty()) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_CHANNEL, null);
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_CHANNEL, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.reply("Successful reset of the channel");
|
context.reply("Successful reset of the channel");
|
||||||
@@ -121,7 +122,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
if (channels.size() == 1) {
|
if (channels.size() == 1) {
|
||||||
TextChannel channel = channels.get(0);
|
TextChannel channel = channels.get(0);
|
||||||
if (channel.getGuild().getId().equals(context.getGuild().getId())) {
|
if (channel.getGuild().getId().equals(context.getGuild().getId())) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_CHANNEL, channel.getId());
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_CHANNEL, channel.getId());
|
||||||
context.reply("Successfully set %s as the channel required to execute music command", channel.getAsMention());
|
context.reply("Successfully set %s as the channel required to execute music command", channel.getAsMention());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,7 +132,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
// Default volume for music player
|
// Default volume for music player
|
||||||
if (context.getArgument(0).equals("music-volume")) {
|
if (context.getArgument(0).equals("music-volume")) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
int volume = Integer.parseInt(guildManager.getSetting(GuildManager.Settings.MUSIC_VOLUME));
|
int volume = Integer.parseInt(guildManager.getSetting(GuildManager.GSettings.MUSIC_VOLUME));
|
||||||
|
|
||||||
context.reply("Default volume for music player: %d", volume);
|
context.reply("Default volume for music player: %d", volume);
|
||||||
context.reply("Set using 'conf music-volume <value between 0 and 100>'");
|
context.reply("Set using 'conf music-volume <value between 0 and 100>'");
|
||||||
@@ -142,7 +143,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
int volume = Integer.parseInt(args[1]);
|
int volume = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
if (volume > 0 && volume <= 100) {
|
if (volume > 0 && volume <= 100) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_VOLUME, String.valueOf(volume));
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_VOLUME, String.valueOf(volume));
|
||||||
MusicManager.get(context.getGuild()).setVolume(volume);
|
MusicManager.get(context.getGuild()).setVolume(volume);
|
||||||
|
|
||||||
context.reply("Default volume for music player is now %d", volume);
|
context.reply("Default volume for music player is now %d", volume);
|
||||||
@@ -158,7 +159,7 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
// Music topic change
|
// Music topic change
|
||||||
if (context.getArgument(0).equals("music-topic")) {
|
if (context.getArgument(0).equals("music-topic")) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
boolean enabled = "1".equals(guildManager.getSetting(GuildManager.Settings.MUSIC_TOPIC));
|
boolean enabled = "1".equals(guildManager.getSetting(GuildManager.GSettings.MUSIC_TOPIC));
|
||||||
|
|
||||||
context.reply("Set currently playing track in text channel topic: %s", (enabled ? "enabled" : "disabled"));
|
context.reply("Set currently playing track in text channel topic: %s", (enabled ? "enabled" : "disabled"));
|
||||||
context.reply("Use 'conf music-topic 0/1' to toggle between states (0 = disabled, 1 = enabled).");
|
context.reply("Use 'conf music-topic 0/1' to toggle between states (0 = disabled, 1 = enabled).");
|
||||||
@@ -166,9 +167,9 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
|
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
String str = args[1];
|
String str = args[1];
|
||||||
boolean enabled = "1".equals(guildManager.getSetting(GuildManager.Settings.MUSIC_TOPIC));
|
boolean enabled = "1".equals(guildManager.getSetting(GuildManager.GSettings.MUSIC_TOPIC));
|
||||||
if (str.equals("0") || str.equals("1")) {
|
if (str.equals("0") || str.equals("1")) {
|
||||||
guildManager.setSettingValue(GuildManager.Settings.MUSIC_TOPIC, str);
|
guildManager.setSettingValue(GuildManager.GSettings.MUSIC_TOPIC, str);
|
||||||
enabled = "1".equals(str);
|
enabled = "1".equals(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +203,32 @@ public class ConfigureCommand extends AbstractCommand {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prefix
|
||||||
|
if (context.getArgument(0).equals("prefix")) {
|
||||||
|
String allowedPrefixes = "!$%?&*";
|
||||||
|
|
||||||
|
if (args.length == 1) {
|
||||||
|
String prefix = guildManager.getSetting(GuildManager.GSettings.PREFIX);
|
||||||
|
|
||||||
|
context.reply("Current prefix for commands: '%s'", prefix);
|
||||||
|
context.reply("Allowed prefixes: %s", Arrays.toString(allowedPrefixes.toCharArray()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length == 2) {
|
||||||
|
String prefix = args[1].trim();
|
||||||
|
|
||||||
|
if (prefix.length() == 1 && allowedPrefixes.contains(prefix)) {
|
||||||
|
guildManager.setSettingValue(GuildManager.GSettings.PREFIX, prefix);
|
||||||
|
|
||||||
|
context.reply("Prefix for commands is now '%s'", prefix);
|
||||||
|
} else {
|
||||||
|
context.reply("Allowed prefixes: %s", Arrays.toString(allowedPrefixes.toCharArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
context.reply(getUsage());
|
context.reply(getUsage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,14 @@ public class CommandManager {
|
|||||||
initAlias();
|
initAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isValidCommand(String commandString) {
|
||||||
|
return commands.containsKey(commandString) || commandsAlias.containsKey(commandString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AbstractCommand getCommand(String commandString) {
|
||||||
|
return commands.containsKey(commandString) ? commands.get(commandString) : commandsAlias.get(commandString);
|
||||||
|
}
|
||||||
|
|
||||||
private static void initCommands() {
|
private static void initCommands() {
|
||||||
logger.info("Loading commands..");
|
logger.info("Loading commands..");
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import netneutrality.handler.ratelimiter.MessageRateLimiterImpl;
|
|||||||
import netneutrality.thread.ThreadManager;
|
import netneutrality.thread.ThreadManager;
|
||||||
import netneutrality.utils.JpaUtils;
|
import netneutrality.utils.JpaUtils;
|
||||||
import netneutrality.utils.MessageFunction;
|
import netneutrality.utils.MessageFunction;
|
||||||
|
import netneutrality.utils.Settings;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -34,7 +35,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
|
|
||||||
public class GuildManager {
|
public class GuildManager {
|
||||||
|
|
||||||
public enum Settings {
|
public enum GSettings {
|
||||||
|
PREFIX("PREFIX", Settings.getInstance().get("BOT_PREFIX", "$")),
|
||||||
MUSIC_ROLE("MUSIC_ROLE", ""),
|
MUSIC_ROLE("MUSIC_ROLE", ""),
|
||||||
MUSIC_CHANNEL("MUSIC_CHANNEL", ""),
|
MUSIC_CHANNEL("MUSIC_CHANNEL", ""),
|
||||||
MUSIC_VOLUME("MUSIC_VOLUME", "30"),
|
MUSIC_VOLUME("MUSIC_VOLUME", "30"),
|
||||||
@@ -44,7 +46,7 @@ public class GuildManager {
|
|||||||
private final String name;
|
private final String name;
|
||||||
private final String defaultValue;
|
private final String defaultValue;
|
||||||
|
|
||||||
Settings(String name, String defaultValue) {
|
GSettings(String name, String defaultValue) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
@@ -140,7 +142,7 @@ public class GuildManager {
|
|||||||
return settingsCache;
|
return settingsCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSetting(Settings setting) {
|
public String getSetting(GSettings setting) {
|
||||||
String value = settingsCache.get(setting.getName());
|
String value = settingsCache.get(setting.getName());
|
||||||
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
@@ -150,7 +152,7 @@ public class GuildManager {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSettingValue(Settings setting, String value) {
|
public void setSettingValue(GSettings setting, String value) {
|
||||||
Session session = sessionFactory.openSession();
|
Session session = sessionFactory.openSession();
|
||||||
JpaUtils.tryLoad(session, guildEntity, guildEntity.getGuildId());
|
JpaUtils.tryLoad(session, guildEntity, guildEntity.getGuildId());
|
||||||
|
|
||||||
|
|||||||
@@ -22,15 +22,14 @@ public class MessageReceivedHandler extends ListenerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) {
|
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) {
|
||||||
|
if (event.getAuthor().isBot()) return;
|
||||||
|
if (event.isWebhookMessage()) return;
|
||||||
|
|
||||||
Message message = event.getMessage();
|
Message message = event.getMessage();
|
||||||
Member member = event.getMember();
|
Member member = event.getMember();
|
||||||
User user = event.getAuthor();
|
|
||||||
Guild guild = event.getGuild();
|
Guild guild = event.getGuild();
|
||||||
TextChannel channel = event.getChannel();
|
TextChannel channel = event.getChannel();
|
||||||
|
|
||||||
if (user.isBot()) return;
|
|
||||||
if (event.isWebhookMessage()) return;
|
|
||||||
|
|
||||||
Sentry.addBreadcrumb(Breadcrumb.info("New message from " + member.getEffectiveName()));
|
Sentry.addBreadcrumb(Breadcrumb.info("New message from " + member.getEffectiveName()));
|
||||||
|
|
||||||
CommandContext context = CommandContext.createContext(event);
|
CommandContext context = CommandContext.createContext(event);
|
||||||
@@ -38,17 +37,20 @@ public class MessageReceivedHandler extends ListenerAdapter {
|
|||||||
GuildManager.getManager(guild).getRateLimiter().check(member, message, channel);
|
GuildManager.getManager(guild).getRateLimiter().check(member, message, channel);
|
||||||
GuildManager.getManager(guild).checkAndLogMessage(message, member, channel);
|
GuildManager.getManager(guild).checkAndLogMessage(message, member, channel);
|
||||||
|
|
||||||
String msg = message.getContentDisplay().trim();
|
String rawMessage = message.getContentDisplay().trim();
|
||||||
|
//String prefix = Settings.getInstance().get("BOT_PREFIX", "$");
|
||||||
|
String prefix = GuildManager.getManager(guild).getSetting(GuildManager.GSettings.PREFIX);
|
||||||
|
|
||||||
if (msg.startsWith(Settings.getInstance().get("BOT_PREFIX", "$"))) {
|
if (rawMessage.startsWith(prefix)) {
|
||||||
msg = msg.substring(Settings.getInstance().get("BOT_PREFIX", "$").length()).trim();
|
rawMessage = rawMessage.replaceFirst(String.format("^[%s\\s]{1,}", prefix), "").trim();
|
||||||
String cmd = msg.split(" ", 2)[0];
|
// rawMessage = rawMessage.substring(prefix.length()).trim();
|
||||||
|
String commandString = rawMessage.split(" ", 2)[0];
|
||||||
|
|
||||||
|
|
||||||
if (CommandManager.commands.containsKey(cmd) || CommandManager.commandsAlias.containsKey(cmd)) {
|
if (CommandManager.isValidCommand(commandString)) {
|
||||||
AbstractCommand c = CommandManager.commands.containsKey(cmd) ? CommandManager.commands.get(cmd) : CommandManager.commandsAlias.get(cmd);
|
AbstractCommand command = CommandManager.getCommand(commandString);
|
||||||
|
|
||||||
context.dispatch(c);
|
context.dispatch(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class MusicManager {
|
|||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
this.player = playerManager.createPlayer();
|
this.player = playerManager.createPlayer();
|
||||||
this.scheduler = new TrackScheduler(this);
|
this.scheduler = new TrackScheduler(this);
|
||||||
this.baseVolume = Integer.parseInt(GuildManager.getManager(guild).getSetting(GuildManager.Settings.MUSIC_VOLUME));
|
this.baseVolume = Integer.parseInt(GuildManager.getManager(guild).getSetting(GuildManager.GSettings.MUSIC_VOLUME));
|
||||||
|
|
||||||
guild.getAudioManager().setSendingHandler(new AudioPlayerSendHandler(player));
|
guild.getAudioManager().setSendingHandler(new AudioPlayerSendHandler(player));
|
||||||
player.addListener(scheduler);
|
player.addListener(scheduler);
|
||||||
|
|||||||
@@ -30,20 +30,12 @@ public class MessageFunction {
|
|||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(MessageFunction.class);
|
private final static Logger logger = LoggerFactory.getLogger(MessageFunction.class);
|
||||||
|
|
||||||
public static void logMessageIfNeeded(Guild guild, TextChannel channel, User author, Message message) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static Member getStartingMention(Message message) {
|
public static Member getStartingMention(Message message) {
|
||||||
if (isStartingWithMention(message)) {
|
if (isStartingWithMention(message)) {
|
||||||
return message.getMentionedMembers().get(0);
|
return message.getMentionedMembers().get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isStartingWithMention(Message message) {
|
public static boolean isStartingWithMention(Message message) {
|
||||||
@@ -188,7 +180,7 @@ public class MessageFunction {
|
|||||||
message.setColor(Color.BLUE);
|
message.setColor(Color.BLUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
message.addField(String.format("%s%s %s", Settings.getInstance().get("BOT_PREFIX"), command.getCommandName(), availAlias), description, false);
|
message.addField(String.format("%s %s", command.getCommandName(), availAlias), description, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user