From ff55dce855ff71c542395dd05a1dfce179b24bd3 Mon Sep 17 00:00:00 2001 From: Jacob Benjamin Cholewa Date: Sun, 28 Jun 2020 17:05:14 +0200 Subject: [PATCH] Feature/fixed presentation (#2) * Fixed issues with subscription * Changed dev settings --- .gitignore | 3 + Modules/AdminModule.cs | 7 ++ Modules/SubscriptionModule.cs | 50 +++++++++++++ Modules/WorldBuffModule.cs | 114 ++++++++--------------------- Program.cs | 2 +- Services/CommandHandlingService.cs | 5 +- Services/NotificationService.cs | 3 + 7 files changed, 96 insertions(+), 88 deletions(-) create mode 100644 Modules/SubscriptionModule.cs diff --git a/.gitignore b/.gitignore index 0223779..b8cd50c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ # Created by https://www.toptal.com/developers/gitignore/api/osx,dotnetcore,windows # Edit at https://www.toptal.com/developers/gitignore?templates=osx,dotnetcore,windows +tuck.db +Hangfire.db + ### DotnetCore ### # .NET Core build folders bin/ diff --git a/Modules/AdminModule.cs b/Modules/AdminModule.cs index 1ec255c..0408687 100644 --- a/Modules/AdminModule.cs +++ b/Modules/AdminModule.cs @@ -18,5 +18,12 @@ public async Task Relay(ulong channelId, [Remainder] string msg) { await channel.SendMessageAsync(msg); } } + + [Command("time")] + public async Task Time() { + if(Context.User.Id == 103492791069327360) { + await ReplyAsync(DateTime.Now.ToString()); + } + } } } \ No newline at end of file diff --git a/Modules/SubscriptionModule.cs b/Modules/SubscriptionModule.cs new file mode 100644 index 0000000..6803526 --- /dev/null +++ b/Modules/SubscriptionModule.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Text.RegularExpressions; +using Discord.Commands; +using Tuck.Model; +using System.Collections.Generic; +using Discord; +using Hangfire; +using Tuck.Services; + +namespace Tuck.Modules +{ + [Group("wbuff")] + public class SubscriptionModule : ModuleBase + { + [Command("subscribe")] + [RequireContext(ContextType.Guild)] + public async Task AddSubscription(ulong guildId) { + using(var context = new TuckContext()) { + + if(context.Subscriptions.AsQueryable().Where(s => s.GuildId == Context.Guild.Id).Any()) { + await ReplyAsync("The subscription was **not** added. A subscription already exists. Do unsubscribe to change the server subscribe too."); + return; + } + + var subscription = new Subscription() { + GuildId = Context.Guild.Id, + ChannelId = Context.Channel.Id, + TargetGuildId = guildId + }; + + await context.AddAsync(subscription); + await context.SaveChangesAsync(); + await ReplyAsync("The subscription was added"); + } + } + + [Command("unsubscribe")] + [RequireContext(ContextType.Guild)] + public async Task Subscription() { + using(var context = new TuckContext()) { + var mathces = context.Subscriptions.AsQueryable().Where(t => t.GuildId == Context.Guild.Id); + context.RemoveRange(mathces); + await context.SaveChangesAsync(); + await ReplyAsync("The subscription was removed"); + } + } + } +} \ No newline at end of file diff --git a/Modules/WorldBuffModule.cs b/Modules/WorldBuffModule.cs index 24ae3dc..356c01a 100644 --- a/Modules/WorldBuffModule.cs +++ b/Modules/WorldBuffModule.cs @@ -20,7 +20,6 @@ public class WorldBuffModule : ModuleBase {BuffType.Ony, 6}, {BuffType.Rend, 2} }; - private static Dictionary _icons = new Dictionary { {BuffType.Ony, "<:ony1:724665644641026068>"}, {BuffType.Nef, "<:nef1:724665563967782923>"}, @@ -29,31 +28,23 @@ public class WorldBuffModule : ModuleBase }; [Command("list")] + [Alias("overview")] [RequireContext(ContextType.Guild)] public async Task PostBuffOverview() { using(var context = new TuckContext()) { - await ReplyAsync(GetBuffPost(context, Context.Guild.Id)); - } - } - - [Command("overview")] - [RequireContext(ContextType.Guild)] - public async Task PostBuffOverviewFromSubscribedGuilds() { - using(var context = new TuckContext()) { - var subscriptions = context.Subscriptions.AsQueryable() + var subscription = context.Subscriptions.AsQueryable() .Where(s => s.GuildId == Context.Guild.Id) - .ToList(); + .FirstOrDefault(); - foreach(var subscription in subscriptions) { - await ReplyAsync(GetBuffPost(context, subscription.TargetGuildId)); - } + await ReplyAsync(GetBuffPost(context, subscription?.TargetGuildId ?? Context.Guild.Id)); } } [Command("add")] [RequireContext(ContextType.Guild)] public async Task RegisterBuff(BuffType type, DateTime time) { - await RegisterBuff(type, time, Context.Guild.GetUser(Context.User.Id).Nickname); + var user = Context.Guild.GetUser(Context.User.Id); + await RegisterBuff(type, time, user.Nickname ?? user.Username); } [Command("add")] @@ -67,6 +58,15 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string using(var context = new TuckContext()) { + var subscription = context.Subscriptions.AsQueryable() + .Where(s => s.GuildId == Context.Guild.Id) + .Any(); + + if(subscription) { + await ReplyAsync("You cannot add buffs if a subscription exists"); + return; + } + var buff = new BuffInstance { GuildId = Context.Guild.Id, UserId = Context.User.Id, @@ -100,7 +100,7 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string } // Adding a job for later to make notifications - var notification = $"{_icons[buff.Type]} {buff.Type} is popping in {(buff.Time - DateTime.Now).Minutes} minutes by {buff.Username} @here"; + var notification = $"{_icons[buff.Type]} {buff.Type} is being popped in {(buff.Time - DateTime.Now).Minutes} minutes by {buff.Username}"; buff.JobId = NotificationService.PushNotification(buff.GuildId, notification, buff.Time - DateTime.Now - TimeSpan.FromMinutes(5)); // Saving the buff instance @@ -120,67 +120,18 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string [RequireContext(ContextType.Guild)] public async Task RemoveBuff(BuffType type, DateTime time) { using(var context = new TuckContext()) { - var mathces = context.Buffs.AsQueryable() + var match = context.Buffs.AsQueryable() .Where(t => t.GuildId == Context.Guild.Id) .Where(t => t.Type == type && t.Time == time) - .ToList(); - - foreach(var buff in mathces) { - NotificationService.CancelNotification(buff.JobId); - var update = $"{_icons[buff.Type]} {buff.Type} that was planned for {buff.Time.ToString("dddd")} at {buff.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>."; - NotificationService.PushNotification(buff.GuildId, update); - } + .FirstOrDefault(); - context.RemoveRange(mathces); - await context.SaveChangesAsync(); - await ReplyAsync(GetBuffPost(context, Context.Guild.Id)); - } - } - - [Command("subscribe")] - [RequireContext(ContextType.Guild)] - public async Task AddSubscription(ulong guildId) { - using(var context = new TuckContext()) { - var subscription = new Subscription() { - GuildId = Context.Guild.Id, - ChannelId = Context.Channel.Id, - TargetGuildId = guildId - }; - await context.AddAsync(subscription); - await context.SaveChangesAsync(); - await ReplyAsync("The subscription was added"); - } - } - - [Command("unsubscribe")] - [RequireContext(ContextType.Guild)] - public async Task Subscription(ulong guildId) { - using(var context = new TuckContext()) { - var mathces = context.Subscriptions.AsQueryable() - .Where(t => t.GuildId == Context.Guild.Id) - .Where(t => t.TargetGuildId == guildId); - context.RemoveRange(mathces); - await context.SaveChangesAsync(); - await ReplyAsync("The subscription was removed"); - } - } - - [Command("clear")] - [RequireContext(ContextType.Guild)] - public async Task RemoveAllBuffs() { - if(Context.User.Id == 103492791069327360) { - using(var context = new TuckContext()) { - var mathces = context.Buffs.AsQueryable() - .Where(t => t.GuildId == Context.Guild.Id); - - foreach(var buff in mathces) { - NotificationService.CancelNotification(buff.JobId); - var update = $"{_icons[buff.Type]} {buff.Type} that was planned for {buff.Time.ToString("dddd")} at {buff.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>."; - NotificationService.PushNotification(buff.GuildId, update); - } - - context.RemoveRange(mathces); + if(match != null){ + NotificationService.CancelNotification(match.JobId); + var update = $"{_icons[match.Type]} {match.Type} that was planned for {match.Time.ToString("dddd")} at {match.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>."; + NotificationService.PushNotification(match.GuildId, update); + context.Remove(match); await context.SaveChangesAsync(); + await ReplyAsync(GetBuffPost(context, Context.Guild.Id)); } } } @@ -188,18 +139,13 @@ public async Task RemoveAllBuffs() { private string GetBuffPost(TuckContext context, ulong guildId) { var buffs = context.Buffs.AsQueryable() .Where(t => t.GuildId == guildId && DateTime.Now < t.Time) - .ToList(); + .OrderByDescending(t => t.Time).ThenBy(t => t.Type) + .ToList() + .Select(t => $"\n> {_icons[t.Type]} {t.Time.ToString("HH:mm")} by {t.Username}") + .Aggregate("", (s1,s2) => s1 + s2); - var msg = "**World buff schedule:**\n"; - if(buffs.Count() > 0) { - foreach(var type in buffs.GroupBy(t => t.Type)) { - var times = type.OrderBy(t => t.Time).Select(e => $"{e.Time.ToString("HH:mm")} ({e.Username})"); - msg += $"> {_icons[type.Key]} " + String.Join(", ", times) + "\n"; - } - } else { - msg += "> Nothing have been added yet..."; - } - return msg; + return "**World buff schedule:**" + + (string.IsNullOrEmpty(buffs) ? "\n> Nothing have been added yet..." : buffs); } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 9d33d0b..1c08b3a 100644 --- a/Program.cs +++ b/Program.cs @@ -58,4 +58,4 @@ private ServiceProvider ConfigureServices() .BuildServiceProvider(); } } -} \ No newline at end of file +} diff --git a/Services/CommandHandlingService.cs b/Services/CommandHandlingService.cs index 4eb5063..c89a454 100644 --- a/Services/CommandHandlingService.cs +++ b/Services/CommandHandlingService.cs @@ -5,7 +5,6 @@ using Discord; using Discord.Commands; using Discord.WebSocket; -using System.Text.RegularExpressions; using Tuck.Readers; using Tuck.Model; @@ -52,7 +51,7 @@ public async Task MessageReceivedAsync(SocketMessage rawMessage) await message.Channel.SendMessageAsync("Fuck you Sena"); } if(message.Content.Contains("Death's Sting") || message.Content.Contains("item=21126")) { - await message.Channel.SendMessageAsync("That weapon should be prio to Mondino!"); + await message.Channel.SendMessageAsync("That weapon should be prio to <@103492791069327360>!"); } if(_random.Next(100) == 0) { var channel = await message.Author.GetOrCreateDMChannelAsync(); @@ -90,4 +89,4 @@ public async Task CommandExecutedAsync(Optional command, ICommandCo await context.Channel.SendMessageAsync($"{result}"); } } -} \ No newline at end of file +} diff --git a/Services/NotificationService.cs b/Services/NotificationService.cs index d8cdee2..1e3128b 100644 --- a/Services/NotificationService.cs +++ b/Services/NotificationService.cs @@ -18,10 +18,12 @@ public NotificationService(IServiceProvider services) { } public static string PushNotification(ulong guildId, string msg) { + Console.WriteLine($"Notification added. ({msg})"); return BackgroundJob.Enqueue(ns => ns.HandleNotification(guildId, msg)); } public static string PushNotification(ulong guildId, string msg, TimeSpan time) { + Console.WriteLine($"Notification added. ({time} )({msg})"); return BackgroundJob.Schedule(ns => ns.HandleNotification(guildId, msg), time); } @@ -38,6 +40,7 @@ private IEnumerable getSubscriptions(ulong guildId){ } public async Task HandleNotification(ulong guildId, string msg) { + Console.WriteLine($"Now notifying. ({msg})"); foreach(var subscription in getSubscriptions(guildId)) { var channel = _client.GetChannel(subscription.ChannelId) as ISocketMessageChannel; await channel.SendMessageAsync(msg);