diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index 69248fc88..fb1953664 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -127,7 +127,8 @@ private static void startBot() new SkiptoCmd(bot), new StopCmd(bot), new VolumeCmd(bot), - + + new BlacklistUserCmd(bot), new PrefixCmd(bot), new SetdjCmd(bot), new SkipratioCmd(bot), diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/DJCommand.java b/src/main/java/com/jagrosh/jmusicbot/commands/DJCommand.java index b0f573667..a5d9b78fe 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/DJCommand.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/DJCommand.java @@ -35,13 +35,20 @@ public DJCommand(Bot bot) public static boolean checkDJPermission(CommandEvent event) { + Settings settings = event.getClient().getSettingsFor(event.getGuild()); + String authorId = event.getAuthor().getId(); + boolean authorCannotUseCommands = settings.getBlacklistedUsers().contains(authorId); + if (authorCannotUseCommands) { + event.replyError(event.getAuthor().getAsTag() + " cannot use DJ commands!"); + return false; + } + if(event.getAuthor().getId().equals(event.getClient().getOwnerId())) return true; if(event.getGuild()==null) return true; if(event.getMember().hasPermission(Permission.MANAGE_SERVER)) return true; - Settings settings = event.getClient().getSettingsFor(event.getGuild()); Role dj = settings.getRole(event.getGuild()); return dj!=null && (event.getMember().getRoles().contains(dj) || dj.getIdLong()==event.getGuild().getIdLong()); } diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/MusicCommand.java b/src/main/java/com/jagrosh/jmusicbot/commands/MusicCommand.java index 9740a7af7..96e0984bd 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/MusicCommand.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/MusicCommand.java @@ -22,6 +22,7 @@ import com.jagrosh.jmusicbot.audio.AudioHandler; import net.dv8tion.jda.api.entities.GuildVoiceState; import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.VoiceChannel; import net.dv8tion.jda.api.exceptions.PermissionException; @@ -43,9 +44,17 @@ public MusicCommand(Bot bot) } @Override - protected void execute(CommandEvent event) + protected void execute(CommandEvent event) { Settings settings = event.getClient().getSettingsFor(event.getGuild()); + + String authorId = event.getAuthor().getId(); + boolean authorCannotUseCommands = settings.getBlacklistedUsers().contains(authorId); + if (authorCannotUseCommands) { + event.replyError(event.getAuthor().getAsTag() + " cannot use Music commands!"); + return; + } + TextChannel tchannel = settings.getTextChannel(event.getGuild()); if(tchannel!=null && !event.getTextChannel().equals(tchannel)) { diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/admin/BlacklistUserCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/admin/BlacklistUserCmd.java new file mode 100644 index 000000000..0e6812d24 --- /dev/null +++ b/src/main/java/com/jagrosh/jmusicbot/commands/admin/BlacklistUserCmd.java @@ -0,0 +1,104 @@ +/* + * Copyright 2018 John Grosh . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jagrosh.jmusicbot.commands.admin; + +import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.commons.utils.FinderUtil; +import com.jagrosh.jdautilities.menu.OrderedMenu; +import com.jagrosh.jmusicbot.Bot; +import com.jagrosh.jmusicbot.commands.AdminCommand; +import com.jagrosh.jmusicbot.settings.Settings; +import com.jagrosh.jmusicbot.utils.FormatUtil; +import com.sun.org.apache.xpath.internal.operations.String; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; + +import java.text.Format; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author John Grosh + */ +public class BlacklistUserCmd extends AdminCommand +{ + public BlacklistUserCmd(Bot bot) + { + this.name = "blacklist"; + this.help = "allow/disallow user from issuing commands"; + this.arguments = ""; + this.aliases = bot.getConfig().getAliases(this.name); + } + + @Override + protected void execute(CommandEvent event) + { + if(event.getArgs().isEmpty()) + { + event.replyError("You need to mention a user!"); + return; + } + Settings s = event.getClient().getSettingsFor(event.getGuild()); + if(event.getArgs().equalsIgnoreCase("none")) + { + s.clearBlacklistedUsers(); + event.replySuccess("Blacklist cleared; All users can use commands."); + return; + } + + User target; + List found = FinderUtil.findMembers(event.getArgs(), event.getGuild()); + if(found.isEmpty()) + { + event.replyError("Unable to find the user!"); + return; + } + else if(found.size()>1) + { + StringBuilder builder = new StringBuilder(); + for(int i=0; i blacklistedUsers; - public Settings(SettingsManager manager, String textId, String voiceId, String roleId, int volume, String defaultPlaylist, RepeatMode repeatMode, String prefix, double skipRatio) + public Settings(SettingsManager manager, String textId, String voiceId, String roleId, int volume, String defaultPlaylist, RepeatMode repeatMode, String prefix, double skipRatio, List blacklistedUsers) { this.manager = manager; try @@ -71,9 +76,10 @@ public Settings(SettingsManager manager, String textId, String voiceId, String r this.repeatMode = repeatMode; this.prefix = prefix; this.skipRatio = skipRatio; + this.blacklistedUsers = blacklistedUsers; } - public Settings(SettingsManager manager, long textId, long voiceId, long roleId, int volume, String defaultPlaylist, RepeatMode repeatMode, String prefix, double skipRatio) + public Settings(SettingsManager manager, long textId, long voiceId, long roleId, int volume, String defaultPlaylist, RepeatMode repeatMode, String prefix, double skipRatio, List blacklistedUsers) { this.manager = manager; this.textId = textId; @@ -84,8 +90,9 @@ public Settings(SettingsManager manager, long textId, long voiceId, long roleId, this.repeatMode = repeatMode; this.prefix = prefix; this.skipRatio = skipRatio; + this.blacklistedUsers = blacklistedUsers; } - + // Getters public TextChannel getTextChannel(Guild guild) { @@ -132,7 +139,12 @@ public Collection getPrefixes() { return prefix == null ? Collections.emptySet() : Collections.singleton(prefix); } - + + public List getBlacklistedUsers() + { + return blacklistedUsers; + } + // Setters public void setTextChannel(TextChannel tc) { @@ -181,4 +193,19 @@ public void setSkipRatio(double skipRatio) this.skipRatio = skipRatio; this.manager.writeSettings(); } + + public void setBlacklistedUser(String user) { + this.blacklistedUsers.add(user); + this.manager.writeSettings(); + } + + public void removeBlacklistedUser(String user) { + this.blacklistedUsers.remove(user); + this.manager.writeSettings(); + } + + public void clearBlacklistedUsers() { + this.blacklistedUsers = new ArrayList(0); + this.manager.writeSettings(); + } } diff --git a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java index 4a70a0fba..858394c88 100644 --- a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java +++ b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java @@ -19,8 +19,12 @@ import com.jagrosh.jmusicbot.utils.OtherUtil; import java.io.IOException; import java.nio.file.Files; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; + import net.dv8tion.jda.api.entities.Guild; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.LoggerFactory; @@ -46,16 +50,18 @@ public SettingsManager() if (!o.has("repeat_mode") && o.has("repeat") && o.getBoolean("repeat")) o.put("repeat_mode", RepeatMode.ALL); - + List blacklistedUsers = convertJSONArrayToStringList(o.getJSONArray("blacklisted_users")); settings.put(Long.parseLong(id), new Settings(this, - o.has("text_channel_id") ? o.getString("text_channel_id") : null, - o.has("voice_channel_id")? o.getString("voice_channel_id") : null, - o.has("dj_role_id") ? o.getString("dj_role_id") : null, - o.has("volume") ? o.getInt("volume") : 100, - o.has("default_playlist")? o.getString("default_playlist") : null, - o.has("repeat_mode") ? o.getEnum(RepeatMode.class, "repeat_mode"): RepeatMode.OFF, - o.has("prefix") ? o.getString("prefix") : null, - o.has("skip_ratio") ? o.getDouble("skip_ratio") : SKIP_RATIO)); + o.has("text_channel_id") ? o.getString("text_channel_id") : null, + o.has("voice_channel_id") ? o.getString("voice_channel_id") : null, + o.has("dj_role_id") ? o.getString("dj_role_id") : null, + o.has("volume") ? o.getInt("volume") : 100, + o.has("default_playlist") ? o.getString("default_playlist") : null, + o.has("repeat_mode") ? o.getEnum(RepeatMode.class, "repeat_mode"): RepeatMode.OFF, + o.has("prefix") ? o.getString("prefix") : null, + o.has("skip_ratio") ? o.getDouble("skip_ratio") : SKIP_RATIO, + o.has("blacklisted_users") ? blacklistedUsers : new ArrayList(0)) + ); }); } catch(IOException | JSONException e) { LoggerFactory.getLogger("Settings").warn("Failed to load server settings (this is normal if no settings have been set yet): "+e); @@ -81,7 +87,7 @@ public Settings getSettings(long guildId) private Settings createDefaultSettings() { - return new Settings(this, 0, 0, 0, 100, null, RepeatMode.OFF, null, SKIP_RATIO); + return new Settings(this, 0, 0, 0, 100, null, RepeatMode.OFF, null, SKIP_RATIO, new ArrayList(0)); } protected void writeSettings() @@ -106,6 +112,7 @@ protected void writeSettings() o.put("prefix", s.getPrefix()); if(s.getSkipRatio() != SKIP_RATIO) o.put("skip_ratio", s.getSkipRatio()); + o.put("blacklisted_users", s.getBlacklistedUsers()); obj.put(Long.toString(key), o); }); try { @@ -114,4 +121,12 @@ protected void writeSettings() LoggerFactory.getLogger("Settings").warn("Failed to write to file: "+ex); } } + + private List convertJSONArrayToStringList(JSONArray arr) { + List list = new ArrayList(); + for (int i=0; i