2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2017-06-12 23:40:39 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-07-05 11:17:59 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-06-12 23:40:39 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Administration.Services
|
2017-06-12 23:40:39 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class GuildTimezoneService : INService
|
2017-06-12 23:40:39 +00:00
|
|
|
|
{
|
2017-07-15 13:08:34 +00:00
|
|
|
|
// todo 70 this is a hack >.<
|
2017-07-05 11:17:59 +00:00
|
|
|
|
public static ConcurrentDictionary<ulong, GuildTimezoneService> AllServices { get; } = new ConcurrentDictionary<ulong, GuildTimezoneService>();
|
2017-06-12 23:40:39 +00:00
|
|
|
|
private ConcurrentDictionary<ulong, TimeZoneInfo> _timezones;
|
|
|
|
|
private readonly DbService _db;
|
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public GuildTimezoneService(DiscordSocketClient client, NadekoBot bot, DbService db)
|
2017-06-12 23:40:39 +00:00
|
|
|
|
{
|
2017-10-13 00:21:39 +00:00
|
|
|
|
_timezones = bot.AllGuildConfigs
|
2017-06-12 23:40:39 +00:00
|
|
|
|
.Select(x =>
|
|
|
|
|
{
|
|
|
|
|
TimeZoneInfo tz;
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-03 21:27:17 +00:00
|
|
|
|
if (x.TimeZoneId == null)
|
|
|
|
|
tz = null;
|
|
|
|
|
else
|
|
|
|
|
tz = TimeZoneInfo.FindSystemTimeZoneById(x.TimeZoneId);
|
2017-06-12 23:40:39 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
tz = null;
|
|
|
|
|
}
|
2017-10-13 00:21:39 +00:00
|
|
|
|
return (x.GuildId, Timezone: tz);
|
2017-06-12 23:40:39 +00:00
|
|
|
|
})
|
2017-10-13 00:21:39 +00:00
|
|
|
|
.Where(x => x.Timezone != null)
|
|
|
|
|
.ToDictionary(x => x.GuildId, x => x.Timezone)
|
2017-06-12 23:40:39 +00:00
|
|
|
|
.ToConcurrent();
|
|
|
|
|
|
2017-07-05 11:17:59 +00:00
|
|
|
|
var curUser = client.CurrentUser;
|
|
|
|
|
if (curUser != null)
|
|
|
|
|
AllServices.TryAdd(curUser.Id, this);
|
2017-06-12 23:40:39 +00:00
|
|
|
|
_db = db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TimeZoneInfo GetTimeZoneOrDefault(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
if (_timezones.TryGetValue(guildId, out var tz))
|
|
|
|
|
return tz;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetTimeZone(ulong guildId, TimeZoneInfo tz)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(guildId, set => set);
|
|
|
|
|
|
|
|
|
|
gc.TimeZoneId = tz?.Id;
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
|
|
|
|
if (tz == null)
|
|
|
|
|
_timezones.TryRemove(guildId, out tz);
|
|
|
|
|
else
|
|
|
|
|
_timezones.AddOrUpdate(guildId, tz, (key, old) => tz);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TimeZoneInfo GetTimeZoneOrUtc(ulong guildId)
|
|
|
|
|
=> GetTimeZoneOrDefault(guildId) ?? TimeZoneInfo.Utc;
|
|
|
|
|
}
|
|
|
|
|
}
|