Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blacklist user from using music and DJ commands #1367

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/commands/DJCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/commands/MusicCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2018 John Grosh <[email protected]>.
*
* 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 <[email protected]>
*/
public class BlacklistUserCmd extends AdminCommand
{
public BlacklistUserCmd(Bot bot)
{
this.name = "blacklist";
this.help = "allow/disallow user from issuing commands";
this.arguments = "<user>";
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<Member> 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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with StringBuilder instead of OrderedMenu.Builder to display duplicates because it seems like any user would be able to interact with the emoji options and blacklist the incorrect user, which is not the intended use case. If OrderedMenu.Builder checks that the author is the one choosing the option I think it'd be fine to switch back to it though

for(int i=0; i<found.size() && i<4; i++)
{
Member member = found.get(i);
builder.append("\n**"+member.getUser().getName()+"**#"+member.getUser().getDiscriminator());
}
event.replyWarning("Found multiple users: " + builder.toString());
return;
}
else
{
target = found.get(0).getUser();
}
handleBlacklistUser(target, event);
}

private void handleBlacklistUser(User target, CommandEvent event) {
Settings s = event.getClient().getSettingsFor(event.getGuild());
if (s.getBlacklistedUsers().contains(target.getId()))
{
s.removeBlacklistedUser(target.getId());
event.replySuccess("User "+ event.getArgs() + " can now use commands");
} else
{
s.setBlacklistedUser(target.getId());
event.replySuccess("User "+ event.getArgs() + " can no longer use commands");
}
}
}
35 changes: 31 additions & 4 deletions src/main/java/com/jagrosh/jmusicbot/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package com.jagrosh.jmusicbot.settings;

import com.jagrosh.jdautilities.command.GuildSettingsProvider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
Expand All @@ -38,8 +42,9 @@ public class Settings implements GuildSettingsProvider
private RepeatMode repeatMode;
private String prefix;
private double skipRatio;
private List<String> 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<String> blacklistedUsers)
{
this.manager = manager;
try
Expand Down Expand Up @@ -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<String> blacklistedUsers)
{
this.manager = manager;
this.textId = textId;
Expand All @@ -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)
{
Expand Down Expand Up @@ -132,7 +139,12 @@ public Collection<String> getPrefixes()
{
return prefix == null ? Collections.emptySet() : Collections.singleton(prefix);
}


public List<String> getBlacklistedUsers()
{
return blacklistedUsers;
}

// Setters
public void setTextChannel(TextChannel tc)
{
Expand Down Expand Up @@ -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<String>(0);
this.manager.writeSettings();
}
}
35 changes: 25 additions & 10 deletions src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,16 +50,18 @@ public SettingsManager()
if (!o.has("repeat_mode") && o.has("repeat") && o.getBoolean("repeat"))
o.put("repeat_mode", RepeatMode.ALL);


List<String> 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<String>(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);
Expand All @@ -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<String>(0));
}

protected void writeSettings()
Expand All @@ -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 {
Expand All @@ -114,4 +121,12 @@ protected void writeSettings()
LoggerFactory.getLogger("Settings").warn("Failed to write to file: "+ex);
}
}

private List<String> convertJSONArrayToStringList(JSONArray arr) {
List<String> list = new ArrayList<String>();
for (int i=0; i<arr.length(); i++) {
list.add(arr.getString(i));
}
return list;
}
}