NadekoBot/NadekoBot.Modules.Utility/Services/MessageRepeaterService.cs

43 lines
1.6 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using Discord.WebSocket;
using NadekoBot.Modules.Utility.Common;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Modules.Utility.Services
{
public class MessageRepeaterService : INService
{
//messagerepeater
//guildid/RepeatRunners
public ConcurrentDictionary<ulong, ConcurrentQueue<RepeatRunner>> Repeaters { get; set; }
public bool RepeaterReady { get; private set; }
public MessageRepeaterService(NadekoBot bot, DiscordSocketClient client)
{
var _ = Task.Run(async () =>
{
await bot.Ready.Task.ConfigureAwait(false);
2017-06-12 23:40:39 +00:00
Repeaters = new ConcurrentDictionary<ulong, ConcurrentQueue<RepeatRunner>>(
bot.AllGuildConfigs
.Select(gc =>
{
var guild = client.GetGuild(gc.GuildId);
if (guild == null)
return (0, null);
return (gc.GuildId, new ConcurrentQueue<RepeatRunner>(gc.GuildRepeaters
.Select(gr => new RepeatRunner(client, guild, gr))
.Where(x => x.Guild != null)));
})
.Where(x => x.Item2 != null)
.ToDictionary(x => x.GuildId, x => x.Item2));
RepeaterReady = true;
});
}
}
}