Skip to content

Commit 92e7655

Browse files
Reworked the Help Overview Embed
1 parent 6051197 commit 92e7655

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import lombok.EqualsAndHashCode;
55
import net.dv8tion.jda.api.entities.Category;
66
import net.dv8tion.jda.api.entities.Role;
7-
import net.dv8tion.jda.api.entities.TextChannel;
87
import net.javadiscord.javabot.data.config.GuildConfigItem;
98
import net.javadiscord.javabot.systems.help.naming_strategies.*;
109

@@ -20,7 +19,7 @@ public class HelpConfig extends GuildConfigItem {
2019
/**
2120
* The id of the help overview channel.
2221
*/
23-
private long helpOverviewId;
22+
private Map<String, Long> helpOverviewMessageIds = Map.of();
2423

2524
/**
2625
* The id of the channel category that contains all open channels.
@@ -189,10 +188,6 @@ public Category getDormantChannelCategory() {
189188
return getGuild().getCategoryById(this.dormantCategoryId);
190189
}
191190

192-
public TextChannel getHelpOverviewChannel() {
193-
return getGuild().getTextChannelById(this.helpOverviewId);
194-
}
195-
196191
public Role getHelperRole() {
197192
return this.getGuild().getRoleById(this.helperRoleId);
198193
}

src/main/java/net/javadiscord/javabot/systems/help/HelpChannelUpdater.java

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.entities.*;
77
import net.dv8tion.jda.api.entities.emoji.Emoji;
8+
import net.dv8tion.jda.api.interactions.components.buttons.Button;
89
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
910
import net.dv8tion.jda.api.requests.RestAction;
1011
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
1112
import net.dv8tion.jda.internal.interactions.component.ButtonImpl;
1213
import net.dv8tion.jda.internal.requests.CompletedRestAction;
14+
import net.javadiscord.javabot.Bot;
1315
import net.javadiscord.javabot.data.config.guild.HelpConfig;
1416
import net.javadiscord.javabot.systems.help.model.ChannelReservation;
1517
import net.javadiscord.javabot.util.ExceptionLogger;
1618
import net.javadiscord.javabot.util.Responses;
19+
import net.javadiscord.javabot.util.StringResourceCache;
20+
import net.javadiscord.javabot.util.StringUtils;
1721
import org.jetbrains.annotations.NotNull;
1822

1923
import java.sql.SQLException;
@@ -55,14 +59,15 @@ public HelpChannelUpdater(JDA jda, HelpConfig config, List<ChannelSemanticCheck>
5559
@Override
5660
public void run() {
5761
for (TextChannel channel : config.getReservedChannelCategory().getTextChannels()) {
58-
this.checkReservedChannel(channel).queue();
62+
checkReservedChannel(channel).queue();
5963
}
6064
for (TextChannel channel : config.getOpenChannelCategory().getTextChannels()) {
61-
this.checkOpenChannel(channel).queue();
65+
checkOpenChannel(channel).queue();
6266
}
63-
this.balanceChannels();
64-
if (config.getHelpOverviewChannel() != null) {
65-
this.updateHelpOverview();
67+
balanceChannels();
68+
// periodically update the Help Channel Overview Message
69+
if (config.getHelpOverviewMessageIds() != null && !config.getHelpOverviewMessageIds().isEmpty()) {
70+
updateHelpOverview();
6671
}
6772
}
6873

@@ -366,49 +371,61 @@ private RestAction<?> semanticMessageCheck(TextChannel channel, User owner, List
366371
}
367372

368373
private void updateHelpOverview() {
369-
TextChannel channel = config.getHelpOverviewChannel();
370-
MessageHistory history = channel.getHistory();
371-
history.retrievePast(100).queue(
372-
messages -> {
373-
Optional<Message> latestMessage = messages.stream().filter(m -> m.getAuthor().equals(jda.getSelfUser())).findFirst();
374-
if (latestMessage.isPresent()) {
375-
latestMessage.get().editMessageEmbeds(buildHelpOverviewEmbed()).queue();
376-
} else {
377-
channel.sendMessageEmbeds(buildHelpOverviewEmbed()).queue();
378-
}
379-
}
380-
);
374+
config.getHelpOverviewMessageIds().forEach((channelId, messageId) -> {
375+
TextChannel channel = config.getGuild().getTextChannelById(channelId);
376+
if (channel == null) {
377+
log.error("Could not find Help Overview Channel with id '{}'", channelId);
378+
return;
379+
}
380+
List<TextChannel> availableChannels = config.getOpenChannelCategory().getTextChannels();
381+
List<Button> buttons = new ArrayList<>(2);
382+
if (!availableChannels.isEmpty()) {
383+
buttons.add(Button.link(StringUtils.buildChannelJumpUrl(availableChannels.get(0)), "Show me an available Help Channel!"));
384+
}
385+
buttons.add(Button.link(StringResourceCache.load("/help_overview/overview_image_url.txt"), "How does this work?"));
386+
channel.retrieveMessageById(messageId).queue(
387+
m -> m.editMessageEmbeds(buildHelpOverviewEmbed()).setActionRow(buttons).queue(),
388+
err -> channel.sendMessageEmbeds(buildHelpOverviewEmbed()).queue(m -> {
389+
config.getHelpOverviewMessageIds().put(channelId, m.getIdLong());
390+
Bot.getConfig().flush();
391+
log.info("Successfully created new Help Overview Message in '{}' on message with id '{}'", channelId, m.getId());
392+
})
393+
);
394+
});
381395
}
382396

383-
private MessageEmbed buildHelpOverviewEmbed() {
397+
private @NotNull MessageEmbed buildHelpOverviewEmbed() {
384398
String availableHelpChannels = config.getOpenChannelCategory().getTextChannels()
385399
.stream()
386400
.map(TextChannel::getAsMention)
387-
.collect(Collectors.joining("\n"));
401+
.collect(Collectors.joining(", "));
388402
StringBuilder reservedHelpChannels = new StringBuilder();
389403
for (TextChannel channel : config.getReservedChannelCategory().getTextChannels()) {
390404
Optional<ChannelReservation> optional = channelManager.getReservationForChannel(channel.getIdLong());
391405
if (optional.isEmpty()) continue;
392406
ChannelReservation reservation = optional.get();
407+
// Attempt to fetch the user. As JDA will cache the user upon retrieval, this may be too 'slow' on the first run (for the appending),
408+
// but will automatically fix itself after the embed is updated again.
393409
jda.retrieveUserById(reservation.getUserId()).queue(
394-
u -> reservedHelpChannels.append(String.format("""
410+
user -> reservedHelpChannels.append(String.format("""
395411
%s
396412
Reserved by %s <t:%s:R>
397413
398-
""", channel.getAsMention(), u.getAsMention(),
414+
""", channel.getAsMention(), user.getAsMention(),
399415
reservation.getReservedAt().toEpochSecond(ZoneOffset.UTC))),
400-
e -> {
401-
}
416+
e -> ExceptionLogger.capture(e, getClass().getSimpleName())
402417
);
403418
}
404-
return new EmbedBuilder()
419+
EmbedBuilder builder = new EmbedBuilder()
405420
.setTitle("Help Overview")
406421
.setColor(Responses.Type.DEFAULT.getColor())
407-
.addField("Available Help Channels", availableHelpChannels, false)
408-
.addField("Reserved Help Channels", reservedHelpChannels.toString(), false)
409-
.addField("Dormant Help Channels", String.format("%s dormant channels", config.getDormantChannelCategory().getTextChannels().size()), false)
410-
.setFooter(String.format("Last refreshed: %s", Instant.now()))
411-
.setTimestamp(Instant.now())
412-
.build();
422+
.setDescription(availableHelpChannels + " are __**available**__ to claim!")
423+
.setFooter("Last refreshed: ")
424+
.setTimestamp(Instant.now());
425+
if (!reservedHelpChannels.isEmpty()) {
426+
builder.addField("Reserved Help Channels", reservedHelpChannels.toString(), false);
427+
}
428+
builder.addField("Dormant Help Channels", String.format("%s dormant channels", config.getDormantChannelCategory().getTextChannels().size()), false);
429+
return builder.build();
413430
}
414431
}

src/main/java/net/javadiscord/javabot/util/StringUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.javadiscord.javabot.util;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import net.dv8tion.jda.api.entities.GuildMessageChannel;
45
import net.dv8tion.jda.api.entities.Message;
56
import net.dv8tion.jda.api.entities.MessageReaction;
67
import net.dv8tion.jda.api.entities.emoji.Emoji;
@@ -180,4 +181,8 @@ public static int countReactions(@NotNull Message msg, Emoji emoji) {
180181
.filter(user -> !user.isBot() && !user.isSystem())
181182
.count();
182183
}
184+
185+
public static String buildChannelJumpUrl(@NotNull GuildMessageChannel channel) {
186+
return String.format("https://discord.com/channels/%s/%s", channel.getGuild().getId(), channel.getIdLong());
187+
}
183188
}

src/main/resources/help_guidelines/guidelines_text.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**2.** Only ask questions in open help channels. Asking a question in someone else's reserved help channel is not polite, and your messages may be removed without notice. Doing this often will result in a ban.
44

5-
**3.** You may use the` /help-ping` command if your question is urgent.
5+
**3.** You may use the `/help ping` command if your question is urgent.
66
*__Abusing this will result in warnings and/or a ban.__*
77

88
**4.** Do not ask for help with exams or homework. You may ask for help with understanding individual concepts and parts of a question, but homework and exam questions that show little effort on your part will most likely go unanswered and may be removed.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://cdn.discordapp.com/attachments/674585018697515048/1004325439210266624/HelpNew.png

0 commit comments

Comments
 (0)