Skip to content

Commit

Permalink
Remove event discord information if one output message gets deleted a…
Browse files Browse the repository at this point in the history
…nd warn about this
  • Loading branch information
Alf-Melmac committed Apr 4, 2023
1 parent f25b141 commit 9abffef
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void updateDiscordInformation(Set<EventDiscordInformationDto> discordInformation
}

/**
* Removes the discord information for the given channel
* Removes the discord information for the given channel (if present)
*
* @param channelId to remove information for
*/
Expand All @@ -100,4 +100,26 @@ public void removeByChannel(long channelId) {
findEventByChannel(channelId)
.ifPresent(event -> event.getDiscordInformation().removeIf(information -> information.getChannel() == channelId));
}

/**
* Removes the discord information for the given channel and messageId (if present).
* If a discord information is removed, the given runnable is executed.
*
* @param channelId to remove information for
* @param messageId to remove information for
* @param onRemoval to execute if a {@link EventDiscordInformation} is removed
*/
@Async
public void removeByMessage(long channelId, long messageId, Runnable onRemoval) {
findEventByChannel(channelId)
.ifPresent(event -> event.getDiscordInformation().removeIf(information -> {
final boolean match = information.getInfoMsg() == messageId
|| information.getSlotListMsgPartOne() == messageId
|| information.getSlotListMsgPartTwo() == messageId;
if (match) {
onRemoval.run();
}
return match;
}));
}
}
3 changes: 2 additions & 1 deletion src/main/java/de/webalf/slotbot/service/bot/BotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BotService {
private final CommandsService commandsService;
private final MessageSource messageSource;
private final EventDiscordInformationService eventDiscordInformationService;
private final GuildBotService guildBotService;

@Getter
private JDA jda;
Expand All @@ -48,7 +49,7 @@ public void startUp() {
new ReactionAddListener(reactionAddService),
new GuildReadyListener(commandsService),
new InteractionListener(commandClassHelper, messageSource),
new ChannelDeleteListener(eventDiscordInformationService))
new DeleteListener(eventDiscordInformationService, guildBotService, messageSource))
.disableIntents(GUILD_MODERATION, GUILD_EMOJIS_AND_STICKERS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_PRESENCES, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING, SCHEDULED_EVENTS)
.build();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.webalf.slotbot.service.bot.listener;

import de.webalf.slotbot.service.EventDiscordInformationService;
import de.webalf.slotbot.service.bot.GuildBotService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import java.util.Locale;

/**
* @author Alf
* @since 03.04.2023
*/
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Slf4j
public class DeleteListener extends ListenerAdapter {
private final EventDiscordInformationService eventDiscordInformationService;
private final GuildBotService guildBotService;
private final MessageSource messageSource;

@Override
public void onChannelDelete(@NotNull ChannelDeleteEvent event) {
log.trace("Channel {} deleted in guild {}", event.getChannel().getId(), event.getGuild().getId());
eventDiscordInformationService.removeByChannel(event.getChannel().getIdLong());
}

@Override
public void onMessageDelete(@NotNull MessageDeleteEvent event) {
if (!event.isFromGuild()) return;
log.trace("Message {} deleted in channel {} in guild {}", event.getMessageId(), event.getChannel().getId(), event.getGuild().getId());

eventDiscordInformationService.removeByMessage(event.getChannel().getIdLong(), event.getMessageIdLong(), () -> {
final Locale guildLocale = guildBotService.getGuildLocale(event.getGuild().getIdLong());
event.getChannel().sendMessage(messageSource.getMessage("event.discordInformation.broken", null, guildLocale)).queue();
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ bot.embed.event.details.missionType=Mission type
bot.embed.event.details.reserveParticipating=Reserve participates
bot.embed.event.details.schedule.text={0} and lasts {1}
event.slotlist.title=Slotlist
event.discordInformation.broken=An event output message has been deleted. For future updates, the event must be added to the channel again.
3 changes: 2 additions & 1 deletion src/main/resources/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ bot.embed.event.details.schedule=Zeitplan
bot.embed.event.details.missionType=Missionstyp
bot.embed.event.details.reserveParticipating=Reserve nimmt teil
bot.embed.event.details.schedule.text={0} und dauert {1}
event.slotlist.title=Teilnahmeplatzaufzählung
event.slotlist.title=Teilnahmeplatzaufzählung
event.discordInformation.broken=Eine Nachricht der Event-Ausgabe wurde gelöscht. Für zukünftige Aktualisierungen muss das Event erneut dem Kanal hinzugefügt werden.

0 comments on commit 9abffef

Please sign in to comment.