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) {
public ClearCommand(String command, RixaPermission rixaPermission, String description,
List<String> aliases) {
super(command, rixaPermission, description, aliases); super(command, rixaPermission, description, aliases);
this.rixaPermission = rixaPermission; this.rixaPermission = rixaPermission;
} }
@Override @Override
public void execute(String commandLabel, Guild guild, Member member, TextChannel channel, String[] args) { public void execute(String commandLabel, Guild guild, Member member, TextChannel channel,
String[] args) {
RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild); RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild);
if (args == null || args.length == 0 || !Utils.isInteger(args[0])) { if (args == null || args.length == 0 || !Utils.isInteger(args[0])) {
MessageFactory.create(String.format("Incorrect Usage! Example: `%s%s 10`", rixaGuild.getSettings().getPrefix(), MessageFactory.create(
String.format("Incorrect Usage! Example: `%s%s 10`", rixaGuild.getSettings().getPrefix(),
commandLabel)).setColor(member.getColor()).queue(channel); commandLabel)).setColor(member.getColor()).queue(channel);
return; return;
} }
int amount = Integer.parseInt(args[0]); int amount = Integer.parseInt(args[0]);
if (amount < 1 || amount > 100) { 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); MessageFactory.create("Please try a number less than 100 and greater than 1 and :grimacing:")
.setColor(member.getColor()).queue(channel);
return; return;
} }
int i = deleteMessages(channel, amount); deleteMessages(channel, amount, member);
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) { private void deleteMessages(TextChannel channel, int amount, Member member) {
List<Message> messageHistory= channel.getHistory().retrievePast(amount).complete(); channel.getHistory().retrievePast(amount).queue(success -> {
List<Message> pinnedMessages = channel.getPinnedMessages().complete(); List<Message> pinnedMessages = channel.getPinnedMessages().complete();
int i = 0; List<Message> newMessages = success.stream()
List<Message> newMessages = new ArrayList<>(messageHistory.stream().filter(message -> !pinnedMessages.contains(message)).collect(Collectors.toList())); .filter(message -> !pinnedMessages.contains(message) && canDelete(message))
/*messageHistory.forEach(message -> { .collect(Collectors.toList());
if (!(pinnedMessages.contains(message))) {
newMessages.add(message);
}
});*/
// !mute Savvy 10s Hello Savvy!
try { try {
i = newMessages.size(); channel.deleteMessages(newMessages).queue(onSuccess -> {
channel.deleteMessages(newMessages).queue(); 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) { } catch (PermissionException ex) {
if (ex.getPermission() == Permission.MESSAGE_MANAGE) if (ex.getPermission() == Permission.MESSAGE_MANAGE) {
MessageFactory.create("I do not have permission to clear messages within this channel!").queue(channel); MessageFactory.create("I do not have permission to clear messages within this channel!")
} catch (IllegalArgumentException ignored) { .queue(channel);
} }
return i; } 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();