Created
September 12, 2018 16:56
-
-
Save karelmikie3/e352deeca9e54b737b1cb75f11370035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.karelmikie3.wendybot.command; | |
import com.karelmikie3.wendybot.command.api.ICommand; | |
import com.karelmikie3.wendybot.command.api.IGeneralCommand; | |
import net.dv8tion.jda.core.EmbedBuilder; | |
import net.dv8tion.jda.core.JDA; | |
import net.dv8tion.jda.core.entities.Message; | |
import net.dv8tion.jda.core.entities.TextChannel; | |
import net.dv8tion.jda.core.entities.User; | |
import net.dv8tion.jda.core.requests.restaction.MessageAction; | |
import java.awt.*; | |
import java.util.*; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import static net.dv8tion.jda.core.Permission.ADMINISTRATOR; | |
public class CommandImageFromChannel implements IGeneralCommand { | |
private final List<String> imageLinks = new ArrayList<>(); | |
private final Random random = new Random(); | |
private final long id; | |
public CommandImageFromChannel(long id) { | |
this.id = id; | |
} | |
@Override | |
public MessageAction execute(TextChannel textChannel, User user, Message message) { | |
TextChannel channel = textChannel.getJDA().getTextChannelById(id); | |
if (message.getContentDisplay().contains("reload") && | |
channel.getGuild().getMember(user).hasPermission(ADMINISTRATOR)) { | |
getAllImages(channel); | |
return textChannel.sendMessage("Reloaded all images"); | |
} else { | |
EmbedBuilder builder = new EmbedBuilder(); | |
builder.setColor(new Color(54, 57, 63)); | |
builder.setImage(imageLinks.get(random.nextInt(imageLinks.size()))); | |
return textChannel.sendMessage(builder.build()); | |
} | |
} | |
private void getAllImages(TextChannel channel) { | |
imageLinks.addAll(channel.getIterableHistory().parallelStream() | |
.map(Message::getAttachments) | |
.flatMap(Collection::parallelStream) | |
.filter(Message.Attachment::isImage) | |
.map(Message.Attachment::getProxyUrl) | |
.collect(Collectors.toList())); | |
} | |
@Override | |
public void postInit(Map<String, ICommand> commands, JDA jda) { | |
getAllImages(jda.getTextChannelById(id)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment