Fixed ClearCommand and Conversation API

This commit is contained in:
Savvy 2018-01-30 02:37:14 -05:00
parent 7f39801cb5
commit cfe7ec22e2
2 changed files with 69 additions and 53 deletions

View File

@ -1,73 +1,88 @@
package io.rixa.bot.commands.cmds.moderator; package io.rixa.bot.commands.cmds.moderator;
import io.rixa.bot.Rixa;
import io.rixa.bot.commands.Command; import io.rixa.bot.commands.Command;
import io.rixa.bot.commands.perms.RixaPermission; import io.rixa.bot.commands.perms.RixaPermission;
import io.rixa.bot.guild.RixaGuild; import io.rixa.bot.guild.RixaGuild;
import io.rixa.bot.guild.manager.GuildManager; import io.rixa.bot.guild.manager.GuildManager;
import io.rixa.bot.user.RixaUser;
import io.rixa.bot.utils.MessageFactory; import io.rixa.bot.utils.MessageFactory;
import io.rixa.bot.utils.Utils; import io.rixa.bot.utils.Utils;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter; import lombok.Getter;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.exceptions.ErrorResponseException;
import net.dv8tion.jda.core.exceptions.PermissionException; import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.requests.ErrorResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ClearCommand extends Command { public class ClearCommand extends Command {
@Getter @Getter
private RixaPermission rixaPermission; private RixaPermission rixaPermission;
public ClearCommand(String command, RixaPermission rixaPermission, String description, List<String> aliases) {
super(command, rixaPermission, description, aliases);
this.rixaPermission = rixaPermission;
}
@Override public ClearCommand(String command, RixaPermission rixaPermission, String description,
public void execute(String commandLabel, Guild guild, Member member, TextChannel channel, String[] args) { List<String> aliases) {
RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild); super(command, rixaPermission, description, aliases);
if (args == null || args.length == 0 || !Utils.isInteger(args[0])) { this.rixaPermission = rixaPermission;
MessageFactory.create(String.format("Incorrect Usage! Example: `%s%s 10`", rixaGuild.getSettings().getPrefix(), }
commandLabel)).setColor(member.getColor()).queue(channel);
return;
}
int amount = Integer.parseInt(args[0]);
if (amount < 1 || amount > 100) {
MessageFactory.create("Please try a number less than 100 and greater than 1 and :grimacing:").setColor(member.getColor()).queue(channel);
return;
}
int i = deleteMessages(channel, amount);
MessageFactory.create(((i == 0) ? "Could not find any messages to delete" : "Successfully deleted *" + i +
"* messages in " + channel.getAsMention()))
.footer("Requested by: " + member.getEffectiveName(), member.getUser().getEffectiveAvatarUrl())
.setColor(member.getColor())
.queue(channel);
}
private int deleteMessages(TextChannel channel, int amount) { @Override
List<Message> messageHistory= channel.getHistory().retrievePast(amount).complete(); public void execute(String commandLabel, Guild guild, Member member, TextChannel channel,
List<Message> pinnedMessages = channel.getPinnedMessages().complete(); String[] args) {
int i = 0; RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild);
List<Message> newMessages = new ArrayList<>(messageHistory.stream().filter(message -> !pinnedMessages.contains(message)).collect(Collectors.toList())); if (args == null || args.length == 0 || !Utils.isInteger(args[0])) {
/*messageHistory.forEach(message -> { MessageFactory.create(
if (!(pinnedMessages.contains(message))) { String.format("Incorrect Usage! Example: `%s%s 10`", rixaGuild.getSettings().getPrefix(),
newMessages.add(message); commandLabel)).setColor(member.getColor()).queue(channel);
} return;
});*/
// !mute Savvy 10s Hello Savvy!
try {
i = newMessages.size();
channel.deleteMessages(newMessages).queue();
} catch (PermissionException ex) {
if (ex.getPermission() == Permission.MESSAGE_MANAGE)
MessageFactory.create("I do not have permission to clear messages within this channel!").queue(channel);
} catch (IllegalArgumentException ignored) {
}
return i;
} }
int amount = Integer.parseInt(args[0]);
if (amount < 1 || amount > 100) {
MessageFactory.create("Please try a number less than 100 and greater than 1 and :grimacing:")
.setColor(member.getColor()).queue(channel);
return;
}
deleteMessages(channel, amount, member);
}
private void deleteMessages(TextChannel channel, int amount, Member member) {
channel.getHistory().retrievePast(amount).queue(success -> {
List<Message> pinnedMessages = channel.getPinnedMessages().complete();
List<Message> newMessages = success.stream()
.filter(message -> !pinnedMessages.contains(message) && canDelete(message))
.collect(Collectors.toList());
try {
channel.deleteMessages(newMessages).queue(onSuccess -> {
MessageFactory.create(
((newMessages.isEmpty()) ? "Could not find any messages to delete"
: "Successfully deleted *" + newMessages.size() + "* messages in " + channel.getAsMention()))
.footer("Requested by: " + member.getEffectiveName(),
member.getUser().getEffectiveAvatarUrl())
.setColor(member.getColor())
.queue(channel);
});
} catch (PermissionException ex) {
if (ex.getPermission() == Permission.MESSAGE_MANAGE) {
MessageFactory.create("I do not have permission to clear messages within this channel!")
.queue(channel);
}
} catch (ErrorResponseException ex) {
if (ex.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
Rixa.getInstance().getLogger().info("Ignored message during clear.");
}
}
}, failure -> MessageFactory
.create("I could not retrieve message history! Please forgive me for this. :(")
.setColor(member.getColor()).queue(channel));
}
private boolean canDelete(Message message) {
return message.getCreationTime().isAfter(OffsetDateTime.now().minusWeeks(2)
.plusMinutes(2)); // Pulled from Fredboat
}
} }

View File

@ -26,9 +26,10 @@ public class MessageListener {
} }
String message = event.getMessage().getContentRaw().trim(); String message = event.getMessage().getContentRaw().trim();
RixaGuild rixaGuild = GuildManager.getInstance().getGuild(event.getGuild()); RixaGuild rixaGuild = GuildManager.getInstance().getGuild(event.getGuild());
if (message.startsWith("@" + event.getGuild().getSelfMember().getEffectiveName())) { if (event.getMessage().getContentDisplay().startsWith("@" + event.getGuild().getSelfMember().getEffectiveName())) {
String chat = event.getMessage().getContentDisplay();
chatter(rixaGuild, event.getChannel(), chatter(rixaGuild, event.getChannel(),
message.replace("@" + event.getGuild().getSelfMember().getEffectiveName(), "")); chat.replace("@" + event.getGuild().getSelfMember().getEffectiveName(), ""));
return; return;
} }
String prefix = rixaGuild.getSettings().getPrefix(); String prefix = rixaGuild.getSettings().getPrefix();