2022-11-21 15:02:40 +01:00
|
|
|
package wtf.beatrice.hidekobot.runnables;
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
import net.dv8tion.jda.api.entities.Guild;
|
|
|
|
import net.dv8tion.jda.api.entities.Message;
|
2022-11-21 19:07:34 +01:00
|
|
|
import net.dv8tion.jda.api.entities.User;
|
|
|
|
import net.dv8tion.jda.api.entities.channel.ChannelType;
|
|
|
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
2022-11-21 00:14:13 +01:00
|
|
|
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;
|
2022-11-21 00:14:13 +01:00
|
|
|
import wtf.beatrice.hidekobot.HidekoBot;
|
2022-11-22 23:44:34 +01:00
|
|
|
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
2022-11-22 00:31:52 +01:00
|
|
|
import wtf.beatrice.hidekobot.util.Logger;
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
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 {
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
private final DateTimeFormatter formatter;
|
|
|
|
private final Logger logger;
|
2022-11-22 00:28:33 +01:00
|
|
|
private DatabaseSource databaseSource;
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
|
2022-11-21 15:02:40 +01:00
|
|
|
public ExpiredMessageTask()
|
2022-11-21 00:14:13 +01:00
|
|
|
{
|
2022-11-21 20:20:11 +01:00
|
|
|
String format = Cache.getExpiryTimestampFormat();
|
2022-11-21 00:14:13 +01:00
|
|
|
formatter = DateTimeFormatter.ofPattern(format);
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource = Cache.getDatabaseSource();
|
2022-11-21 00:14:13 +01:00
|
|
|
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-21 00:14:13 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
List<String> expiringMessages = Cache.getDatabaseSource().getQueuedExpiringMessages();
|
2022-11-21 00:14:13 +01:00
|
|
|
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);
|
2022-11-21 11:23:52 +01:00
|
|
|
if(expiryTimestamp == null || expiryTimestamp.equals("")) // if missing timestamp
|
|
|
|
{
|
|
|
|
// count it as already expired
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource.untrackExpiredMessage(messageId);
|
2022-11-21 11:23:52 +01:00
|
|
|
// move on to next message
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
2022-11-21 00:14:13 +01:00
|
|
|
disableExpired(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disableExpired(String messageId)
|
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
String channelId = databaseSource.getQueuedExpiringMessageChannel(messageId);
|
2022-11-21 00:14:13 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
ChannelType msgChannelType = databaseSource.getTrackedMessageChannelType(messageId);
|
2022-11-21 00:14:13 +01:00
|
|
|
|
2022-11-21 19:07:34 +01:00
|
|
|
MessageChannel textChannel = null;
|
2022-11-21 00:14:13 +01:00
|
|
|
|
2022-11-21 19:07:34 +01:00
|
|
|
|
|
|
|
// this should never happen, but only message channels are supported.
|
|
|
|
if(!msgChannelType.isMessage())
|
2022-11-21 11:23:52 +01:00
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource.untrackExpiredMessage(messageId);
|
2022-11-21 11:23:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-11-21 19:07:34 +01:00
|
|
|
|
|
|
|
// if this is a DM
|
2022-11-22 17:08:31 +01:00
|
|
|
if(!(msgChannelType.isGuild()))
|
2022-11-21 19:07:34 +01:00
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
String userId = databaseSource.getTrackedReplyUserId(messageId);
|
2022-11-21 19:07:34 +01:00
|
|
|
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);
|
2022-11-21 19:07:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
textChannel = user.openPrivateChannel().complete();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId);
|
2022-11-21 19:07:34 +01:00
|
|
|
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);
|
2022-11-21 19:07:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
textChannel = guild.getTextChannelById(channelId);
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:23:52 +01:00
|
|
|
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);
|
2022-11-21 11:23:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
RestAction<Message> retrieveAction = textChannel.retrieveMessageById(messageId);
|
|
|
|
|
|
|
|
|
2022-11-21 20:20:11 +01:00
|
|
|
if(Cache.isVerbose()) logger.log("cleaning up: " + messageId);
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
retrieveAction.queue(
|
|
|
|
|
|
|
|
message -> {
|
2022-11-21 11:23:52 +01:00
|
|
|
if(message == null)
|
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource.untrackExpiredMessage(messageId);
|
2022-11-21 11:23:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
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);
|
2022-11-21 00:14:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
(error) -> {
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource.untrackExpiredMessage(messageId);
|
2022-11-21 00:14:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|