forked from Java-Discord/JavaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java-Discord#60 working implementation for very basic gist links. Doe…
…s not work yet if a specific file in a multifile gist is given.
- Loading branch information
Showing
6 changed files
with
176 additions
and
1 deletion.
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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/javadiscord/javabot/events/GistListener.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,70 @@ | ||
package com.javadiscord.javabot.events; | ||
|
||
import com.javadiscord.javabot.external_apis.github.GistResponse; | ||
import com.javadiscord.javabot.external_apis.github.GithubService; | ||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class GistListener extends ListenerAdapter { | ||
|
||
private static String BASIC_GIST_PATTERN_STRING = ".*https://gist\\.github\\.com/.+?(?=/)/(.+?(?=#|\\s))"; | ||
private static Pattern BASIC_GIST_PATTERN = Pattern.compile(BASIC_GIST_PATTERN_STRING); | ||
private GithubService service; | ||
private static int SIZE_LIMIT = 2000; | ||
private static final Logger log = LoggerFactory.getLogger(GistListener.class); | ||
|
||
|
||
public GistListener() { | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl("https://api.github.com/") | ||
.addConverterFactory(JacksonConverterFactory.create()) | ||
.build(); | ||
|
||
service = retrofit.create(GithubService.class); | ||
} | ||
|
||
@Override | ||
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { | ||
if (event.getAuthor().isBot()) return; | ||
Matcher matcher = BASIC_GIST_PATTERN.matcher(event.getMessage().getContentRaw()); | ||
|
||
if (matcher.find()) { | ||
service.getGistInformation(matcher.group(1)).enqueue(new Callback<>() { | ||
@Override | ||
public void onResponse(@NotNull Call<GistResponse> call, @NotNull Response<GistResponse> response) { | ||
GistResponse result = response.body(); | ||
if (result == null || result.getFiles().size() != 1) return; | ||
result.getFiles().values().forEach(file -> { | ||
if (file.getSize() <= SIZE_LIMIT) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
String message = constructMessage(file.getContent(), file.getLanguage()); | ||
event.getChannel().sendMessage(message).queue(); | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
public void onFailure(@NotNull Call<GistResponse> call, @NotNull Throwable t) { | ||
log.error("Unable to complete/parse Gist api call", t); | ||
} | ||
}); | ||
} | ||
|
||
|
||
} | ||
|
||
public String constructMessage(String content, String language) { | ||
return "```" + language + "\n" + content + "```"; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/javadiscord/javabot/external_apis/github/File.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,53 @@ | ||
package com.javadiscord.javabot.external_apis.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class File { | ||
private String language; | ||
private String raw_url; | ||
private Integer size; | ||
private String content; | ||
|
||
@Override | ||
public String toString() { | ||
return "File{" + | ||
"language='" + language + '\'' + | ||
", rawURL='" + raw_url + '\'' + | ||
", size=" + size + | ||
", content='" + content + '\'' + | ||
'}'; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
|
||
public void setLanguage(String language) { | ||
this.language = language; | ||
} | ||
|
||
public String getRaw_url() { | ||
return raw_url; | ||
} | ||
|
||
public void setRaw_url(String raw_url) { | ||
this.raw_url = raw_url; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(int size) { | ||
this.size = size; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/javadiscord/javabot/external_apis/github/GistResponse.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,35 @@ | ||
package com.javadiscord.javabot.external_apis.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
import java.util.Map; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class GistResponse { | ||
private String url; | ||
private Map<String, File> files; | ||
|
||
@Override | ||
public String toString() { | ||
return "GistResponse{" + | ||
"url='" + url + '\'' + | ||
", files=" + files + | ||
'}'; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public Map<String, File> getFiles() { | ||
return files; | ||
} | ||
|
||
public void setFiles(Map<String, File> files) { | ||
this.files = files; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/javadiscord/javabot/external_apis/github/GithubService.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,11 @@ | ||
package com.javadiscord.javabot.external_apis.github; | ||
|
||
|
||
import retrofit2.Call; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Path; | ||
|
||
public interface GithubService { | ||
@GET("gists/{gistID}") | ||
Call<GistResponse> getGistInformation(@Path("gistID") String gistID); | ||
} |
Note that this does not include the backticks created in
constructMessage
.