use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.
the class SaveCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
if (args.length == 0) {
MessageUtils.sendUsage(this, channel, sender, args);
return;
}
String name = MessageUtils.getMessage(args, 0);
if (name.length() > 20) {
MessageUtils.sendErrorMessage("Name can only be a maximum of 20 characters!", channel);
return;
}
if (!FlareBot.instance().getMusicManager().hasPlayer(channel.getGuild().getId())) {
MessageUtils.sendErrorMessage("Your playlist is empty!", channel);
return;
}
Queue<Track> playlist = FlareBot.instance().getMusicManager().getPlayer(guild.getGuildId()).getPlaylist();
Track currentlyPlaying = FlareBot.instance().getMusicManager().getPlayer(guild.getGuildId()).getPlayingTrack();
channel.sendTyping().complete();
List<String> tracks = playlist.stream().map(track -> track.getTrack().getIdentifier()).collect(Collectors.toList());
if (currentlyPlaying != null) {
tracks.add(currentlyPlaying.getTrack().getIdentifier());
}
if (tracks.isEmpty()) {
MessageUtils.sendErrorMessage("Your playlist is empty!", channel);
return;
}
FlareBot.instance().getManager().savePlaylist(this, channel, sender.getId(), this.getPermissions(channel).hasPermission(member, Permission.SAVE_OVERWRITE), name, tracks);
}
use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.
the class LockChatCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
if (!guild.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
MessageUtils.sendErrorMessage("I can't lock the chat due to lack of permissions! " + "I need the `Manage Roles` permission", channel);
return;
}
String reason = null;
long time = -1;
@Nonnull AtomicReference<TextChannel> tc = new AtomicReference<>(channel);
if (args.length >= 1) {
TextChannel tmp = ParseUtils.parseChannel(guild.getGuild(), args[0], false);
if (tmp != null)
tc.set(tmp);
if (tmp == null || args.length >= 2) {
Long l = GeneralUtils.parseTime(tmp == null ? args[0] : args[1]);
if (l == null) {
MessageUtils.sendErrorMessage("Invalid time format! Please use something like `1h10m`", channel);
return;
}
time = l;
}
if (tmp != null && time > 0)
reason = MessageUtils.getMessage(args, 2);
else if ((tmp == null && time > 0) || (tmp != null && time == -1))
reason = MessageUtils.getMessage(args, 1);
else
reason = MessageUtils.getMessage(args, 0);
if (reason.isEmpty())
reason = null;
}
PermissionOverride everyoneOvr = tc.get().getPermissionOverride(guild.getGuild().getPublicRole());
boolean locking = !everyoneOvr.getDenied().contains(Permission.MESSAGE_WRITE);
EnumSet<Permission> perm = EnumSet.of(Permission.MESSAGE_WRITE);
EnumSet<Permission> empty = EnumSet.noneOf(Permission.class);
tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().deny(locking ? perm : empty).clear(locking ? empty : perm).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
tc.get().putPermissionOverride(guild.getGuild().getSelfMember()).setPermissions(locking ? perm : empty, empty).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
if (tc.get().getIdLong() != channel.getIdLong())
channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription(tc.get().getAsMention() + " has been " + (locking ? "locked" : "unlocked") + "!").build()).queue();
if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription("The chat has been " + (locking ? "locked" : "unlocked") + " by a staff member" + (locking && time > 0 ? " for " + FormatUtils.formatTime(time, TimeUnit.MILLISECONDS, true, false) : "") + "!" + (reason != null ? "\nReason: " + reason : "")).build()).queue();
if (locking && time > 0) {
new FlareBotTask("ChannelUnlock-" + tc.get().getIdLong()) {
@Override
public void run() {
tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().clear(Permission.MESSAGE_WRITE).queue();
if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.GREEN).setDescription("The chat has been unlocked").build()).queue();
}
}.delay(time);
}
}
use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.
the class WarningsCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
if (args.length == 1) {
if (args[0].equalsIgnoreCase("stats")) {
Map.Entry<String, List<String>> highestEntry = Collections.max(guild.getWarningsMap().entrySet(), Comparator.comparingInt(entry -> entry.getValue().size()));
User mostWarned = GuildUtils.getUser(highestEntry.getKey(), guild.getGuildId(), true);
channel.sendMessage(new EmbedBuilder().setTitle("Warning stats", null).addField("Total Warnings", String.valueOf(guild.getWarningsMap().values().stream().mapToLong(List::size).sum()), true).addField("Users warned", String.valueOf(guild.getWarningsMap().size()), true).addField("Most warned user", MessageUtils.getTag(mostWarned) + " - " + highestEntry.getValue().size() + " warnings", true).setColor(Color.CYAN).build()).queue();
} else {
User user = GuildUtils.getUser(args[0]);
if (user == null) {
MessageUtils.sendErrorMessage("That user could not be found!!", channel);
return;
}
StringBuilder sb = new StringBuilder();
List<String> tmp = guild.getUserWarnings(user);
List<String> warnings = tmp.subList(Math.max(tmp.size() - 5, 0), tmp.size());
int i = 1;
for (String warning : warnings) {
sb.append(i).append(". ").append(warning.substring(0, Math.min(725, warning.length()))).append(warning.length() > 725 ? "..." : "").append("\n");
i++;
}
EmbedBuilder eb = new EmbedBuilder().setTitle("Warnings for " + user.getName()).addField("Warning count", String.valueOf(tmp.size()), true).addField("Last 5 warnings", "```md\n" + sb.toString().trim() + "\n```", false).setColor(Color.CYAN);
channel.sendMessage(eb.build()).queue();
}
} else {
MessageUtils.sendUsage(this, channel, sender, args);
}
}
use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.
the class SettingsCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
if (args.length >= 1) {
if (args[0].equalsIgnoreCase("list")) {
channel.sendMessage(MessageUtils.getEmbed(sender).setTitle("Guild Settings").setDescription(String.format("`delete-commands` - %b\n" + "`blacklisted-channels` - %s\n" + "`blaclisted-users` - %s", guild.getSettings().shouldDeleteCommands(), getBlacklistedChannels(guild), getBlacklistedUsers(guild))).build()).queue();
} else if (args[0].equalsIgnoreCase("change")) {
if (args.length >= 3) {
if (args[1].equalsIgnoreCase("delete-commands")) {
if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("yes") || args[2].equalsIgnoreCase("y")) {
guild.getSettings().setDeleteCommands(true);
MessageUtils.sendSuccessMessage("FlareBot will now delete the commands sent by users!", channel);
} else if (args[2].equalsIgnoreCase("false") || args[2].equalsIgnoreCase("no") || args[2].equalsIgnoreCase("n")) {
guild.getSettings().setDeleteCommands(false);
MessageUtils.sendWarningMessage("FlareBot will no longer delete the commands sent by users!", channel);
} else
MessageUtils.sendWarningMessage("Invalid value! Expecting `yes` or `no`", channel);
} else
MessageUtils.sendWarningMessage("Invalid option or invalid option which can be changed with " + "the `change` argument. Do `{%}usage " + getCommand() + "` and see if you can find " + "the argument required!", channel);
} else
MessageUtils.sendUsage(this, channel, sender, args);
} else if (args[0].equalsIgnoreCase("blacklist")) {
if (args.length >= 3) {
if (args[1].equalsIgnoreCase("list")) {
if (args[2].equalsIgnoreCase("channels") || args[2].equalsIgnoreCase("channel"))
MessageUtils.sendInfoMessage(getBlacklistedChannels(guild), channel);
else if (args[2].equalsIgnoreCase("users") || args[2].equalsIgnoreCase("user"))
MessageUtils.sendInfoMessage(getBlacklistedUsers(guild), channel);
else
MessageUtils.sendWarningMessage("Invalid blacklist item!", channel);
} else if (args[1].equalsIgnoreCase("add")) {
TextChannel tc = GuildUtils.getChannel(args[2], guild);
if (tc != null) {
guild.getSettings().getChannelBlacklist().add(tc.getIdLong());
MessageUtils.sendSuccessMessage("Added " + tc.getAsMention() + " to the blacklist!", channel);
return;
}
User user = GuildUtils.getUser(args[2], guild.getGuildId());
if (user != null) {
guild.getSettings().getUserBlacklist().add(user.getIdLong());
MessageUtils.sendSuccessMessage("Added " + user.getAsMention() + " to the blacklist!", channel);
return;
}
MessageUtils.sendWarningMessage("Invalid channel or user! Try the ID if you're sure the " + "entity is valid", channel);
} else if (args[1].equalsIgnoreCase("remove")) {
TextChannel tc = GuildUtils.getChannel(args[2], guild);
if (tc != null) {
if (!guild.getSettings().getChannelBlacklist().contains(tc.getIdLong())) {
MessageUtils.sendWarningMessage("That channel is not blacklisted!", channel);
return;
}
guild.getSettings().getChannelBlacklist().remove(tc.getIdLong());
MessageUtils.sendSuccessMessage("Removed " + tc.getAsMention() + " from the blacklist!", channel);
return;
}
User user = GuildUtils.getUser(args[2], guild.getGuildId());
if (user != null) {
if (!guild.getSettings().getUserBlacklist().contains(user.getIdLong())) {
MessageUtils.sendWarningMessage("That user is not blacklisted!", channel);
return;
}
guild.getSettings().getUserBlacklist().remove(user.getIdLong());
MessageUtils.sendSuccessMessage("Removed " + user.getAsMention() + " from the blacklist!", channel);
return;
}
MessageUtils.sendWarningMessage("Invalid channel or user! Try the ID if you're sure the " + "entity is valid", channel);
} else
MessageUtils.sendUsage(this, channel, sender, args);
} else
MessageUtils.sendUsage(this, channel, sender, args);
} else
MessageUtils.sendUsage(this, channel, sender, args);
} else
MessageUtils.sendUsage(this, channel, sender, args);
}
use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.
the class GuildUtils method getRole.
/**
* Gets a {@link Role} that matches a string. Case doesn't matter.
*
* @param s The String to get a role from
* @param guildId The id of the {@link Guild} to get the role from
* @param channel The channel to send an error message to if anything goes wrong.
* @return null if the role doesn't, otherwise a list of roles matching the string
*/
public static Role getRole(String s, String guildId, TextChannel channel) {
Guild guild = Getters.getGuildById(guildId);
Role role = guild.getRoles().stream().filter(r -> r.getName().equalsIgnoreCase(s)).findFirst().orElse(null);
if (role != null)
return role;
try {
role = guild.getRoleById(Long.parseLong(s.replaceAll("[^0-9]", "")));
if (role != null)
return role;
} catch (NumberFormatException | NullPointerException ignored) {
}
if (channel != null) {
if (guild.getRolesByName(s, true).isEmpty()) {
String closest = null;
int distance = LEVENSHTEIN_DISTANCE;
for (Role role1 : guild.getRoles().stream().filter(role1 -> FlareBotManager.instance().getGuild(guildId).getSelfAssignRoles().contains(role1.getId())).collect(Collectors.toList())) {
int currentDistance = StringUtils.getLevenshteinDistance(role1.getName(), s);
if (currentDistance < distance) {
distance = currentDistance;
closest = role1.getName();
}
}
MessageUtils.sendErrorMessage("That role does not exist! " + (closest != null ? "Maybe you mean `" + closest + "`" : ""), channel);
return null;
} else {
return guild.getRolesByName(s, true).get(0);
}
}
return null;
}
Aggregations