Skip to content

Commit

Permalink
Java-Discord#60 working implementation for very basic gist links. Doe…
Browse files Browse the repository at this point in the history
…s not work yet if a specific file in a multifile gist is given.
  • Loading branch information
Kemikals committed Oct 10, 2021
1 parent 8080075 commit 693949e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ dependencies {
// H2 Database
implementation group: 'com.h2database', name: 'h2', version: '1.4.200'

// Retrofit
implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.9.0'
implementation group: 'com.squareup.retrofit2', name: 'converter-jackson', version: '2.9.0'


// Lombok Annotations
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/javadiscord/javabot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ private static void addEventListeners(JDA jda) {
new StarboardListener(),
new InteractionListener(),
new HelpChannelListener(),
new ShareKnowledgeVoteListener()
new ShareKnowledgeVoteListener(),
new GistListener()
);
}
}
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/javadiscord/javabot/events/GistListener.java
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.

Copy link
@danthe1st

danthe1st Oct 11, 2021

Note that this does not include the backticks created in constructMessage.

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 + "```";
}
}
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;
}
}
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;
}
}
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);
}

0 comments on commit 693949e

Please sign in to comment.