public async Task AddSubscription(ulong guildId, ulong channelId, ulong targetId) { if (Context.User.Id == 103492791069327360) { using (var context = new TuckContext()) { if (context.Subscriptions.AsQueryable().Where(s => s.GuildId == guildId && s.TargetGuildId == targetId).Any()) { await ReplyAsync("A subscription already exists."); return; } var subscription = new Subscription() { GuildId = guildId, ChannelId = channelId, TargetGuildId = targetId }; await context.AddAsync(subscription); await context.SaveChangesAsync(); await ReplyAsync("The subscription was added"); } } }
private async Task NotifySubscriber(TuckContext context, Subscription subscription) { var channel = Context.Client.GetChannel(subscription.ChannelId) as ISocketMessageChannel; var message = await channel.SendMessageAsync(string.Format( "{0}World buff list has been updated. ({1})", // SubscriberAlert can never be null here either.. So fallback is just to cast to int(64) MentionUtils.MentionRole(subscription.SubscriberAlert ?? 0) + ": ", // CultureInfo param should be refactored to EU/NA/etc when multi-realm support is added. DateTime.Now.ToString("HH:mm", new System.Globalization.CultureInfo("fr-FR")) )); // If a previous alert exists, delete it. ulong lastAlert = subscription.LastAlert ?? 0; if (lastAlert != 0) { var lastMsg = await channel.GetMessageAsync(lastAlert); if (lastMsg != null) { await lastMsg.DeleteAsync(); } } // Store the new message Id. subscription.LastAlert = message.Id; context.Update(subscription); await context.SaveChangesAsync(); }
public async Task RemoveBuff(BuffType type, DateTime time) { using (var context = new TuckContext()) { var buff = context.Buffs.AsQueryable() .Where(t => t.GuildId == Context.Guild.Id) .Where(t => t.Type == type && t.Time == time) .FirstOrDefault(); if (buff != null) { context.Remove(buff); await context.SaveChangesAsync(); try { await Context.Message.AddReactionAsync(Emote.Parse(_icons[buff.Type])); } catch (Discord.Net.HttpException) { // If custom emoji's returns an error, add a thumbs up instead. await Context.Message.AddReactionAsync(new Emoji("👍")); } // Posting an update message in all subscribing channels await MakeNotification(context, buff.GuildId, GetBuffPost(context, Context.Guild.Id)); } } }
private async Task PostMessage(TuckContext context, Subscription subscription, Embed embed) { var channel = Context.Client.GetChannel(subscription.ChannelId) as ISocketMessageChannel; var message = await channel.SendMessageAsync("", false, embed); subscription.LastMessage = message.Id; context.Update(subscription); await context.SaveChangesAsync(); }
public async Task Subscription() { using (var context = new TuckContext()) { var matches = context.Subscriptions.AsQueryable().Where(t => t.GuildId == Context.Guild.Id); context.RemoveRange(matches); await context.SaveChangesAsync(); await ReplyAsync("The subscription was removed"); } }
private async Task RemoveAlert(TuckContext context, Subscription subscription) { if (subscription.SubscriberAlert == null) { await ReplyAsync("No alert is currently active."); return; } subscription.SubscriberAlert = null; context.Update(subscription); await context.SaveChangesAsync(); }
private async Task RemoveSubsciption(SocketGuild guild, Subscription subscription) { using (var context = new TuckContext()) { // Remove the subscription that failed. context.Subscriptions.Remove(subscription); await context.SaveChangesAsync(); // Notify in the bother tuck channel that an error happend. var channel = Context.Client.GetChannel(subscription.ChannelId) as ISocketMessageChannel; await ReplyAsync($"I removed the subscription for guildId={subscription.GuildId} because the guild no longer exists."); Console.WriteLine($"Removed subscription with id={subscription.Id}"); } }
public async Task RegisterNinjaPop(BuffType type, DateTime time, [Remainder] string username) { if (time > DateTime.Now) { await ReplyAsync($"On my watch the time is already {DateTime.Now.ToString("HH:mm:ss")}. You can't add a ninja pop that didn't happen yet."); return; } using (var context = new TuckContext()) { var subscription = context.Subscriptions.AsQueryable() .Where(s => s.GuildId == Context.Guild.Id && s.TargetGuildId != s.GuildId) .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, Time = time, Type = type, Ninja = true, Username = username }; // Saving the buff instance await context.AddAsync(buff); await context.SaveChangesAsync(); try { await Context.Message.AddReactionAsync(Emote.Parse(_icons[buff.Type])); } catch (Discord.Net.HttpException) { // If custom emoji's returns an error, add a thumbs up instead. await Context.Message.AddReactionAsync(new Emoji("👍")); } // Posting an update message in all subscribing channels await MakeNotification(context, buff.GuildId, GetBuffPost(context, buff.GuildId)); } }
public async Task PostBuffOverview() { using (var context = new TuckContext()) { var subscription = context.Subscriptions.AsQueryable() .Where(s => s.GuildId == Context.Guild.Id) .FirstOrDefault(); var embed = GetBuffPost(context, subscription?.TargetGuildId ?? Context.Guild.Id); var msg = await ReplyAsync("", false, embed); if (subscription != null && Context.Channel.Id == subscription.ChannelId) { subscription.LastMessage = msg.Id; context.Update(subscription); await context.SaveChangesAsync(); } } }
private async Task AddAlert(TuckContext context, Subscription subscription, ulong role) { if (subscription.SubscriberAlert != null) { await ReplyAsync(string.Format( "Replacing alerts for {0} with {1}.", MentionUtils.MentionRole(subscription.SubscriberAlert ?? 0), MentionUtils.MentionRole(role) )); } else { await ReplyAsync(string.Format( "Enabled alerts for {0}.", MentionUtils.MentionRole(role) )); } subscription.SubscriberAlert = role; context.Update(subscription); await context.SaveChangesAsync(); }
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"); } }
public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string username) { if (time < DateTime.Now) { await ReplyAsync($"On my watch the time is already {DateTime.Now.ToString("HH:mm")}. You can't add buffs earlier than that."); return; } using (var context = new TuckContext()) { var subscription = context.Subscriptions.AsQueryable() .Where(s => s.GuildId == Context.Guild.Id && s.TargetGuildId != s.GuildId) .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, Time = time, Type = type, Username = username }; if (_cooldown.ContainsKey(buff.Type)) { var from = buff.Time.AddHours(-_cooldown[buff.Type]); var to = buff.Time.AddHours(_cooldown[buff.Type]); var overlaps = context.Buffs.AsQueryable() .Where(t => t.GuildId == Context.Guild.Id) .Where(t => t.Type == buff.Type) .Where(t => t.Time > from && t.Time < to) .OrderBy(t => t.Time) .ToList(); if (overlaps.Count > 0) { var warning = $"The buff you just added will clash with buffs already added:"; foreach (var overlap in overlaps) { warning += $"\n> {overlap.Time.ToString("HH:mm")} by {overlap.Username} (<@{overlap.UserId}>)"; } warning += $"\n\n To remove the buff you added, write !wbuff remove {buff.Type} {buff.Time.ToString("HH:mm")}"; var channel = await Context.User.GetOrCreateDMChannelAsync(); await channel.SendMessageAsync(warning); } } // Saving the buff instance await context.AddAsync(buff); await context.SaveChangesAsync(); try { await Context.Message.AddReactionAsync(Emote.Parse(_icons[buff.Type])); } catch (Discord.Net.HttpException) { // If custom emoji's returns an error, add a thumbs up instead. await Context.Message.AddReactionAsync(new Emoji("👍")); } // Posting an update message in all subscribing channels await MakeNotification(context, buff.GuildId, GetBuffPost(context, buff.GuildId)); } }