using Jellyfin.Plugin.Vanguard.Configuration;
using Jellyfin.Plugin.Vanguard.Services;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Vanguard.Tasks;
/// Every 5 minutes: pull the guild's scheduled events into the persistent history.
public sealed class DiscordSnapshotTask : IScheduledTask
{
private readonly DiscordClient _discord;
private readonly EventStore _store;
private readonly ILogger _log;
public DiscordSnapshotTask(DiscordClient discord, EventStore store, ILogger log) { _discord = discord; _store = store; _log = log; }
public string Name => "Vanguard: snapshot Discord events";
public string Key => "VanguardDiscordSnapshot";
public string Description => "Keeps a history of Discord scheduled events so recordings can be matched to them later.";
public string Category => "Vanguard";
public async Task ExecuteAsync(IProgress progress, CancellationToken ct)
{
var cfg = Plugin.Instance?.Configuration ?? new PluginConfiguration();
if (string.IsNullOrWhiteSpace(cfg.DiscordBotToken) || string.IsNullOrWhiteSpace(cfg.DiscordGuildId)) { _log.LogInformation("Discord not configured; skipping"); return; }
var events = await _discord.GetScheduledEventsAsync(cfg.DiscordBotToken, cfg.DiscordGuildId, ct).ConfigureAwait(false);
_store.Merge(events);
_log.LogInformation("Discord snapshot: {Fresh} current events, {Total} in history", events.Count, _store.Count);
progress.Report(100);
}
public IEnumerable GetDefaultTriggers() =>
[
new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromMinutes(5).Ticks },
new TaskTriggerInfo { Type = TaskTriggerInfoType.StartupTrigger },
];
}