1
0
mirror of https://github.com/demodude4u/Factorio-FBSR.git synced 2024-11-27 08:20:57 +02:00

Added custom request for reddit things

This commit is contained in:
Weston Rye (Demod) 2017-06-27 16:22:17 -04:00
parent 22e866ffc5
commit e25d544937
2 changed files with 56 additions and 0 deletions

View File

@ -255,6 +255,33 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
sendReport(event, reporting);
}
private void handleRedditCheckThingsCommand(MessageReceivedEvent event) {
String content = event.getMessage().getContent();
TaskReporting reporting = new TaskReporting();
reporting.setContext(content);
try {
String[] args = content.split("\\s");
if (args.length < 2) {
event.getChannel().sendMessage("You didn't specify anything!").complete();
return;
}
String[] ids = Arrays.asList(args).stream().skip(1).toArray(String[]::new);
ServiceFinder.findService(BlueprintBotRedditService.class).ifPresent(s -> {
try {
s.processRequest(ids);
reporting.addInfo("Request successful!");
} catch (Exception e) {
reporting.addException(e);
}
});
} catch (Exception e) {
reporting.addException(e);
}
sendReport(event, reporting);
}
private void processBlueprints(List<BlueprintStringData> blueprintStrings, MessageReceivedEvent event,
TaskReporting reporting) {
for (BlueprintStringData blueprintString : blueprintStrings) {
@ -482,6 +509,8 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
.addCommand("dataRaw", createDataRawCommandHandler(table::getRaw))//
.withHelp("Provides a dump of lua from `data.raw` for the specified key.")//
//
.addCommand("redditCheckThings", event -> handleRedditCheckThingsCommand(event))
//
.create();
bot.startAsync().awaitRunning();

View File

@ -8,6 +8,7 @@ import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@ -46,6 +47,7 @@ import net.dean.jraw.models.CommentNode;
import net.dean.jraw.models.Listing;
import net.dean.jraw.models.Message;
import net.dean.jraw.models.Submission;
import net.dean.jraw.models.Thing;
import net.dean.jraw.paginators.CommentStream;
import net.dean.jraw.paginators.InboxPaginator;
import net.dean.jraw.paginators.Sorting;
@ -392,6 +394,31 @@ public class BlueprintBotRedditService extends AbstractScheduledService {
}
}
public void processRequest(String... ids) throws NetworkException, ApiException {
System.out.println("REQUESTED: " + Arrays.toString(ids));
Listing<Thing> listing = reddit.get(ids);
System.out.println("REQUESTED RESULT = " + listing.size());
for (Thing thing : listing) {
if (thing instanceof Comment) {
System.out.println("REQUESTED COMMENT!");
Comment comment = (Comment) thing;
Optional<String> response = processContent(comment.getBody(), getPermaLink(comment),
comment.getSubredditName(), comment.getAuthor());
if (response.isPresent()) {
account.reply(comment, response.get());
}
} else if (thing instanceof Submission) {
System.out.println("REQUESTED SUBMISSION!");
Submission submission = (Submission) thing;
Optional<String> response = processContent(submission.getSelftext(), submission.getUrl(),
submission.getSubredditName(), submission.getAuthor());
if (response.isPresent()) {
account.reply(submission, response.get());
}
}
}
}
@Override
protected void runOneIteration() throws Exception {
try {