-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from beanbeanjuice/integration
Ready for Release v2.4.1
- Loading branch information
Showing
10 changed files
with
240 additions
and
43 deletions.
There are no files selected for viewing
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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/beanbeanjuice/command/fun/CoffeeMemeCommand.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.beanbeanjuice.command.fun; | ||
|
||
import com.beanbeanjuice.main.CafeBot; | ||
import com.beanbeanjuice.utility.command.CommandContext; | ||
import com.beanbeanjuice.utility.command.ICommand; | ||
import com.beanbeanjuice.utility.command.usage.Usage; | ||
import com.beanbeanjuice.utility.command.usage.categories.CategoryType; | ||
import com.beanbeanjuice.utility.helper.RedditAPI; | ||
import net.dv8tion.jda.api.entities.User; | ||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* An {@link ICommand} used to get coffee memes from the {@link com.beanbeanjuice.utility.helper.RedditAPI RedditAPI}. | ||
* | ||
* @author beanbeanjuice | ||
*/ | ||
public class CoffeeMemeCommand implements ICommand { | ||
|
||
@Override | ||
public void handle(CommandContext ctx, ArrayList<String> args, User user, GuildMessageReceivedEvent event) { | ||
event.getChannel().sendMessage(new RedditAPI().getRedditEmbed(getSubreddits().get(CafeBot.getGeneralHelper().getRandomNumber(0, getSubreddits().size())))).queue(); | ||
} | ||
|
||
@NotNull | ||
private ArrayList<String> getSubreddits() { | ||
ArrayList<String> arrayList = new ArrayList<>(); | ||
arrayList.add("CoffeePorn"); | ||
arrayList.add("coffeememes"); | ||
arrayList.add("coffeewithaview"); | ||
return arrayList; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "coffee-meme"; | ||
} | ||
|
||
@Override | ||
public ArrayList<String> getAliases() { | ||
ArrayList<String> arrayList = new ArrayList<>(); | ||
arrayList.add("coffeememe"); | ||
arrayList.add("coffee"); | ||
arrayList.add("coffee-memes"); | ||
arrayList.add("coffeememes"); | ||
arrayList.add("cafe-meme"); | ||
arrayList.add("cafememe"); | ||
return arrayList; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Get a coffee meme!"; | ||
} | ||
|
||
@Override | ||
public String exampleUsage() { | ||
return "`!!coffee-meme`"; | ||
} | ||
|
||
@Override | ||
public Usage getUsage() { | ||
return new Usage(); | ||
} | ||
|
||
@Override | ||
public CategoryType getCategoryType() { | ||
return CategoryType.FUN; | ||
} | ||
} |
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
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
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
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
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
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
78 changes: 78 additions & 0 deletions
78
src/main/java/com/beanbeanjuice/utility/helper/RedditAPI.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.beanbeanjuice.utility.helper; | ||
|
||
import com.beanbeanjuice.main.CafeBot; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
public class RedditAPI { | ||
|
||
private String reddit_api_url = "https://www.reddit.com/r/{SUBREDDIT}/random/.json"; | ||
private String reddit_url; | ||
private String reddit_image_url; | ||
private String reddit_title; | ||
private String reddit_username; | ||
private String reddit_subreddit; | ||
private String reddit_description; | ||
|
||
/** | ||
* Get the completed {@link MessageEmbed} for the subreddit. | ||
* @param subreddit The subreddit specified. | ||
* @return The new {@link MessageEmbed} to be sent. | ||
*/ | ||
@NotNull | ||
public MessageEmbed getRedditEmbed(@NotNull String subreddit) { | ||
reddit_subreddit = subreddit; | ||
reddit_api_url = reddit_api_url.replace("{SUBREDDIT}", reddit_subreddit); | ||
contactRedditAPI(); | ||
|
||
// Making sure it's not a video | ||
while (reddit_image_url.contains("v.red")) { | ||
contactRedditAPI(); | ||
} | ||
|
||
EmbedBuilder embedBuilder = new EmbedBuilder(); | ||
embedBuilder.setTitle(reddit_title, reddit_url); | ||
embedBuilder.setImage(reddit_image_url); | ||
embedBuilder.setFooter("By: " + reddit_username + " - r/" + reddit_subreddit); | ||
embedBuilder.setColor(CafeBot.getGeneralHelper().getRandomColor()); | ||
embedBuilder.setDescription(reddit_description); | ||
return embedBuilder.build(); | ||
} | ||
|
||
/** | ||
* A method used for contacting the guilds who have enabled the bot update notification. | ||
*/ | ||
private void contactRedditAPI() { | ||
HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build(); | ||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(reddit_api_url)).build(); | ||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(HttpResponse::body) | ||
.thenApply(this::parse) | ||
.join(); | ||
} | ||
|
||
private String parse(String responseBody) { | ||
ObjectMapper defaultObjectMapper = new ObjectMapper(); | ||
try { | ||
JsonNode node = defaultObjectMapper.readTree(responseBody).get(0).get("data").get("children").get(0).get("data"); | ||
reddit_url = "https://www.reddit.com" + node.get("permalink").textValue(); | ||
reddit_image_url = node.get("url").textValue(); | ||
reddit_title = node.get("title").textValue(); | ||
reddit_username = node.get("author").textValue(); | ||
reddit_description = node.get("selftext").textValue(); | ||
return node.toString(); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |