Skip to content

Commit 0686498

Browse files
committed
Add a command to link to github issues
1 parent 35a2e01 commit 0686498

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ application {
3434
}
3535

3636
dependencies {
37-
// implementation("com.discord4j:discord4j-core:3.2.4")
38-
implementation("com.github.htmlcsjs:Discord4Floppa:c9e32fe")
39-
implementation("ch.qos.logback:logback-classic:1.4.6")
37+
implementation("com.discord4j:discord4j-core:3.3.0-RC1")
38+
//implementation("com.github.htmlcsjs:Discord4Floppa:c9e32fe")
39+
implementation("ch.qos.logback:logback-classic:1.4.12")
4040
implementation("com.googlecode.json-simple:json-simple:1.1.1")
4141
implementation("me.xdrop:fuzzywuzzy:1.4.0")
4242
implementation("org.luaj:luaj-jse:3.0.1")

src/main/java/xyz/htmlcsjs/coffeeFloppa/CoffeeFloppa.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public static void refreshCommands() {
161161
MessageHandler.addCommand(new SearchMatCommand());
162162
MessageHandler.addCommand(new SearchMatByIdCommand());
163163
MessageHandler.addCommand(new GithubIssueCommand());
164+
MessageHandler.addCommand(new GithubLinkCommand());
164165
MessageHandler.addCommand(new AddonCommand());
165166
MessageHandler.addCommand(new QuestAdminCommand());
166167
MessageHandler.addCommand(new RefreshCommand());

src/main/java/xyz/htmlcsjs/coffeeFloppa/commands/GithubIssueCommand.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public class GithubIssueCommand implements ICommand {
3030

3131
private static final String apiRoot = "https://api.github.com/";
3232
private static final Map<String, String> aliasMap = new HashMap<>();
33-
private static final Map<String, String> categoryAliasMap = new HashMap<>();
34-
33+
private static final Map<String, String> channelAliasMap = new HashMap<>();
3534

3635
public GithubIssueCommand() {
3736
aliasMap.clear();
@@ -46,7 +45,7 @@ public GithubIssueCommand() {
4645
}
4746
aliasMap.put(alias.toLowerCase(), repo);
4847
}
49-
categoryAliasMap.clear();
48+
channelAliasMap.clear();
5049
for (String str : FloppaTomlConfig.channelAliases.split(";")) {
5150
List<String> split = new ArrayList<>(List.of(str.split(":")));
5251
String alias = split.remove(0);
@@ -56,13 +55,13 @@ public GithubIssueCommand() {
5655
} else {
5756
repo = split.get(0);
5857
}
59-
categoryAliasMap.put(alias, repo.toLowerCase());
58+
channelAliasMap.put(alias, repo.toLowerCase());
6059
}
6160
}
6261

6362
@Override
6463
public @NotNull String getName() {
65-
return "gh";
64+
return "ghissue";
6665
}
6766

6867
@Nullable
@@ -78,12 +77,12 @@ public String execute(Message message) {
7877
.forEach(matchResult -> {
7978
if (matchResult.group(1) == null) {
8079
MessageChannel channel = message.getChannel().block();
81-
if (channel != null && categoryAliasMap.containsKey(channel.getId().asString())) {
82-
issueList.add(new IssueListItem(aliasMap.get(categoryAliasMap.get(channel.getId().asString())), matchResult.group(2)));
80+
if (channel != null && channelAliasMap.containsKey(channel.getId().asString())) {
81+
issueList.add(new IssueListItem(aliasMap.get(channelAliasMap.get(channel.getId().asString())), matchResult.group(2)));
8382
} else if (channel instanceof ThreadChannel thread) {
8483
Optional<Snowflake> parentId = thread.getParentId();
85-
if (parentId.isPresent() && categoryAliasMap.containsKey(parentId.get().asString())) {
86-
issueList.add(new IssueListItem(aliasMap.get(categoryAliasMap.get(parentId.get().asString())), matchResult.group(2)));
84+
if (parentId.isPresent() && channelAliasMap.containsKey(parentId.get().asString())) {
85+
issueList.add(new IssueListItem(aliasMap.get(channelAliasMap.get(parentId.get().asString())), matchResult.group(2)));
8786
}
8887
}
8988
} else if (aliasMap.containsKey(matchResult.group(1).toLowerCase())){
@@ -173,7 +172,14 @@ public String execute(Message message) {
173172
public String helpInfo() {
174173
return "Provides links to issues for github repos";
175174
}
176-
private record IssueListItem(String repo, String id) {
177175

176+
public static Map<String, String> getAliasMap() {
177+
return Collections.unmodifiableMap(aliasMap);
178+
}
179+
180+
public static Map<String, String> getChannelAliasMap() {
181+
return Collections.unmodifiableMap(channelAliasMap);
178182
}
183+
184+
private record IssueListItem(String repo, String id) { }
179185
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package xyz.htmlcsjs.coffeeFloppa.commands;
2+
3+
import discord4j.core.object.entity.Message;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
import xyz.htmlcsjs.coffeeFloppa.toml.FloppaTomlConfig;
7+
8+
public class GithubLinkCommand implements ICommand {
9+
@Override
10+
public @NotNull String getName() {
11+
return "github";
12+
}
13+
14+
@Override
15+
public @Nullable String execute(Message message) {
16+
String[] args = message.getContent().split("\\s");
17+
String alias;
18+
if (args.length > 1) {
19+
alias = args[1];
20+
} else {
21+
alias = GithubIssueCommand.getChannelAliasMap().get(message.getChannelId().asString());
22+
}
23+
if (alias == null) {
24+
return String.format("No repository provided\nUsage: `%s%s <repo>` where `repo` is a repository name/alias, "
25+
+ "can be omitted in any channel that has a default alias (i.e ones which #{issue number] works in)", FloppaTomlConfig.prefix, getName());
26+
}
27+
28+
String repo;
29+
if (alias.contains("/")) { // attempt to use alias as the repo directly
30+
repo = alias;
31+
} else {
32+
repo = GithubIssueCommand.getAliasMap().get(alias);
33+
if (repo == null) {
34+
return String.format("No defined repository for alias `%s`", alias);
35+
}
36+
}
37+
38+
return String.format("https://github.com/%s/issues", repo);
39+
}
40+
}

src/main/java/xyz/htmlcsjs/coffeeFloppa/handlers/MessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static Mono<Object> executeMessage(Message message, String msgContent) {
116116
sendMessage(message, command);
117117
} catch (IndexOutOfBoundsException ignored) {}
118118
} else if ((CommandUtil.ghIssuePattern.matcher(msgContent).find() || CommandUtil.ghIssueSmallPattern.matcher(msgContent).find())&& !message.getAuthor().get().isBot()) {
119-
sendMessage(message, commands.get("gh"));
119+
sendMessage(message, commands.get("ghissue"));
120120
} else {
121121
for (String key : searchCommands.keySet()) {
122122
String prefix = key.split(" ")[0];

0 commit comments

Comments
 (0)