Skip to content

Commit b75809b

Browse files
Log a warning and close inactive ticket if ticket thread is not found
1 parent e962ddf commit b75809b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Nellebot/CommandHandlers/Modmail/CloseInactiveModmailTicketHandler.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
using MediatR;
66
using Nellebot.Common.Models.Modmail;
77
using Nellebot.Data.Repositories;
8+
using Nellebot.Services.Loggers;
89
using Nellebot.Utils;
910

1011
namespace Nellebot.CommandHandlers.Modmail;
1112

1213
public class CloseInactiveModmailTicketHandler : IRequestHandler<CloseInactiveModmailTicketCommand>
1314
{
1415
private readonly ModmailTicketRepository _modmailTicketRepo;
16+
private readonly IDiscordErrorLogger _discordErrorLogger;
1517
private readonly DiscordResolver _resolver;
1618

17-
public CloseInactiveModmailTicketHandler(DiscordResolver resolver, ModmailTicketRepository modmailTicketRepo)
19+
public CloseInactiveModmailTicketHandler(
20+
DiscordResolver resolver,
21+
ModmailTicketRepository modmailTicketRepo,
22+
IDiscordErrorLogger discordErrorLogger)
1823
{
1924
_resolver = resolver;
2025
_modmailTicketRepo = modmailTicketRepo;
26+
_discordErrorLogger = discordErrorLogger;
2127
}
2228

2329
public async Task Handle(CloseInactiveModmailTicketCommand request, CancellationToken cancellationToken)
@@ -27,13 +33,21 @@ public async Task Handle(CloseInactiveModmailTicketCommand request, Cancellation
2733
ModmailTicketPost ticketPost = ticket.TicketPost
2834
?? throw new Exception("The ticket does not have a post channelId");
2935

30-
DiscordThreadChannel threadChannel = _resolver.ResolveThread(ticketPost.ChannelThreadId)
31-
?? throw new Exception("Could not resolve thread channel");
32-
3336
await _modmailTicketRepo.CloseTicket(ticket, cancellationToken);
3437

35-
const string ticketClosureMessage = "This ticket has been closed due to inactivity.";
38+
DiscordThreadChannel? threadChannel = _resolver.ResolveThread(ticketPost.ChannelThreadId);
39+
40+
if (threadChannel is null)
41+
{
42+
_discordErrorLogger.LogWarning(
43+
$"Could not resolve thread channel for ticket id: {ticket.Id}",
44+
$"Thread channel for ticket id: {ticket.Id} could not be resolved. Probably deleted. Closing ticket anyway.");
45+
}
46+
else
47+
{
48+
const string ticketClosureMessage = "This ticket has been closed due to inactivity.";
3649

37-
await threadChannel.SendMessageAsync(ticketClosureMessage);
50+
await threadChannel.SendMessageAsync(ticketClosureMessage);
51+
}
3852
}
3953
}

0 commit comments

Comments
 (0)