public async Task SetJanitor([Remainder] TimeSpan messageDuration) { if (messageDuration < TimeSpan.FromMinutes(1)) { await ReplyAsync("Duration must be at least 1 minute long!"); return; } using (Context.Channel.EnterTypingState()) { if (!StaticBase.ChannelJanitors.ContainsKey(Context.Channel.Id)) { var janitor = new ChannelJanitor(Context.Channel.Id, messageDuration); await ChannelJanitor.InsertToDBAsync(janitor); StaticBase.ChannelJanitors.Add(Context.Channel.Id, janitor); await ReplyAsync($"Added janitor with timespan: {messageDuration.ToString(@"d\d\ h\h\ m\m\ s\s")}\n**This will only check the most recent 100 messages starting from now!**"); } else { var janitor = StaticBase.ChannelJanitors[Context.Channel.Id]; janitor.MessageDuration = messageDuration; janitor.NextCheck = DateTime.UtcNow.AddMinutes(1); await janitor.SetTimer(); await ReplyAsync($"Replaced janitor timespan: {messageDuration.ToString(@"d\d\ h\h\ m\m\ s\s")}"); } } }
public async Task RemoveJanitor() { using (Context.Channel.EnterTypingState()) { bool worked = StaticBase.ChannelJanitors.TryGetValue(Context.Channel.Id, out ChannelJanitor janitor); if (worked) { await ChannelJanitor.RemoveFromDBAsync(janitor); StaticBase.ChannelJanitors.Remove(Context.Channel.Id); await ReplyAsync("Removed janitor for this channel."); } else { await ReplyAsync("Could not find a janitor service for this channel."); } } }
public async Task Prune(bool testing = true) { using (Context.Channel.EnterTypingState()) { var toCheck = StaticBase.ChannelJanitors.Keys; var toPrune = toCheck.Select(x => Tuple.Create(x, Program.Client.GetChannel(x) == null)); if (!testing) { foreach (var channel in toPrune.Where(x => x.Item2).Select(x => x.Item1).ToList()) { bool worked = StaticBase.ChannelJanitors.TryGetValue(channel, out ChannelJanitor janitor); if (worked) { await ChannelJanitor.RemoveFromDBAsync(janitor); StaticBase.ChannelJanitors.Remove(channel); } } } await ReplyAsync($"Pruned {toPrune.Where(x => x.Item2).Count()} objects"); } }