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 net.dv8tion.jda.api.JDA;
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,
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());
}
public TextChannel getChannel(JDA jda) {
public Channel getChannel(JDA jda) {
Guild guildById = getGuild(jda);
if (guildById == null)
return null;
TextChannel textChannelById = guildById.getTextChannelById(this.channelId);
if (textChannelById == null) {
Channel channelById = guildById.getTextChannelById(this.channelId);
if (channelById == null)
channelById = guildById.getThreadChannelById(this.channelId);
if (channelById == null) {
Logger.warning("Unable to find text channel for reminder, text channel id: [" + channelId + "]");
return null;
}
return textChannelById;
return channelById;
}
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");
return;
}
MessageCreateAction messageCreateAction = message.getChannel().sendMessageEmbeds(embed);
if (member != null)
messageCreateAction = messageCreateAction.mentionUsers(member.getIdLong());
messageCreateAction.queue(res -> {
message.getChannel().sendMessageEmbeds(embed).queue(res -> {
res.editMessageComponents().setActionRow(reminderAccepted, reminderInProgress, reminderDenied).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..."));
});
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.entities.Guild;
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.ThreadChannel;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
@ -82,11 +84,11 @@ public class ReminderScheduler {
@Override
public void run() {
long time = new Date().getTime();
long time = System.currentTimeMillis();
while (nextReminder != null && time > nextReminder.remindDate()) {
TextChannel channel = nextReminder.getChannel(jda);
if (channel == null || !channel.canTalk()) {
Logger.warning("Unable to run reminder: " + nextReminder.id() +
Channel channel = nextReminder.getChannel(jda);
if (channel == null) {
Logger.warning("Couldn't find channel, unable to run reminder: " + nextReminder.id() +
"\ntitle: [" + nextReminder.title() +
"]\ndescription: [" + nextReminder.description() + "]");
return;
@ -114,8 +116,7 @@ public class ReminderScheduler {
removeReminder(nextReminder, true);
}
}
private void sendEmbed(Reminder reminder, TextChannel channel) {
private void sendEmbed(Reminder reminder, Channel channel) {
EmbedBuilder embedBuilder = new EmbedBuilder()
.setTitle(reminder.title())
.setDescription(reminder.description())
@ -130,11 +131,33 @@ public class ReminderScheduler {
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());
switch (reminder.reminderType()) {
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 -> {
if (reminder.data() == null)
@ -153,17 +176,23 @@ public class ReminderScheduler {
e.printStackTrace();
}
}
MessageCreateAction messageCreateAction = channel.sendMessageEmbeds(embedBuilder.build());
if (userId != 0)
messageCreateAction = messageCreateAction.mentionUsers(userId);
MessageCreateAction messageCreateAction = getCreateAction(channel, embedBuilder);
if (messageCreateAction == null)
return;
if (userId != 0) {
messageCreateAction.addContent("<@" + userId + ">");
}
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() + "");
channel.sendMessageEmbeds(embedBuilder.build()).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
MessageCreateAction createAction = getCreateAction(channel, embedBuilder);
if (createAction == null)
return;
createAction.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
}
}
}