Skip to content

Commit

Permalink
Fixed pin added message in rare cases not getting deleted after disco…
Browse files Browse the repository at this point in the history
…rd event print
  • Loading branch information
Alf-Melmac committed Jan 9, 2022
1 parent 00186d1 commit a72eeba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private Consumer<Message> slotListMsgConsumer(@NonNull MessageChannel channel, @
return slotListMsg -> {
eventApiDto.getDiscordInformation(guildId).ifPresent(discordInformation -> discordInformation.setSlotListMsgPartOne(slotListMsg.getId()));

slotListMsg.pin().queue(unused -> deleteLatestMessageIfTypePinAdd(channel));
slotListMsg.pin().queue();

sendMessage(channel, spacerCharIfEmpty(ListUtils.shift(slotListMessages)),
slotListMsgLastConsumer(channel, eventApiDto, guildId));
Expand All @@ -217,7 +217,7 @@ private Consumer<Message> slotListMsgLastConsumer(@NonNull MessageChannel channe
return slotListMsg -> {
eventApiDto.getDiscordInformation(guildId).ifPresent(discordInformation -> discordInformation.setSlotListMsgPartTwo(slotListMsg.getId()));

slotListMsg.pin().queue(unused -> deleteLatestMessageIfTypePinAdd(channel));
slotListMsg.pin().queue(unused -> deletePinAddedMessages(channel));

eventBotService.updateEvent(eventApiDto);
};
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/de/webalf/slotbot/util/bot/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ private static void deleteMessages(int delay, Message... messages) {
}

/**
* Deletes the latest message from the given channel if {@link MessageType#CHANNEL_PINNED_ADD}
* Checks the last {@code 4} messages and deletes them from the given channel if {@link MessageType#CHANNEL_PINNED_ADD}
*
* @param channel in which the latest message should be deleted
* @param channel in which the latest messages should be deleted
*/
public static void deleteLatestMessageIfTypePinAdd(@NonNull MessageChannel channel) {
channel.getHistory().retrievePast(1).queue(messages -> {
public static void deletePinAddedMessages(@NonNull MessageChannel channel) {
channel.getHistory().retrievePast(4).queue(messages -> {
if (ListUtils.isEmpty(messages)) {
return;
}
final Message message = messages.get(0);
if (MessageType.CHANNEL_PINNED_ADD == message.getType()) {
deleteMessagesInstant(channel, message.getIdLong());
}
messages.forEach(message -> {
if (MessageType.CHANNEL_PINNED_ADD == message.getType()) {
deleteMessagesInstant(channel, message.getIdLong());
}
});
});
}

Expand Down

0 comments on commit a72eeba

Please sign in to comment.