Skip to content

Commit

Permalink
!trick -info will now use Gists when too large for discord...
Browse files Browse the repository at this point in the history
  • Loading branch information
RealMangorage committed May 23, 2024
1 parent f062e4f commit 681026b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mangorage.mangobot.modules.github;

import org.eclipse.egit.github.core.Gist;
import org.eclipse.egit.github.core.GistFile;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.GistService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public final class GistHandler {
public static Gist createGist(String content, String fileName) {
GitHubClient CLIENT = PasteRequestModule.GITHUB_CLIENT.get();
GistService service = new GistService(CLIENT);

Gist gist = new Gist();
gist.setPublic(false);
gist.setDescription("Automatically made from MangoBot.");


Map<String, GistFile> fileMap = new HashMap<>();
GistFile file = new GistFile();
file.setContent(content);
file.setFilename(fileName);
fileMap.put(fileName, file);

gist.setFiles(fileMap);

try {
return service.createGist(gist);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class PasteRequestModule {
);


private static final LazyReference<GitHubClient> GITHUB_CLIENT = LazyReference.create(() -> new GitHubClient().setOAuth2Token(MangoBotPlugin.GITHUB_TOKEN.get()));
static final LazyReference<GitHubClient> GITHUB_CLIENT = LazyReference.create(() -> new GitHubClient().setOAuth2Token(MangoBotPlugin.GITHUB_TOKEN.get()));
private static final List<String> GUILDS = List.of(
"1129059589325852724",
"834300742864601088",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.interactions.modals.Modal;
import net.dv8tion.jda.api.utils.MarkdownSanitizer;
import org.eclipse.egit.github.core.Gist;
import org.jetbrains.annotations.NotNull;
import org.mangorage.basicutils.LogHelper;
import org.mangorage.basicutils.TaskScheduler;
Expand All @@ -22,6 +23,7 @@
import org.mangorage.jdautils.command.CommandOption;
import org.mangorage.mangobot.MangoBotPlugin;
import org.mangorage.mangobot.modules.actions.TrashButtonAction;
import org.mangorage.mangobot.modules.github.GistHandler;
import org.mangorage.mangobotapi.core.commands.Arguments;
import org.mangorage.mangobotapi.core.commands.CommandAlias;
import org.mangorage.mangobotapi.core.commands.CommandResult;
Expand Down Expand Up @@ -428,11 +430,19 @@ public CommandResult execute(Message message, Arguments args) {
if (trick.getType() == TrickType.NORMAL) {
if (trick.getContent().length() < 2000)
details = details + "Content: \n" + trick.getContent();
else {
Gist gist = GistHandler.createGist(trick.getContent(), trick.getTrickID() + ".txt");
details = details + "[[Content](%s)]".formatted(gist.getHtmlUrl());
}
} else if (trick.getType() == TrickType.ALIAS) {
details = details + "Alias -> " + trick.getAliasTarget();
} else if (trick.getType() == TrickType.SCRIPT) {
if (trick.getScript().length() < 2000)
details = details + "Script: \n" + trick.getScript();
else {
Gist gist = GistHandler.createGist(trick.getScript(), trick.getTrickID() + ".txt");
details = details + "[[Script](%s)]".formatted(gist.getHtmlUrl());
}
}

dMessage.apply(message.reply(details))
Expand Down

0 comments on commit 681026b

Please sign in to comment.