Add basic commands for birthday greet
This commit is contained in:
		
							parent
							
								
									6885d6b6b9
								
							
						
					
					
						commit
						cf4ab2a15c
					
				
							
								
								
									
										
											BIN
										
									
								
								DiscoBot/615631052047908870-bdaygreet.db
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								DiscoBot/615631052047908870-bdaygreet.db
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								DiscoBot/98293701175955456-bdaygreet.db
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								DiscoBot/98293701175955456-bdaygreet.db
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										206
									
								
								DiscoBot/bdaygreet/BDayGreet.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								DiscoBot/bdaygreet/BDayGreet.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,206 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Globalization; | ||||
| using System.Linq; | ||||
| using System.Net.WebSockets; | ||||
| using System.ServiceModel.Syndication; | ||||
| using System.Threading.Tasks; | ||||
| using System.Timers; | ||||
| using System.Xml; | ||||
| using Discord; | ||||
| using Discord.WebSocket; | ||||
| 
 | ||||
| namespace DiscoBot.bdaygreet | ||||
| { | ||||
|     public class BDayGreet : IModule | ||||
|     { | ||||
|         string IModule.Name { get => "BDayGreet"; set => throw new NotImplementedException(); } | ||||
|         private SocketGuild guild; | ||||
|         private BDayGreetContext bdayGreetContext; | ||||
|         private Dictionary<string, Timer> timers = new Dictionary<string, Timer>(); | ||||
|         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 BDayGreet(SocketGuild guild) | ||||
|         { | ||||
|             this.guild = guild; | ||||
|             bdayGreetContext = new BDayGreetContext(guild.Id); | ||||
|             bdayGreetContext.Database.EnsureCreated(); | ||||
|             foreach(var f in bdayGreetContext.BDays) | ||||
|             { | ||||
|                 //InitializeFeed(f); | ||||
|             } | ||||
| 
 | ||||
|             Commands.Add("bdayadd", HandleBDayAddCommand); | ||||
|             Commands.Add("bdaydel", HandleBDayDelCommand); | ||||
|             Commands.Add("bdaylist", HandleBDayListCommand); | ||||
|             Commands.Add("bdaydebug", HandleBDayDebugCommand); | ||||
|         } | ||||
| 
 | ||||
|         /* | ||||
|         private Task InitializeFeed(BDayUser feed) | ||||
|         { | ||||
|             Console.WriteLine("Found feed " + feed.Name); | ||||
|              | ||||
|             Timer timer = new Timer(feed.CheckInterval.TotalMilliseconds); | ||||
|             timer.AutoReset = true; | ||||
|             timer.Elapsed += async (sender, e) => | ||||
|             { | ||||
|                 await Task.Run(() => HandleBDayCheck(feed)); | ||||
|             }; | ||||
|             timer.Start(); | ||||
|             timers.Add(feed.Name, timer); | ||||
| 
 | ||||
|             HandleBDayCheck(feed); | ||||
|             return Task.CompletedTask; | ||||
|         } | ||||
| 
 | ||||
|         private Task DeinitializeFeed(BDayFeed feed) | ||||
|         { | ||||
|             Timer t = timers[feed.Name]; | ||||
|             t.Stop(); | ||||
|             timers.Remove(feed.Name); | ||||
|             return Task.CompletedTask; | ||||
|         } | ||||
| 
 | ||||
|         private Task HandleBDayCheck(BDayFeed feed) | ||||
|         { | ||||
|             SocketTextChannel c = guild.Channels.Where(g => g.Id == feed.Channel).Single() as SocketTextChannel; | ||||
|             c.SendMessageAsync("Yes yes i will check."); | ||||
|              | ||||
|             try | ||||
|             { | ||||
|                 XmlReader reader = XmlReader.Create(feed.Url); | ||||
|                 SyndicationFeed f = SyndicationFeed.Load(reader); | ||||
|                 DateTimeOffset newestItem = feed.LastChecked; | ||||
|                 foreach(var item in f.Items) | ||||
|                 { | ||||
|                     if (item.LastUpdatedTime <= feed.LastChecked) | ||||
|                         continue; | ||||
|                     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!"); | ||||
|                         newestItem = item.LastUpdatedTime; | ||||
|                     } | ||||
|                     foreach (var l in item.Links) | ||||
|                     { | ||||
|                         m.Add(l.Uri.ToString()); | ||||
|                     } | ||||
|                     c.SendMessageAsync(string.Join(" | ", m)); | ||||
|                 } | ||||
|                 feed.LastChecked = newestItem; | ||||
|                 rssContext.SaveChangesAsync(); | ||||
|             } catch(System.Net.WebException e) | ||||
|             { | ||||
|                 c.SendMessageAsync("<" + feed.Name + "> " + "NetworkFailure: " + e.Message); | ||||
|             } catch(XmlException e) | ||||
|             { | ||||
|                 c.SendMessageAsync("<" + feed.Name + "> " + "Malformed Response: " + e.Message); | ||||
|             } | ||||
|              | ||||
|             return Task.CompletedTask; | ||||
|         } | ||||
|         */ | ||||
| 
 | ||||
|         private async Task HandleBDayAddCommand(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 add birthdays :("); | ||||
|                 return; | ||||
|             } | ||||
|             var gchan = msg.Channel as IGuildChannel; | ||||
|             string name = parameters[1]; | ||||
|             string date = parameters[2]; | ||||
| 
 | ||||
|             SocketGuildUser user; | ||||
|             try | ||||
|             { | ||||
|                 user = guild.Users.Where(u => u.Mention == name).Single(); | ||||
|             } | ||||
|             catch (InvalidOperationException) | ||||
|             { | ||||
|                 await msg.Channel.SendMessageAsync("I can't find user " + name + " so I can't add them."); | ||||
|                 return; | ||||
|             } | ||||
|             DateTimeOffset d; | ||||
|             try { | ||||
|                 d = DateTimeOffset.Parse(date, null, DateTimeStyles.AssumeUniversal); | ||||
|             } catch(FormatException) { | ||||
|                 await msg.Channel.SendMessageAsync("Sorry I can't parse your date. Please try again!"); | ||||
|                 return; | ||||
|             }  | ||||
|             await msg.Channel.SendMessageAsync("Add " + name + " on " + d); | ||||
|              | ||||
|             var bday = new BDayUser(); | ||||
|             bday.DiscordTag = user.Username + "#" + user.DiscriminatorValue; | ||||
|             bday.Channel = msg.Channel.Id; | ||||
|             bday.BirthDate = d; | ||||
|             bday.LastGreet = d.AddYears(-1); | ||||
|             bdayGreetContext.Add(bday); | ||||
|             bdayGreetContext.SaveChanges(); | ||||
|             await msg.Channel.SendMessageAsync("I remembered that " + name + "'s next birthday "); | ||||
|         } | ||||
| 
 | ||||
|         private async Task HandleBDayListCommand(SocketMessage msg, string[] parameters) | ||||
|         { | ||||
|             var bdays = bdayGreetContext.BDays.Where(b => b.Channel == msg.Channel.Id).ToList(); | ||||
|             List<string> m = new List<string>(); | ||||
|             m.Add("I am tracking birthdays for: "); | ||||
|             int cnt = 0; | ||||
|             string str = ""; | ||||
|             foreach(var bday in bdays) | ||||
|             { | ||||
|                 str += bday.DiscordTag + " "; | ||||
|                 cnt++; | ||||
|                 if(cnt > 4) | ||||
|                 { | ||||
|                     m.Add(str); | ||||
|                     str = ""; | ||||
|                 } | ||||
|             } | ||||
|             m.Add(str); | ||||
|             await msg.Channel.SendMessageAsync(string.Join("\n",m)); | ||||
|         } | ||||
| 
 | ||||
|         private async Task HandleBDayDelCommand(SocketMessage msg, string[] parameters) | ||||
|         { | ||||
|             var gchan = msg.Channel as IGuildChannel; | ||||
|             string tag = parameters[1]; | ||||
|             try | ||||
|             { | ||||
|                 var b = bdayGreetContext.BDays.Where(b => b.DiscordTag == tag).Single(); | ||||
|                 bdayGreetContext.Remove(b); | ||||
|                 bdayGreetContext.SaveChanges(); | ||||
|                 await msg.Channel.SendMessageAsync("Removed birthday " + b.DiscordTag); | ||||
|             } catch(InvalidOperationException) | ||||
|             { | ||||
|                 await msg.Channel.SendMessageAsync("Could not find " + tag+"'s birthday. Add it!"); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         private Task HandleBDayDebugCommand(SocketMessage msg, string[] parameters) | ||||
|         { | ||||
|             Console.WriteLine("Handling test command!"); | ||||
|             string para = string.Join(",", parameters); | ||||
|             msg.Channel.SendMessageAsync("Test succeeded. Params: "+para); | ||||
|             return Task.CompletedTask; | ||||
|         } | ||||
| 
 | ||||
|         public void Initialize() | ||||
|         { | ||||
|             Console.WriteLine("Initializing bdaygreet..."); | ||||
|         } | ||||
| 
 | ||||
|         public async Task OnNewWebSocketAsync(WebSocket ws, TaskCompletionSource<object> tcs) | ||||
|         { | ||||
|             Console.WriteLine("Calendar " + guild.Id + " has a new websocket."); | ||||
|             webSockets.Add(ws); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										35
									
								
								DiscoBot/bdaygreet/BDayGreetContext.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								DiscoBot/bdaygreet/BDayGreetContext.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace DiscoBot.bdaygreet | ||||
| { | ||||
|     public class BDayGreetContext : DbContext | ||||
|     { | ||||
|         private ulong guildId; | ||||
|         public DbSet<BDayUser> BDays { get; set; } | ||||
| 
 | ||||
|         public BDayGreetContext(ulong guildId) | ||||
|         { | ||||
|             this.guildId = guildId; | ||||
|         } | ||||
| 
 | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||||
|         { | ||||
|             optionsBuilder.UseSqlite("Data Source=" + guildId + "-bdaygreet.db"); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public class BDayUser | ||||
|     { | ||||
|         [Key] | ||||
|         public string DiscordTag { get; set; } | ||||
|         public DateTimeOffset BirthDate { get; set; } | ||||
|         public ulong Channel { get; set; } | ||||
|         public DateTimeOffset LastGreet { get; set; } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -63,7 +63,7 @@ namespace DiscoBot.gsmeet | ||||
|                     t.Elapsed += async (sender, e) => | ||||
|                     { | ||||
|                         foreach(var user in users) | ||||
|                         { | ||||
|                         {                             | ||||
|                             if(user.Signups[idx] != "1" && user.Signups[idx] != "0" && user.Signups[idx] != "0.5") | ||||
|                             { | ||||
|                                 if(user.User != null) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user