Skip to content

Commit 6b22076

Browse files
Add the /reddit command
1 parent 7dfe0bd commit 6b22076

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

src/main/java/technobot/commands/CommandRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public CommandRegistry(TechnoBot bot) {
5050
new NsfwCommand(bot),
5151
new EightBallCommand(bot),
5252
new GoogleCommand(bot),
53+
new RedditCommand(bot),
5354

5455
//Leveling commands
5556
new RankCommand(bot),
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package technobot.commands.fun;
2+
3+
import net.dv8tion.jda.api.EmbedBuilder;
4+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
5+
import net.dv8tion.jda.api.interactions.commands.OptionType;
6+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
7+
import okhttp3.Call;
8+
import okhttp3.Callback;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
import technobot.TechnoBot;
12+
import technobot.commands.Category;
13+
import technobot.commands.Command;
14+
import technobot.data.json.Post;
15+
import technobot.util.embeds.EmbedColor;
16+
import technobot.util.embeds.EmbedUtils;
17+
18+
import java.io.IOException;
19+
20+
/**
21+
* Command that generates a post from various subreddits.
22+
*
23+
* @author TechnoVision
24+
*/
25+
public class RedditCommand extends Command {
26+
27+
public RedditCommand(TechnoBot bot) {
28+
super(bot);
29+
this.name = "reddit";
30+
this.description = "Get a reddit post from various subreddits.";
31+
this.category = Category.FUN;
32+
this.args.add(new OptionData(OptionType.STRING, "category", "The subreddit to browse", true)
33+
.addChoice("facepalm", "facepalm")
34+
.addChoice("comic", "comics")
35+
.addChoice("blacktwitter", "BlackPeopleTwitter")
36+
.addChoice("foodporn", "foodporn"));
37+
}
38+
39+
@Override
40+
public void execute(SlashCommandInteractionEvent event) {
41+
event.deferReply().queue();
42+
String category = event.getOption("category").getAsString();
43+
String url = "https://meme-api.herokuapp.com/gimme/" + category;
44+
45+
// Asynchronous API call
46+
Request request = new Request.Builder().url(url).build();
47+
bot.httpClient.newCall(request).enqueue(new Callback() {
48+
@Override
49+
public void onFailure(Call call, IOException e) {
50+
String text = "I was unable to fetch any posts from that subreddit!";
51+
event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue();
52+
}
53+
54+
@Override
55+
public void onResponse(Call call, final Response response) throws IOException {
56+
if (!response.isSuccessful()) throw new IOException();
57+
Post entity = bot.gson.fromJson(response.body().string(), Post.class);
58+
59+
EmbedBuilder embed = new EmbedBuilder()
60+
.setColor(EmbedColor.DEFAULT.color)
61+
.setTitle(entity.getTitle(), entity.getPostLink())
62+
.setImage(entity.getImageUrl())
63+
.setFooter(""+entity.getUpvotes(), Post.UPVOTE_EMOJI);
64+
event.getHook().sendMessageEmbeds(embed.build()).queue();
65+
}
66+
});
67+
}
68+
}

0 commit comments

Comments
 (0)