Database overhaul to fix #3
All checks were successful
continuous-integration/drone/push Build is passing

We are now tracking whether messages are sent privately on in a guild, and acting accordingly.
This commit is contained in:
2022-11-21 19:07:34 +01:00
parent 0aec543a46
commit da511f2913
5 changed files with 124 additions and 48 deletions

View File

@@ -2,7 +2,9 @@ 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.channel.concrete.TextChannel;
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;
import wtf.beatrice.hidekobot.Configuration;
@@ -71,20 +73,49 @@ public class ExpiredMessageTask implements Runnable {
private void disableExpired(String messageId)
{
String guildId = databaseManager.getQueuedExpiringMessageGuild(messageId);
String channelId = databaseManager.getQueuedExpiringMessageChannel(messageId);
ChannelType msgChannelType = databaseManager.getTrackedMessageChannelType(messageId);
MessageChannel textChannel = null;
Guild guild = HidekoBot.getAPI().getGuildById(guildId);
if(guild == null)
// this should never happen, but only message channels are supported.
if(!msgChannelType.isMessage())
{
// if guild is not found, consider it expired
// (server was deleted or bot was kicked)
databaseManager.untrackExpiredMessage(messageId);
return;
}
TextChannel textChannel = guild.getTextChannelById(channelId);
// if this is a DM
if(msgChannelType == ChannelType.PRIVATE)
{
String userId = databaseManager.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)
databaseManager.untrackExpiredMessage(messageId);
return;
}
textChannel = user.openPrivateChannel().complete();
}
else
{
String guildId = databaseManager.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)
databaseManager.untrackExpiredMessage(messageId);
return;
}
textChannel = guild.getTextChannelById(channelId);
}
if(textChannel == null)
{
// if channel is not found, count it as expired