Fixed reminders not running if the reminder was in a thread channel, and fixed reminders (and appeal posts) not pinging

This commit is contained in:
Teriuihi 2023-03-20 04:45:55 +01:00
parent df6d060863
commit 0ae96bd6e5
3 changed files with 56 additions and 23 deletions

View File

@ -3,7 +3,7 @@ package com.alttd.database.queries.QueriesReminders;
import com.alttd.util.Logger; import com.alttd.util.Logger;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.Channel;
public record Reminder (int id, String title, String description, long userId, long guildId, long channelId, public record Reminder (int id, String title, String description, long userId, long guildId, long channelId,
long messageId, boolean shouldRepeat, long creationDate, long remindDate, ReminderType reminderType, byte[] data) { long messageId, boolean shouldRepeat, long creationDate, long remindDate, ReminderType reminderType, byte[] data) {
@ -24,18 +24,20 @@ public record Reminder (int id, String title, String description, long userId, l
reminder.data()); reminder.data());
} }
public TextChannel getChannel(JDA jda) { public Channel getChannel(JDA jda) {
Guild guildById = getGuild(jda); Guild guildById = getGuild(jda);
if (guildById == null) if (guildById == null)
return null; return null;
TextChannel textChannelById = guildById.getTextChannelById(this.channelId); Channel channelById = guildById.getTextChannelById(this.channelId);
if (textChannelById == null) { if (channelById == null)
channelById = guildById.getThreadChannelById(this.channelId);
if (channelById == null) {
Logger.warning("Unable to find text channel for reminder, text channel id: [" + channelId + "]"); Logger.warning("Unable to find text channel for reminder, text channel id: [" + channelId + "]");
return null; return null;
} }
return textChannelById; return channelById;
} }
public Guild getGuild(JDA jda) { public Guild getGuild(JDA jda) {

View File

@ -99,13 +99,15 @@ public class AppealRepost extends ListenerAdapter {
Logger.warning("Unable to get a button for appeals"); Logger.warning("Unable to get a button for appeals");
return; return;
} }
MessageCreateAction messageCreateAction = message.getChannel().sendMessageEmbeds(embed); message.getChannel().sendMessageEmbeds(embed).queue(res -> {
if (member != null)
messageCreateAction = messageCreateAction.mentionUsers(member.getIdLong());
messageCreateAction.queue(res -> {
res.editMessageComponents().setActionRow(reminderAccepted, reminderInProgress, reminderDenied).queue(); res.editMessageComponents().setActionRow(reminderAccepted, reminderInProgress, reminderDenied).queue();
res.createThreadChannel("Appeal").queue(( res.createThreadChannel("Appeal").queue((
threadChannel -> scheduleReminder(res, member, threadChannel)), threadChannel -> {
scheduleReminder(res, member, threadChannel);
if (member != null) {
threadChannel.sendMessage(member.getAsMention() + " you have a new appeal!").queue();
}
}),
failure -> Logger.warning("Unable to create thread channel so won't schedule reminder...")); failure -> Logger.warning("Unable to create thread channel so won't schedule reminder..."));
}); });
message.delete().queue(); message.delete().queue();

View File

@ -8,7 +8,9 @@ import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
@ -82,11 +84,11 @@ public class ReminderScheduler {
@Override @Override
public void run() { public void run() {
long time = new Date().getTime(); long time = System.currentTimeMillis();
while (nextReminder != null && time > nextReminder.remindDate()) { while (nextReminder != null && time > nextReminder.remindDate()) {
TextChannel channel = nextReminder.getChannel(jda); Channel channel = nextReminder.getChannel(jda);
if (channel == null || !channel.canTalk()) { if (channel == null) {
Logger.warning("Unable to run reminder: " + nextReminder.id() + Logger.warning("Couldn't find channel, unable to run reminder: " + nextReminder.id() +
"\ntitle: [" + nextReminder.title() + "\ntitle: [" + nextReminder.title() +
"]\ndescription: [" + nextReminder.description() + "]"); "]\ndescription: [" + nextReminder.description() + "]");
return; return;
@ -114,8 +116,7 @@ public class ReminderScheduler {
removeReminder(nextReminder, true); removeReminder(nextReminder, true);
} }
} }
private void sendEmbed(Reminder reminder, Channel channel) {
private void sendEmbed(Reminder reminder, TextChannel channel) {
EmbedBuilder embedBuilder = new EmbedBuilder() EmbedBuilder embedBuilder = new EmbedBuilder()
.setTitle(reminder.title()) .setTitle(reminder.title())
.setDescription(reminder.description()) .setDescription(reminder.description())
@ -130,11 +131,33 @@ public class ReminderScheduler {
failed -> sendEmbed(reminder, channel, embedBuilder)); failed -> sendEmbed(reminder, channel, embedBuilder));
} }
private void sendEmbed(Reminder reminder, TextChannel channel, EmbedBuilder embedBuilder, Member member) { private MessageCreateAction getCreateAction(Channel channel, EmbedBuilder embedBuilder) {
switch (channel.getType()) {
case TEXT, NEWS, FORUM -> {
if (channel instanceof TextChannel textChannel) {
return textChannel.sendMessageEmbeds(embedBuilder.build());
}
}
case GUILD_NEWS_THREAD, GUILD_PUBLIC_THREAD, GUILD_PRIVATE_THREAD -> {
if (channel instanceof ThreadChannel threadChannel) {
return threadChannel.sendMessageEmbeds(embedBuilder.build());
}
}
default -> {
Logger.warning("Received unexpected channel type " + channel.getType() + " can't send reminder...");
}
}
return null;
}
private void sendEmbed(Reminder reminder, Channel channel, EmbedBuilder embedBuilder, Member member) {
embedBuilder.setAuthor(member.getEffectiveName(), null, member.getEffectiveAvatarUrl()); embedBuilder.setAuthor(member.getEffectiveName(), null, member.getEffectiveAvatarUrl());
switch (reminder.reminderType()) { switch (reminder.reminderType()) {
case NONE, MANUAL -> { case NONE, MANUAL -> {
channel.sendMessageEmbeds(embedBuilder.build()).queue(RestAction.getDefaultSuccess(), Util::handleFailure); MessageCreateAction createAction = getCreateAction(channel, embedBuilder);
if (createAction == null)
return;
createAction.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
} }
case APPEAL -> { case APPEAL -> {
if (reminder.data() == null) if (reminder.data() == null)
@ -153,17 +176,23 @@ public class ReminderScheduler {
e.printStackTrace(); e.printStackTrace();
} }
} }
MessageCreateAction messageCreateAction = channel.sendMessageEmbeds(embedBuilder.build()); MessageCreateAction messageCreateAction = getCreateAction(channel, embedBuilder);
if (userId != 0) if (messageCreateAction == null)
messageCreateAction = messageCreateAction.mentionUsers(userId); return;
if (userId != 0) {
messageCreateAction.addContent("<@" + userId + ">");
}
messageCreateAction.queue(RestAction.getDefaultSuccess(), Util::handleFailure); messageCreateAction.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
} }
} }
} }
private void sendEmbed(Reminder reminder, TextChannel channel, EmbedBuilder embedBuilder) { private void sendEmbed(Reminder reminder, Channel channel, EmbedBuilder embedBuilder) {
embedBuilder.setAuthor(reminder.userId() + ""); embedBuilder.setAuthor(reminder.userId() + "");
channel.sendMessageEmbeds(embedBuilder.build()).queue(RestAction.getDefaultSuccess(), Util::handleFailure); MessageCreateAction createAction = getCreateAction(channel, embedBuilder);
if (createAction == null)
return;
createAction.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
} }
} }
} }