HidekoBot/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java

158 lines
5.2 KiB
Java
Raw Normal View History

2022-11-21 15:02:40 +01:00
package wtf.beatrice.hidekobot.runnables;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
import net.dv8tion.jda.api.requests.RestAction;
2022-11-21 20:20:11 +01:00
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
2022-11-22 00:28:33 +01:00
import wtf.beatrice.hidekobot.datasource.DatabaseSource;
2022-11-21 15:02:40 +01:00
import wtf.beatrice.hidekobot.utils.Logger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
2022-11-21 15:02:40 +01:00
public class ExpiredMessageTask implements Runnable {
private final DateTimeFormatter formatter;
private final Logger logger;
2022-11-22 00:28:33 +01:00
private DatabaseSource databaseSource;
2022-11-21 15:02:40 +01:00
public ExpiredMessageTask()
{
2022-11-21 20:20:11 +01:00
String format = Cache.getExpiryTimestampFormat();
formatter = DateTimeFormatter.ofPattern(format);
2022-11-22 00:28:33 +01:00
databaseSource = Cache.getDatabaseSource();
logger = new Logger(getClass());
}
@Override
public void run() {
2022-11-22 00:28:33 +01:00
databaseSource = Cache.getDatabaseSource();
if(databaseSource == null) return;
2022-11-22 00:28:33 +01:00
List<String> expiringMessages = Cache.getDatabaseSource().getQueuedExpiringMessages();
if(expiringMessages == null || expiringMessages.isEmpty()) return;
LocalDateTime now = LocalDateTime.now();
for(String messageId : expiringMessages)
{
2022-11-21 11:19:23 +01:00
2022-11-21 20:20:11 +01:00
if(Cache.isVerbose()) logger.log("expired check: " + messageId);
2022-11-21 11:19:23 +01:00
2022-11-22 00:28:33 +01:00
String expiryTimestamp = databaseSource.getQueuedExpiringMessageExpiryDate(messageId);
if(expiryTimestamp == null || expiryTimestamp.equals("")) // if missing timestamp
{
// count it as already expired
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
// move on to next message
continue;
}
LocalDateTime expiryDate = LocalDateTime.parse(expiryTimestamp, formatter);
if(now.isAfter(expiryDate))
{
2022-11-21 20:20:11 +01:00
if(Cache.isVerbose()) logger.log("expired: " + messageId);
disableExpired(messageId);
}
}
}
private void disableExpired(String messageId)
{
2022-11-22 00:28:33 +01:00
String channelId = databaseSource.getQueuedExpiringMessageChannel(messageId);
2022-11-22 00:28:33 +01:00
ChannelType msgChannelType = databaseSource.getTrackedMessageChannelType(messageId);
MessageChannel textChannel = null;
// this should never happen, but only message channels are supported.
if(!msgChannelType.isMessage())
{
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
return;
}
// if this is a DM
if(msgChannelType == ChannelType.PRIVATE)
{
2022-11-22 00:28:33 +01:00
String userId = databaseSource.getTrackedReplyUserId(messageId);
User user = HidekoBot.getAPI().retrieveUserById(userId).complete();
if(user == null)
{
// if user is not found, consider it expired
// (deleted profile, or blocked the bot)
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
return;
}
textChannel = user.openPrivateChannel().complete();
}
else
{
2022-11-22 00:28:33 +01:00
String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId);
Guild guild = HidekoBot.getAPI().getGuildById(guildId);
if(guild == null)
{
// if guild is not found, consider it expired
// (server was deleted or bot was kicked)
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
return;
}
textChannel = guild.getTextChannelById(channelId);
}
if(textChannel == null)
{
// if channel is not found, count it as expired
// (channel was deleted or bot permissions restricted)
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
return;
}
RestAction<Message> retrieveAction = textChannel.retrieveMessageById(messageId);
2022-11-21 20:20:11 +01:00
if(Cache.isVerbose()) logger.log("cleaning up: " + messageId);
retrieveAction.queue(
message -> {
if(message == null)
{
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
return;
}
List<LayoutComponent> components = message.getComponents();
List<LayoutComponent> newComponents = new ArrayList<>();
for (LayoutComponent component : components)
{
component = component.asDisabled();
newComponents.add(component);
}
message.editMessageComponents(newComponents).queue();
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
},
(error) -> {
2022-11-22 00:28:33 +01:00
databaseSource.untrackExpiredMessage(messageId);
});
}
}