|
@ -9,6 +9,7 @@ using System.Timers; |
|
|
using System.Xml; |
|
|
using System.Xml; |
|
|
using Discord; |
|
|
using Discord; |
|
|
using Discord.WebSocket; |
|
|
using Discord.WebSocket; |
|
|
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
|
|
|
namespace DiscoBot.bdaygreet |
|
|
namespace DiscoBot.bdaygreet |
|
|
{ |
|
|
{ |
|
@ -17,7 +18,7 @@ namespace DiscoBot.bdaygreet |
|
|
string IModule.Name { get => "BDayGreet"; set => throw new NotImplementedException(); } |
|
|
string IModule.Name { get => "BDayGreet"; set => throw new NotImplementedException(); } |
|
|
private SocketGuild guild; |
|
|
private SocketGuild guild; |
|
|
private BDayGreetContext bdayGreetContext; |
|
|
private BDayGreetContext bdayGreetContext; |
|
|
private Dictionary<string, Timer> timers = new Dictionary<string, Timer>(); |
|
|
private Timer updateTimer = new Timer(); |
|
|
private List<WebSocket> webSockets = new List<WebSocket>(); |
|
|
private List<WebSocket> webSockets = new List<WebSocket>(); |
|
|
|
|
|
|
|
|
public Dictionary<string, Func<SocketMessage, string[], Task>> Commands { get; set; } = new Dictionary<string, Func<SocketMessage, string[], Task>>(); |
|
|
public Dictionary<string, Func<SocketMessage, string[], Task>> Commands { get; set; } = new Dictionary<string, Func<SocketMessage, string[], Task>>(); |
|
@ -27,10 +28,7 @@ namespace DiscoBot.bdaygreet |
|
|
this.guild = guild; |
|
|
this.guild = guild; |
|
|
bdayGreetContext = new BDayGreetContext(guild.Id); |
|
|
bdayGreetContext = new BDayGreetContext(guild.Id); |
|
|
bdayGreetContext.Database.EnsureCreated(); |
|
|
bdayGreetContext.Database.EnsureCreated(); |
|
|
foreach(var f in bdayGreetContext.BDays) |
|
|
InitializeCheck(); |
|
|
{ |
|
|
|
|
|
//InitializeFeed(f);
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Commands.Add("bdayadd", HandleBDayAddCommand); |
|
|
Commands.Add("bdayadd", HandleBDayAddCommand); |
|
|
Commands.Add("bdaydel", HandleBDayDelCommand); |
|
|
Commands.Add("bdaydel", HandleBDayDelCommand); |
|
@ -38,24 +36,32 @@ namespace DiscoBot.bdaygreet |
|
|
Commands.Add("bdaydebug", HandleBDayDebugCommand); |
|
|
Commands.Add("bdaydebug", HandleBDayDebugCommand); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
private void SetTimer() |
|
|
private Task InitializeFeed(BDayUser feed) |
|
|
|
|
|
{ |
|
|
{ |
|
|
Console.WriteLine("Found feed " + feed.Name); |
|
|
updateTimer.Dispose(); |
|
|
|
|
|
var now = DateTime.Now; |
|
|
Timer timer = new Timer(feed.CheckInterval.TotalMilliseconds); |
|
|
//DateTimeOffset next = now.AddHours(1).AddMinutes(-now.Minute).AddSeconds(-now.Second);
|
|
|
timer.AutoReset = true; |
|
|
DateTimeOffset next = now.AddMinutes(1).AddSeconds(-now.Second); |
|
|
timer.Elapsed += async (sender, e) => |
|
|
var span = next - now; |
|
|
|
|
|
Console.WriteLine("Next bday timer is: " + next + " in " + span.TotalMilliseconds + "ms."); |
|
|
|
|
|
updateTimer = new Timer(span.TotalMilliseconds); |
|
|
|
|
|
updateTimer.AutoReset = false; |
|
|
|
|
|
updateTimer.Elapsed += async (sender, e) => |
|
|
{ |
|
|
{ |
|
|
await Task.Run(() => HandleBDayCheck(feed)); |
|
|
await Task.Run(() => HandleBDayCheck()); |
|
|
}; |
|
|
}; |
|
|
timer.Start(); |
|
|
updateTimer.Start(); |
|
|
timers.Add(feed.Name, timer); |
|
|
} |
|
|
|
|
|
|
|
|
HandleBDayCheck(feed); |
|
|
private Task InitializeCheck() |
|
|
|
|
|
{ |
|
|
|
|
|
Console.WriteLine("Starting check timer!"); |
|
|
|
|
|
SetTimer(); |
|
|
|
|
|
HandleBDayCheck(); |
|
|
return Task.CompletedTask; |
|
|
return Task.CompletedTask; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
private Task DeinitializeFeed(BDayFeed feed) |
|
|
private Task DeinitializeFeed(BDayFeed feed) |
|
|
{ |
|
|
{ |
|
|
Timer t = timers[feed.Name]; |
|
|
Timer t = timers[feed.Name]; |
|
@ -63,48 +69,42 @@ namespace DiscoBot.bdaygreet |
|
|
timers.Remove(feed.Name); |
|
|
timers.Remove(feed.Name); |
|
|
return Task.CompletedTask; |
|
|
return Task.CompletedTask; |
|
|
} |
|
|
} |
|
|
|
|
|
*/ |
|
|
private Task HandleBDayCheck(BDayFeed feed) |
|
|
private async Task HandleBDayCheck() |
|
|
{ |
|
|
{ |
|
|
SocketTextChannel c = guild.Channels.Where(g => g.Id == feed.Channel).Single() as SocketTextChannel; |
|
|
Console.WriteLine("Checktime!"); |
|
|
c.SendMessageAsync("Yes yes i will check."); |
|
|
var bdays = bdayGreetContext.BDays; |
|
|
|
|
|
foreach(var bday in bdays) |
|
|
try |
|
|
|
|
|
{ |
|
|
{ |
|
|
XmlReader reader = XmlReader.Create(feed.Url); |
|
|
SocketTextChannel c = guild.Channels.Where(g => g.Id == bday.Channel).Single() as SocketTextChannel; |
|
|
SyndicationFeed f = SyndicationFeed.Load(reader); |
|
|
//await c.SendMessageAsync("Checking " + bday.DiscordTag + "'s birthday. NextGreet: "+bday.NextGreet);
|
|
|
DateTimeOffset newestItem = feed.LastChecked; |
|
|
var now = DateTimeOffset.Now; |
|
|
foreach(var item in f.Items) |
|
|
if(bday.NextGreet < now) |
|
|
{ |
|
|
{ |
|
|
if (item.LastUpdatedTime <= feed.LastChecked) |
|
|
var call = ""; |
|
|
continue; |
|
|
try |
|
|
Console.WriteLine("{0}: {1} | {2}", feed.Name, item.LastUpdatedTime, feed.LastChecked); |
|
|
|
|
|
List<string> m = new List<string>(); |
|
|
|
|
|
m.Add("<" + feed.Name + "> " + item.Title.Text); |
|
|
|
|
|
if(item.LastUpdatedTime > newestItem) |
|
|
|
|
|
{ |
|
|
{ |
|
|
Console.WriteLine("^-- UPDATE!"); |
|
|
call = guild.Users.Where(gu => (gu.Username + "#" + gu.DiscriminatorValue) == bday.DiscordTag).Single().Mention; |
|
|
newestItem = item.LastUpdatedTime; |
|
|
|
|
|
} |
|
|
} |
|
|
foreach (var l in item.Links) |
|
|
catch (InvalidOperationException e) |
|
|
{ |
|
|
{ |
|
|
m.Add(l.Uri.ToString()); |
|
|
// Too spammy, do something about it? Don't rely on u.User
|
|
|
} |
|
|
//c.SendMessageAsync("Could not find User "+ u.DiscordTag+" in Discord, cannot mention.");
|
|
|
c.SendMessageAsync(string.Join(" | ", m)); |
|
|
call = bday.DiscordTag; |
|
|
} |
|
|
} |
|
|
feed.LastChecked = newestItem; |
|
|
if (bday.NextGreet.Day == now.Day && bday.NextGreet.Month == now.Month) |
|
|
rssContext.SaveChangesAsync(); |
|
|
|
|
|
} catch(System.Net.WebException e) |
|
|
|
|
|
{ |
|
|
{ |
|
|
c.SendMessageAsync("<" + feed.Name + "> " + "NetworkFailure: " + e.Message); |
|
|
await c.SendMessageAsync("Today is "+call+"'s birthday! Happy Birthday " + call+"!"); |
|
|
} catch(XmlException e) |
|
|
} else |
|
|
{ |
|
|
{ |
|
|
c.SendMessageAsync("<" + feed.Name + "> " + "Malformed Response: " + e.Message); |
|
|
await c.SendMessageAsync("Happy belated Birthday " + call+"! I guess I was offline when it happened. :("); |
|
|
} |
|
|
} |
|
|
|
|
|
bday.NextGreet = bday.NextGreet.AddYears(1); |
|
|
return Task.CompletedTask; |
|
|
bdayGreetContext.SaveChanges(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
SetTimer(); |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
private async Task HandleBDayAddCommand(SocketMessage msg, string[] parameters) |
|
|
private async Task HandleBDayAddCommand(SocketMessage msg, string[] parameters) |
|
|
{ |
|
|
{ |
|
@ -130,25 +130,51 @@ namespace DiscoBot.bdaygreet |
|
|
} |
|
|
} |
|
|
DateTimeOffset d; |
|
|
DateTimeOffset d; |
|
|
try { |
|
|
try { |
|
|
d = DateTimeOffset.Parse(date, null, DateTimeStyles.AssumeUniversal); |
|
|
d = DateTimeOffset.ParseExact(date,new string[] { "dd.MM.", "d.MM", "dd.M", "d.M." }, null, DateTimeStyles.AssumeUniversal); |
|
|
} catch(FormatException) { |
|
|
} catch(FormatException) { |
|
|
await msg.Channel.SendMessageAsync("Sorry I can't parse your date. Please try again!"); |
|
|
await msg.Channel.SendMessageAsync("Sorry I can't parse your date. Please try again!"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
await msg.Channel.SendMessageAsync("Add " + name + " on " + d); |
|
|
|
|
|
|
|
|
|
|
|
var bday = new BDayUser(); |
|
|
var bday = new BDayUser(); |
|
|
bday.DiscordTag = user.Username + "#" + user.DiscriminatorValue; |
|
|
bday.DiscordTag = user.Username + "#" + user.DiscriminatorValue; |
|
|
bday.Channel = msg.Channel.Id; |
|
|
bday.Channel = msg.Channel.Id; |
|
|
bday.BirthDate = d; |
|
|
bday.BirthDate = d; |
|
|
bday.LastGreet = d.AddYears(-1); |
|
|
if(bday.BirthDate > DateTimeOffset.Now) |
|
|
|
|
|
{ |
|
|
|
|
|
bday.NextGreet = bday.BirthDate; |
|
|
|
|
|
} else |
|
|
|
|
|
{ |
|
|
|
|
|
if(bday.BirthDate.Day == DateTimeOffset.Now.Day && bday.BirthDate.Month == DateTimeOffset.Now.Month) |
|
|
|
|
|
{ |
|
|
|
|
|
bday.NextGreet = bday.BirthDate; |
|
|
|
|
|
} else |
|
|
|
|
|
{ |
|
|
|
|
|
bday.NextGreet = bday.BirthDate.AddYears(1); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
bdayGreetContext.Add(bday); |
|
|
bdayGreetContext.Add(bday); |
|
|
bdayGreetContext.SaveChanges(); |
|
|
bdayGreetContext.SaveChanges(); |
|
|
await msg.Channel.SendMessageAsync("I remembered that " + name + "'s next birthday "); |
|
|
await msg.Channel.SendMessageAsync("I remembered that " + name + "'s next birthday is on " + bday.NextGreet.ToString("dd.MM.yyyy")); |
|
|
|
|
|
} |
|
|
|
|
|
catch (InvalidOperationException) |
|
|
|
|
|
{ |
|
|
|
|
|
await msg.Channel.SendMessageAsync("I can't add " + name + ". Looks like they are already in the database?"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private async Task HandleBDayListCommand(SocketMessage msg, string[] parameters) |
|
|
private async Task HandleBDayListCommand(SocketMessage msg, string[] parameters) |
|
|
{ |
|
|
{ |
|
|
|
|
|
var gauthor = guild.Users.Where(u => u.Id == msg.Author.Id).Single(); |
|
|
|
|
|
if (!gauthor.GuildPermissions.Administrator) |
|
|
|
|
|
{ |
|
|
|
|
|
await msg.Channel.SendMessageAsync("Sorry only administrators can list birthdays :("); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
var bdays = bdayGreetContext.BDays.Where(b => b.Channel == msg.Channel.Id).ToList(); |
|
|
var bdays = bdayGreetContext.BDays.Where(b => b.Channel == msg.Channel.Id).ToList(); |
|
|
List<string> m = new List<string>(); |
|
|
List<string> m = new List<string>(); |
|
|
m.Add("I am tracking birthdays for: "); |
|
|
m.Add("I am tracking birthdays for: "); |
|
@ -170,6 +196,12 @@ namespace DiscoBot.bdaygreet |
|
|
|
|
|
|
|
|
private async Task HandleBDayDelCommand(SocketMessage msg, string[] parameters) |
|
|
private async Task HandleBDayDelCommand(SocketMessage msg, string[] parameters) |
|
|
{ |
|
|
{ |
|
|
|
|
|
var gauthor = guild.Users.Where(u => u.Id == msg.Author.Id).Single(); |
|
|
|
|
|
if (!gauthor.GuildPermissions.Administrator) |
|
|
|
|
|
{ |
|
|
|
|
|
await msg.Channel.SendMessageAsync("Sorry only administrators can delete birthdays :("); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
var gchan = msg.Channel as IGuildChannel; |
|
|
var gchan = msg.Channel as IGuildChannel; |
|
|
string tag = parameters[1]; |
|
|
string tag = parameters[1]; |
|
|
try |
|
|
try |
|
@ -177,10 +209,10 @@ namespace DiscoBot.bdaygreet |
|
|
var b = bdayGreetContext.BDays.Where(b => b.DiscordTag == tag).Single(); |
|
|
var b = bdayGreetContext.BDays.Where(b => b.DiscordTag == tag).Single(); |
|
|
bdayGreetContext.Remove(b); |
|
|
bdayGreetContext.Remove(b); |
|
|
bdayGreetContext.SaveChanges(); |
|
|
bdayGreetContext.SaveChanges(); |
|
|
await msg.Channel.SendMessageAsync("Removed birthday " + b.DiscordTag); |
|
|
await msg.Channel.SendMessageAsync("Removed "+ b.DiscordTag+"'s birthday from the database."); |
|
|
} catch(InvalidOperationException) |
|
|
} catch(InvalidOperationException) |
|
|
{ |
|
|
{ |
|
|
await msg.Channel.SendMessageAsync("Could not find " + tag+"'s birthday. Add it!"); |
|
|
await msg.Channel.SendMessageAsync("Could not find " + tag+"'s birthday."); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|