C# Discord bot made using NetCord for keeping a TikTok-style streak

fix: updating streak channel prematurely and error handling on reminders

Minito 84ce75b9 c0498c7b

+30 -10
+26 -9
Services/ChannelService.cs
··· 1 1 using Microsoft.EntityFrameworkCore; 2 + using Microsoft.Extensions.Logging; 2 3 using NetCord.Rest; 3 4 using StreakBot.Data; 4 5 using StreakBot.Data.Entities; ··· 11 12 { 12 13 private readonly StreakDbContext _context; 13 14 private readonly RestClient _restClient; 15 + private readonly ILogger<ChannelService> _logger; 14 16 15 - public ChannelService(StreakDbContext context, RestClient restClient) 17 + public ChannelService(StreakDbContext context, RestClient restClient, ILogger<ChannelService> logger) 16 18 { 17 19 _context = context; 18 20 _restClient = restClient; 21 + _logger = logger; 19 22 } 20 23 21 24 public async Task UpdateStreakChannelAsync(ulong serverId, int streakCount, bool endOfDay = false) ··· 106 109 { 107 110 if (user1NeedsReminder) 108 111 { 109 - var dmChannel = await _restClient.GetDMChannelAsync(user1Id); 110 - var message = new MessageProperties() 111 - .WithContent($"Your streak with <@{user2Id}> will reset in less than an hour! Send a message in the server to keep it alive!"); 112 - await _restClient.SendMessageAsync(dmChannel.Id, message); 112 + try 113 + { 114 + var dmChannel = await _restClient.GetDMChannelAsync(user1Id); 115 + var message = new MessageProperties() 116 + .WithContent($"Your streak with <@{user2Id}> will reset in less than an hour! Send a message in the server to keep it alive!"); 117 + await _restClient.SendMessageAsync(dmChannel.Id, message); 118 + } 119 + catch (RestException) 120 + { 121 + _logger.LogWarning("Failed to send streak reminder DM to user {UserId}", user1Id); 122 + } 113 123 } 114 124 115 125 if (user2NeedsReminder) 116 126 { 117 - var dmChannel = await _restClient.GetDMChannelAsync(user2Id); 118 - var message = new MessageProperties() 119 - .WithContent($"Your streak with <@{user1Id}> will reset in less than an hour! Send a message in the server to keep it alive!"); 120 - await _restClient.SendMessageAsync(dmChannel.Id, message); 127 + try 128 + { 129 + var dmChannel = await _restClient.GetDMChannelAsync(user2Id); 130 + var message = new MessageProperties() 131 + .WithContent($"Your streak with <@{user1Id}> will reset in less than an hour! Send a message in the server to keep it alive!"); 132 + await _restClient.SendMessageAsync(dmChannel.Id, message); 133 + } 134 + catch (RestException) 135 + { 136 + _logger.LogWarning("Failed to send streak reminder DM to user {UserId}", user2Id); 137 + } 121 138 } 122 139 } 123 140
+4 -1
Services/StreakService.cs
··· 35 35 { 36 36 await _context.SaveChangesAsync(); 37 37 38 - await _channelService.UpdateStreakChannelAsync(serverId, streak.StreakNumber); 38 + if (streak.User1MessageSent && streak.User2MessageSent) 39 + { 40 + await _channelService.UpdateStreakChannelAsync(serverId, streak.StreakNumber); 41 + } 39 42 } 40 43 } 41 44