diff --git a/src/NadekoBot/Modules/Searches/StreamNotification/StreamNotificationService.cs b/src/NadekoBot/Modules/Searches/StreamNotification/StreamNotificationService.cs index fccff7d4b..ed7896e82 100644 --- a/src/NadekoBot/Modules/Searches/StreamNotification/StreamNotificationService.cs +++ b/src/NadekoBot/Modules/Searches/StreamNotification/StreamNotificationService.cs @@ -1,6 +1,4 @@ #nullable disable -using LinqToDB; -using LinqToDB.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Nadeko.Common; using NadekoBot.Common.ModuleBehaviors; @@ -12,95 +10,6 @@ using NadekoBot.Services.Database.Models; namespace NadekoBot.Modules.Searches.Services; -public sealed class StreamOnlineMessageDeleterService : INService, IReadyExecutor -{ - private readonly StreamNotificationService _notifService; - private readonly DbService _db; - private readonly DiscordSocketClient _client; - private readonly IPubSub _pubSub; - - public StreamOnlineMessageDeleterService( - StreamNotificationService notifService, - DbService db, - IPubSub pubSub, - DiscordSocketClient client) - { - _notifService = notifService; - _db = db; - _client = client; - _pubSub = pubSub; - } - - public async Task OnReadyAsync() - { - _notifService.OnlineMessagesSent += OnOnlineMessagesSent; - - if(_client.ShardId == 0) - await _pubSub.Sub(_notifService.StreamsOfflineKey, OnStreamsOffline); - } - - private async Task OnOnlineMessagesSent(FollowedStream.FType type, string name, IReadOnlyCollection<(ulong, ulong)> pairs) - { - await using var ctx = _db.GetDbContext(); - foreach (var (channelId, messageId) in pairs) - { - await ctx.GetTable() - .InsertAsync(() => new() - { - Name = name, - Type = type, - MessageId = messageId, - ChannelId = channelId, - DateAdded = DateTime.UtcNow, - }); - } - } - - private async ValueTask OnStreamsOffline(List streamDatas) - { - if (_client.ShardId != 0) - return; - - var pairs = await GetMessagesToDelete(streamDatas); - - foreach (var (channelId, messageId) in pairs) - { - try - { - var textChannel = await _client.GetChannelAsync(channelId) as ITextChannel; - if (textChannel is null) - continue; - - await textChannel.DeleteMessageAsync(messageId); - } - catch - { - continue; - } - } - } - - private async Task> GetMessagesToDelete(List streamDatas) - { - await using var ctx = _db.GetDbContext(); - - var toReturn = new List<(ulong, ulong)>(); - foreach (var sd in streamDatas) - { - var key = sd.CreateKey(); - var toDelete = await ctx.GetTable() - .Where(x => (x.Type == key.Type && x.Name == key.Name) - || Sql.DateDiff(Sql.DateParts.Day, x.DateAdded, DateTime.UtcNow) > 1) - .DeleteWithOutputAsync(); - - toReturn.AddRange(toDelete.Select(x => (x.ChannelId, x.MessageId))); - } - - return toReturn; - } -} - - public sealed class StreamNotificationService : INService, IReadyExecutor { private readonly DbService _db; @@ -370,7 +279,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor var message = string.IsNullOrWhiteSpace(fs.Message) ? "" : rep.Replace(fs.Message); - var msg = await textChannel.EmbedAsync(GetEmbed(fs.GuildId, stream), message); + var msg = await textChannel.EmbedAsync(GetEmbed(fs.GuildId, stream, false), message); // only cache the ids of channel/message pairs if(_deleteOnOfflineServers.Contains(fs.GuildId)) @@ -563,18 +472,22 @@ public sealed class StreamNotificationService : INService, IReadyExecutor return data; } - public IEmbedBuilder GetEmbed(ulong guildId, StreamData status) + public IEmbedBuilder GetEmbed(ulong guildId, StreamData status, bool showViewers = true) { var embed = _eb.Create() .WithTitle(status.Name) .WithUrl(status.StreamUrl) .WithDescription(status.StreamUrl) - .AddField(GetText(guildId, strs.status), status.IsLive ? "🟢 Online" : "🔴 Offline", true) - .AddField(GetText(guildId, strs.viewers), - status.Viewers == 0 && !status.IsLive - ? "-" - : status.Viewers, - true); + .AddField(GetText(guildId, strs.status), status.IsLive ? "🟢 Online" : "🔴 Offline", true); + + if (showViewers) + { + embed.AddField(GetText(guildId, strs.viewers), + status.Viewers == 0 && !status.IsLive + ? "-" + : status.Viewers, + true); + } if (status.IsLive) embed = embed.WithOkColor(); diff --git a/src/NadekoBot/Modules/Searches/StreamNotification/StreamOnlineMessageDeleterService.cs b/src/NadekoBot/Modules/Searches/StreamNotification/StreamOnlineMessageDeleterService.cs new file mode 100644 index 000000000..dbcd3b1cf --- /dev/null +++ b/src/NadekoBot/Modules/Searches/StreamNotification/StreamOnlineMessageDeleterService.cs @@ -0,0 +1,99 @@ +#nullable disable +using LinqToDB; +using LinqToDB.EntityFrameworkCore; +using NadekoBot.Common.ModuleBehaviors; +using NadekoBot.Db.Models; +using NadekoBot.Modules.Searches.Common; + +namespace NadekoBot.Modules.Searches.Services; + +public sealed class StreamOnlineMessageDeleterService : INService, IReadyExecutor +{ + private readonly StreamNotificationService _notifService; + private readonly DbService _db; + private readonly DiscordSocketClient _client; + private readonly IPubSub _pubSub; + + public StreamOnlineMessageDeleterService( + StreamNotificationService notifService, + DbService db, + IPubSub pubSub, + DiscordSocketClient client) + { + _notifService = notifService; + _db = db; + _client = client; + _pubSub = pubSub; + } + + public async Task OnReadyAsync() + { + _notifService.OnlineMessagesSent += OnOnlineMessagesSent; + + if (_client.ShardId == 0) + await _pubSub.Sub(_notifService.StreamsOfflineKey, OnStreamsOffline); + } + + private async Task OnOnlineMessagesSent( + FollowedStream.FType type, + string name, + IReadOnlyCollection<(ulong, ulong)> pairs) + { + await using var ctx = _db.GetDbContext(); + foreach (var (channelId, messageId) in pairs) + { + await ctx.GetTable() + .InsertAsync(() => new() + { + Name = name, + Type = type, + MessageId = messageId, + ChannelId = channelId, + DateAdded = DateTime.UtcNow, + }); + } + } + + private async ValueTask OnStreamsOffline(List streamDatas) + { + if (_client.ShardId != 0) + return; + + var pairs = await GetMessagesToDelete(streamDatas); + + foreach (var (channelId, messageId) in pairs) + { + try + { + var textChannel = await _client.GetChannelAsync(channelId) as ITextChannel; + if (textChannel is null) + continue; + + await textChannel.DeleteMessageAsync(messageId); + } + catch + { + continue; + } + } + } + + private async Task> GetMessagesToDelete(List streamDatas) + { + await using var ctx = _db.GetDbContext(); + + var toReturn = new List<(ulong, ulong)>(); + foreach (var sd in streamDatas) + { + var key = sd.CreateKey(); + var toDelete = await ctx.GetTable() + .Where(x => (x.Type == key.Type && x.Name == key.Name) + || Sql.DateDiff(Sql.DateParts.Day, x.DateAdded, DateTime.UtcNow) > 1) + .DeleteWithOutputAsync(); + + toReturn.AddRange(toDelete.Select(x => (x.ChannelId, x.MessageId))); + } + + return toReturn; + } +} \ No newline at end of file