Skip to content

Commit 4480316

Browse files
authored
Merge pull request #121 from Matyrobbrt/role-panels
Update to JDA v5 and reaction roles.
2 parents 0985c64 + 284468b commit 4480316

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1695
-157
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ repositories {
5252
}
5353
maven {
5454
name 'jda-chewtils'
55-
url 'https://m2.chew.pro/releases'
55+
url 'https://m2.chew.pro/snapshots'
5656
}
5757
maven {
5858
name 'linkie'

dependencies.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ ext {
88
licenser = "0.6.1"
99

1010
//-- DEPENDENCY VERSIONS
11-
jdaVersion = "4.4.0_351"
12-
jdaChewtilsVersion = "1.24.1"
11+
jdaVersion = "5.0.0-alpha.5"
12+
jdaChewtilsVersion = "2.0-interactions-SNAPSHOT"
1313
guavaVersion = "31.0.1-jre"
1414
gsonVersion = "2.8.9"
1515
logbackClassicVersion = "1.2.10"

src/main/java/com/mcmoddev/mmdbot/MMDBot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public static Jdbi database() {
126126
* @param args Arguments provided to the program.
127127
*/
128128
public static void main(final String[] args) {
129+
Runtime.getRuntime().addShutdownHook(new Thread(() -> LOGGER.warn("The bot is shutting down!")));
129130
final var configPath = Paths.get("mmdbot_config.toml");
130131
MMDBot.config = new BotConfig(configPath);
131132
if (MMDBot.config.isNewlyGenerated()) {

src/main/java/com/mcmoddev/mmdbot/core/BotConfig.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
3131
import com.mcmoddev.mmdbot.MMDBot;
3232
import net.dv8tion.jda.api.Permission;
33+
import net.dv8tion.jda.api.entities.Message;
3334
import org.jetbrains.annotations.NotNull;
3435

3536
import java.nio.file.Path;
@@ -117,6 +118,13 @@ public String getToken() {
117118
.orElse("");
118119
}
119120

121+
@NotNull
122+
public String getGithubToken() {
123+
return config.<String>getOptional("bot.githubToken")
124+
.filter(string -> string.indexOf('!') == -1 || string.isEmpty())
125+
.orElse("");
126+
}
127+
120128
/**
121129
* Returns the snowflake ID of the bot's owner.
122130
* <p>
@@ -471,4 +479,24 @@ public boolean isCommandModuleEnabled() {
471479
public boolean isEventLoggingModuleEnabled() {
472480
return config.<Boolean>getOrElse("modules.event_logging_module_enabled", true);
473481
}
482+
483+
/**
484+
* Gets the role associated with the {@code emote} from the specified role panel
485+
* @param channelId the channel ID of the role panel
486+
* @param messageId the message ID of the role panel
487+
* @param emote the emote
488+
* @return
489+
*/
490+
public long getRoleForRolePanel(final long channelId, final long messageId, final String emote) {
491+
return config.<Long>getOrElse("role_panels.%s-%s.%s".formatted(channelId, messageId, emote), 0l);
492+
}
493+
494+
public long getRoleForRolePanel(final Message message, final String emote) {
495+
return getRoleForRolePanel(message.getChannel().getIdLong(), message.getIdLong(), emote);
496+
}
497+
498+
public void addRolePanel(final long channelId, final long messageId, final String emote, final long roleId) {
499+
config.set("role_panels.%s-%s.%s".formatted(channelId, messageId, emote), roleId);
500+
config.save();
501+
}
474502
}

src/main/java/com/mcmoddev/mmdbot/core/References.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
*/
2121
package com.mcmoddev.mmdbot.core;
2222

23+
import com.google.gson.Gson;
24+
import com.google.gson.GsonBuilder;
2325
import com.mcmoddev.mmdbot.MMDBot;
2426

2527
import java.time.Instant;
2628
import java.time.OffsetDateTime;
2729
import java.time.ZoneOffset;
2830
import java.time.format.DateTimeFormatter;
31+
import java.util.Random;
2932

3033
/**
3134
* Store settings that don't really change in here.
@@ -77,4 +80,10 @@ public class References {
7780
* The constant STARTUP_TIME.
7881
*/
7982
public static Instant STARTUP_TIME;
83+
84+
public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
85+
86+
public static final Random RANDOM = new Random();
87+
88+
public static boolean BOT_READY = false;
8089
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.gist;
22+
23+
import com.google.gson.JsonObject;
24+
import com.mcmoddev.mmdbot.core.References;
25+
import com.mcmoddev.mmdbot.utilities.Utils;
26+
import kotlinx.serialization.json.Json;
27+
28+
import javax.annotation.Nullable;
29+
import java.util.LinkedHashMap;
30+
import java.util.Map;
31+
32+
/**
33+
* Represents a gist which was got from an HTTP request
34+
*
35+
* @author matyrobbrt
36+
*/
37+
public record ExistingGist(JsonObject json, String url, String forksUrl, String commitsUrl,
38+
String id, boolean isPublic, String commentsUrl, int comments,
39+
String htmlUrl, String gitPullUrl, String gitPushUrl, String createdAt,
40+
String updatedAt, Map<String, File> files, @Nullable String description) {
41+
42+
public static ExistingGist fromJson(final JsonObject json) {
43+
var url = getFromJson(json, "url");
44+
var id = getFromJson(json, "id");
45+
var isPublic = json.get("public").getAsBoolean();
46+
var commentsUrl = getFromJson(json, "comments_url");
47+
var comments = json.get("comments").getAsInt();
48+
var htmlUrl = getFromJson(json, "html_url");
49+
var gitPullUrl = getFromJson(json, "git_pull_url");
50+
var gitPushUrl = getFromJson(json, "git_push_url");
51+
var createdAt = getFromJson(json, "created_at");
52+
var updatedAt = getFromJson(json, "updated_at");
53+
var forksUrl = getFromJson(json, "forks_url");
54+
var commitsUrl = getFromJson(json, "commits_url");
55+
var description = getFromJson(json, "description");
56+
57+
var files = new LinkedHashMap<String, File>();
58+
59+
final var filesJson = json.get("files").getAsJsonObject();
60+
for (final String key : filesJson.keySet()) {
61+
files.put(key, File.fromJson(filesJson.get(key).getAsJsonObject()));
62+
}
63+
return new ExistingGist(json, url, forksUrl, commitsUrl, id, isPublic, commentsUrl, comments, htmlUrl, gitPullUrl,
64+
gitPushUrl, createdAt, updatedAt, files, description);
65+
}
66+
67+
private static String getFromJson(final JsonObject json, final String key) {
68+
return json.get(key).isJsonPrimitive() && json.get(key).getAsJsonPrimitive().isString() ? json.get(key).getAsString() : null;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return json.toString();
74+
}
75+
76+
public record File(int size, String url, String type, boolean isTruncated) {
77+
78+
public static File fromJson(final JsonObject json) {
79+
final var size = json.get("size").getAsInt();
80+
final var url = json.get("raw_url").getAsString();
81+
final var type = json.get("type").getAsString();
82+
final var truncated = json.has("truncated") && json.get("truncated").getAsBoolean();
83+
return new File(size, url, type, truncated);
84+
}
85+
86+
}
87+
88+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.gist;
22+
23+
import com.google.gson.JsonObject;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.util.LinkedHashMap;
28+
import java.util.Map;
29+
30+
/**
31+
* Represents a gist which is ready to be made using an HTTP request.
32+
*
33+
* @author matyrobbrt
34+
*/
35+
public class Gist {
36+
37+
private final String description;
38+
private final boolean isPublic;
39+
private final Map<String, JsonObject> files = new LinkedHashMap<>();
40+
41+
public Gist(final String description, final boolean isPublic) {
42+
this.description = description;
43+
this.isPublic = isPublic;
44+
}
45+
46+
public Gist(final String description, final boolean isPublic, final String filename, final String content) {
47+
this.description = description;
48+
this.isPublic = isPublic;
49+
50+
final JsonObject fileobj = new JsonObject();
51+
fileobj.addProperty("content", content);
52+
53+
files.put(filename, fileobj);
54+
}
55+
56+
public Gist(final String description, final boolean isPublic, final File file) throws IOException {
57+
this.description = description;
58+
this.isPublic = isPublic;
59+
60+
final JsonObject fileobj = new JsonObject();
61+
fileobj.addProperty("content", GistUtils.readFile(file));
62+
63+
files.put(file.getName(), fileobj);
64+
}
65+
66+
public Gist addFile(final String filename, final String content) {
67+
final JsonObject fileobj = new JsonObject();
68+
fileobj.addProperty("content", content);
69+
70+
files.put(filename, fileobj);
71+
return this;
72+
}
73+
74+
public Gist addFile(final File file) throws IOException {
75+
final JsonObject fileobj = new JsonObject();
76+
fileobj.addProperty("content", GistUtils.readFile(file));
77+
78+
files.put(file.getName(), fileobj);
79+
return this;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
JsonObject gistobj = new JsonObject();
85+
JsonObject fileobj = new JsonObject();
86+
87+
gistobj.addProperty("public", isPublic);
88+
gistobj.addProperty("description", description);
89+
90+
for (final var entry : files.entrySet()) {
91+
fileobj.add(entry.getKey(), entry.getValue());
92+
}
93+
94+
gistobj.add("files", fileobj);
95+
96+
return gistobj.toString();
97+
}
98+
99+
}

0 commit comments

Comments
 (0)