Skip to content

Commit a85ac84

Browse files
authored
Merge branch 'main' into small-cleanup
2 parents fe095df + 8494700 commit a85ac84

File tree

57 files changed

+592
-246
lines changed

Some content is hidden

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

57 files changed

+592
-246
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ At startup, the bot will initially start by loading just the global settings, an
2828

2929
We're using [DIH4JDA](https://github.com/DynxstyGIT/DIH4JDA) as our Command/Interaction framework, which makes it quite easy to add new commands.
3030

31-
[PingCommand.java](https://github.com/Java-Discord/JavaBot/blob/main/src/main/java/net/javadiscord/javabot/systems/commands/PingCommand.java)
31+
[PingCommand.java](https://github.com/Java-Discord/JavaBot/blob/main/src/main/java/net/javadiscord/javabot/systems/user_commands/PingCommand.java)
3232
```java
3333
/**
3434
* <h3>This class represents the /ping command.</h3>

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ dependencies {
3030
compileOnly("org.jetbrains:annotations:23.0.0")
3131

3232
// DIH4JDA (Command Framework) & JDA
33-
implementation("xyz.dynxsty:dih4jda:1.6.2")
34-
implementation("net.dv8tion:JDA:5.0.0-beta.2") {
33+
implementation("com.github.DynxstyGIT:DIH4JDA:120a15ad2e")
34+
implementation("net.dv8tion:JDA:5.0.0-beta.12") {
3535
exclude(module = "opus-java")
3636
}
3737

src/main/java/net/javadiscord/javabot/SpringConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import xyz.dynxsty.dih4jda.DIH4JDA;
1616
import xyz.dynxsty.dih4jda.DIH4JDABuilder;
17-
import xyz.dynxsty.dih4jda.DIH4JDALogger;
1817
import xyz.dynxsty.dih4jda.exceptions.DIH4JDAException;
1918
import xyz.dynxsty.dih4jda.interactions.commands.application.RegistrationType;
2019

@@ -93,7 +92,8 @@ JDA jda(BotConfig botConfig, ApplicationContext ctx) throws LoginException {
9392
DIH4JDA initializeDIH4JDA(JDA jda) throws DIH4JDAException {
9493
DIH4JDA.setDefaultRegistrationType(RegistrationType.GLOBAL);
9594
return DIH4JDABuilder.setJDA(jda)
96-
.disableLogging(DIH4JDALogger.Type.SMART_QUEUE_IGNORED)
95+
.setGlobalSmartQueue(false)
96+
.setGuildSmartQueue(false)
9797
.disableAutomaticCommandRegistration()
9898
.build();
9999
}

src/main/java/net/javadiscord/javabot/data/config/guild/HelpConfig.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ public class HelpConfig extends GuildConfigItem {
4949
You can disable notifications like this using the `/preferences` command.
5050
[Post link](%s)
5151
""";
52-
53-
52+
53+
/**
54+
* The message that is sent in a post to tell users that they
55+
* should use discord's code-formatting, provided the bot detects unformatted code.
56+
* Issued by {@link net.javadiscord.javabot.systems.help.AutoCodeFormatter}
57+
*/
58+
private String formatHintMessage = "> Please format your code to make it more readable. \n> For java, it should look like this: \n```\u200B`\u200B`\u200B`\u200Bjava\npublic void foo() {\n \n}\n\u200B`\u200B`\u200B`\u200B```";
59+
5460
/**
5561
* The message that's sent when a user unreserved a channel where other users
5662
* participated in.
@@ -93,6 +99,12 @@ public class HelpConfig extends GuildConfigItem {
9399
*/
94100
private int minimumMessageLength = 10;
95101

102+
/**
103+
* The message-embed's footnote of an unformatted-code-replacement.
104+
* Issued by {@link net.javadiscord.javabot.systems.help.AutoCodeFormatter}
105+
*/
106+
private String autoFormatInfoMessage = "This message has been formatted automatically. You can disable this using ``/preferences``.";
107+
96108
/**
97109
* The amount of experience points one gets for being thanked by the help channel owner.
98110
*/

src/main/java/net/javadiscord/javabot/data/h2db/commands/MessageCacheInfoSubcommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.javadiscord.javabot.data.h2db.commands;
22

3+
import net.javadiscord.javabot.util.UserUtils;
34
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
45
import net.dv8tion.jda.api.EmbedBuilder;
56
import net.dv8tion.jda.api.Permission;
@@ -45,7 +46,7 @@ private MessageEmbed buildInfoEmbed(GuildConfig config, User author) {
4546
long messages = dbActions.count("SELECT count(*) FROM message_cache");
4647
int maxMessages = config.getMessageCacheConfig().getMaxCachedMessages();
4748
return new EmbedBuilder()
48-
.setAuthor(author.getAsTag(), null, author.getEffectiveAvatarUrl())
49+
.setAuthor(UserUtils.getUserTag(author), null, author.getEffectiveAvatarUrl())
4950
.setTitle("Message Cache Info")
5051
.setColor(Responses.Type.DEFAULT.getColor())
5152
.addField("Table Size", dbActions.getLogicalSize("message_cache") + " bytes", false)

src/main/java/net/javadiscord/javabot/data/h2db/commands/QuickMigrateSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ CREATE TABLE my_table (
118118
.setRequired(true)
119119
.build();
120120
return Modal.create("quick-migrate", "Quick Migrate")
121-
.addActionRows(ActionRow.of(sqlInput), ActionRow.of(confirmInput))
121+
.addComponents(ActionRow.of(sqlInput), ActionRow.of(confirmInput))
122122
.build();
123123
}
124124
}

src/main/java/net/javadiscord/javabot/data/h2db/message_cache/MessageCache.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.ExecutorService;
2929
import java.util.stream.Collectors;
3030

31+
import net.javadiscord.javabot.util.UserUtils;
3132
import org.springframework.dao.DataAccessException;
3233
import org.springframework.stereotype.Service;
3334

@@ -142,7 +143,7 @@ public void sendDeletedMessageToLog(Guild guild, MessageChannel channel, CachedM
142143
private EmbedBuilder buildMessageCacheEmbed(MessageChannel channel, User author, CachedMessage before) {
143144
long epoch = IdCalculatorCommand.getTimestampFromId(before.getMessageId()) / 1000;
144145
return new EmbedBuilder()
145-
.setAuthor(author.getAsTag(), null, author.getEffectiveAvatarUrl())
146+
.setAuthor(UserUtils.getUserTag(author), null, author.getEffectiveAvatarUrl())
146147
.addField("Author", author.getAsMention(), true)
147148
.addField("Channel", channel.getAsMention(), true)
148149
.addField("Created at", String.format("<t:%s:F>", epoch), true)
@@ -200,33 +201,33 @@ private InputStream buildDeletedMessageFile(User author, CachedMessage message)
200201
DateTimeFormatter formatter = TimeUtils.STANDARD_FORMATTER.withZone(ZoneOffset.UTC);
201202
Instant instant = Instant.ofEpochMilli(IdCalculatorCommand.getTimestampFromId(message.getMessageId()));
202203
String in = String.format("""
203-
Author: %s
204-
ID: %s
205-
Created at: %s
204+
Author: %s
205+
ID: %s
206+
Created at: %s
206207
207-
--- Message Content ---
208+
--- Message Content ---
208209
209-
%s
210-
""", author.getAsTag(), message.getMessageId(), formatter.format(instant), message.getMessageContent());
210+
%s
211+
""", UserUtils.getUserTag(author), message.getMessageId(), formatter.format(instant), message.getMessageContent());
211212
return new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8));
212213
}
213214

214215
private InputStream buildEditedMessageFile(User author, CachedMessage before, Message after) {
215216
DateTimeFormatter formatter = TimeUtils.STANDARD_FORMATTER.withZone(ZoneOffset.UTC);
216217
Instant instant = Instant.ofEpochMilli(IdCalculatorCommand.getTimestampFromId(before.getMessageId()));
217218
String in = String.format("""
218-
Author: %s
219-
ID: %s
220-
Created at: %s
219+
Author: %s
220+
ID: %s
221+
Created at: %s
221222
222-
--- Message Content (before) ---
223+
--- Message Content (before) ---
223224
224-
%s
225+
%s
225226
226-
--- Message Content (after) ---
227+
--- Message Content (after) ---
227228
228-
%s
229-
""", author.getAsTag(), before.getMessageId(), formatter.format(instant), before.getMessageContent(), after.getContentRaw());
229+
%s
230+
""", UserUtils.getUserTag(author), before.getMessageId(), formatter.format(instant), before.getMessageContent(), after.getContentRaw());
230231
return new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8));
231232
}
232233
}

src/main/java/net/javadiscord/javabot/listener/HugListener.java

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,59 @@
2222
*/
2323
@RequiredArgsConstructor
2424
public class HugListener extends ListenerAdapter {
25-
private static final Pattern FUCKER = Pattern.compile("(fuck)(ing|er|ed|k+)?", Pattern.CASE_INSENSITIVE);
25+
private static final Pattern FUCKER = Pattern.compile(
26+
"(fuck)(ing|er|ed|k+)?",
27+
Pattern.CASE_INSENSITIVE
28+
);
2629
private final AutoMod autoMod;
2730
private final BotConfig botConfig;
2831

32+
private static String processHug(String originalText) {
33+
// FucK -> HuG, FuCk -> Hug
34+
return String.valueOf(copyCase(originalText, 0, 'h')) + copyCase(originalText, 1, 'u') +
35+
copyCase(originalText, 3, 'g');
36+
}
37+
38+
private static String replaceFucks(String str) {
39+
return FUCKER.matcher(str).replaceAll(matchResult -> {
40+
String theFuck = matchResult.group(1);
41+
String suffix = Objects.requireNonNullElse(matchResult.group(2), "");
42+
String processedSuffix = switch (suffix.toLowerCase()) {
43+
case "er", "ed", "ing" ->
44+
copyCase(suffix, 0, 'g') + suffix; // fucking, fucker, fucked
45+
case "" -> ""; // just fuck
46+
default -> copyCase(suffix, "g".repeat(suffix.length())); // fuckkkkk...
47+
};
48+
return processHug(theFuck) + processedSuffix;
49+
});
50+
}
51+
52+
private static String copyCase(String source, String toChange) {
53+
if (source.length() != toChange.length()) {
54+
throw new IllegalArgumentException("lengths differ");
55+
}
56+
StringBuilder sb = new StringBuilder();
57+
for (int i = 0; i < source.length(); i++) {
58+
sb.append(copyCase(source, i, toChange.charAt(i)));
59+
}
60+
return sb.toString();
61+
}
62+
63+
private static char copyCase(String original, int index, char newChar) {
64+
if (Character.isUpperCase(original.charAt(index))) {
65+
return Character.toUpperCase(newChar);
66+
} else {
67+
return newChar;
68+
}
69+
}
70+
2971
@Override
3072
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
3173
if (!event.isFromGuild()) {
3274
return;
3375
}
34-
if (autoMod.hasSuspiciousLink(event.getMessage()) || autoMod.hasAdvertisingLink(event.getMessage())) {
76+
if (autoMod.hasSuspiciousLink(event.getMessage()) ||
77+
autoMod.hasAdvertisingLink(event.getMessage())) {
3578
return;
3679
}
3780
if (!event.getMessage().getMentions().getUsers().isEmpty()) {
@@ -40,7 +83,8 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
4083
if (event.isWebhookMessage()) {
4184
return;
4285
}
43-
if (event.getChannel().getIdLong() == botConfig.get(event.getGuild()).getModerationConfig()
86+
if (event.getChannel().getIdLong() == botConfig.get(event.getGuild())
87+
.getModerationConfig()
4488
.getSuggestionChannelId()) {
4589
return;
4690
}
@@ -49,65 +93,30 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
4993
tc = event.getChannel().asTextChannel();
5094
}
5195
if (event.isFromThread()) {
52-
StandardGuildChannel parentChannel = event.getChannel().asThreadChannel().getParentChannel().asStandardGuildChannel();
96+
StandardGuildChannel parentChannel = event.getChannel()
97+
.asThreadChannel()
98+
.getParentChannel()
99+
.asStandardGuildChannel();
53100
if (parentChannel instanceof TextChannel textChannel) {
54101
tc = textChannel;
55102
}
56103
}
57104
if (tc == null) {
58105
return;
106+
59107
}
60108
String content = event.getMessage().getContentRaw();
61109
if (FUCKER.matcher(content).find()) {
62110
long threadId = event.isFromThread() ? event.getChannel().getIdLong() : 0;
63-
WebhookUtil.ensureWebhookExists(tc,
64-
wh -> sendWebhookMessage(wh, event.getMessage(), replaceFucks(content), threadId),
65-
e -> ExceptionLogger.capture(e, getClass().getSimpleName()));
66-
}
67-
}
68-
69-
private static String processHug(String originalText) {
70-
// FucK -> HuG, FuCk -> Hug
71-
return String.valueOf(copyCase(originalText, 0, 'h'))
72-
+ copyCase(originalText, 1, 'u')
73-
+ copyCase(originalText, 3, 'g');
74-
}
75-
76-
private static String replaceFucks(String str) {
77-
return FUCKER.matcher(str).replaceAll(matchResult -> {
78-
String theFuck = matchResult.group(1);
79-
String suffix = Objects.requireNonNullElse(matchResult.group(2), "");
80-
String processedSuffix = switch(suffix.toLowerCase()) {
81-
case "er", "ed", "ing" -> copyCase(suffix, 0, 'g') + suffix; // fucking, fucker, fucked
82-
case "" -> ""; // just fuck
83-
default -> copyCase(suffix, "g".repeat(suffix.length())); // fuckkkkk...
84-
};
85-
return processHug(theFuck) + processedSuffix;
86-
});
87-
}
88-
89-
private static String copyCase(String source, String toChange) {
90-
if (source.length() != toChange.length()) throw new IllegalArgumentException("lengths differ");
91-
StringBuilder sb = new StringBuilder();
92-
for (int i = 0; i < source.length(); i++) {
93-
sb.append(copyCase(source, i, toChange.charAt(i)));
111+
WebhookUtil.ensureWebhookExists(
112+
tc,
113+
wh -> WebhookUtil.replaceMemberMessage(wh, event.getMessage(),
114+
replaceFucks(content), threadId
115+
),
116+
e -> ExceptionLogger.capture(e, getClass().getSimpleName())
117+
);
94118
}
95-
return sb.toString();
96119
}
97120

98-
private static char copyCase(String original, int index, char newChar) {
99-
if (Character.isUpperCase(original.charAt(index))) {
100-
return Character.toUpperCase(newChar);
101-
} else {
102-
return newChar;
103-
}
104-
}
105121

106-
private void sendWebhookMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId) {
107-
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId, null, null)
108-
.thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> {
109-
ExceptionLogger.capture(e, getClass().getSimpleName());
110-
return null;
111-
});
112-
}
113122
}

src/main/java/net/javadiscord/javabot/listener/VotingRegulationListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class VotingRegulationListener extends ListenerAdapter{
1717

1818
@Override
1919
public void onMessageReactionAdd(MessageReactionAddEvent event) {
20+
if (event.getUser().isBot() || event.getUser().isSystem()) {
21+
return;
22+
}
2023
if(isCriticalEmoji(event)) {
2124
event.retrieveMessage().queue(msg->{
2225
if(doesAuthorMatch(event.getUserIdLong(), msg)) {
@@ -36,7 +39,7 @@ private boolean doesAuthorMatch(long userId, Message msg) {
3639
}
3740

3841
private boolean isCriticalEmoji(MessageReactionAddEvent event) {
39-
return getUpvoteEmoji(event).equals(event.getEmoji()) ||
42+
return event.getEmoji().equals(getUpvoteEmoji(event)) ||
4043
event.getEmoji().getType() == Emoji.Type.UNICODE &&
4144
botConfig.get(event.getGuild()).getStarboardConfig().getEmojis().contains(event.getEmoji().asUnicode());
4245
}

0 commit comments

Comments
 (0)