Exemplo n.º 1
0
        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));
                }
            }
        }
Exemplo n.º 2
0
        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");
                }
            }
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        private async Task MakeNotification(TuckContext context, ulong guildId, Embed embed)
        {
            var subscriptions = context.Subscriptions.AsQueryable()
                                .Where(s => s.TargetGuildId == guildId)
                                .ToList();

            foreach (var subscription in subscriptions)
            {
                try {
                    if (subscription.LastMessage == null)
                    {
                        await PostMessage(context, subscription, embed);
                    }
                    else
                    {
                        await UpdateMessage(context, subscription, embed);
                    }
                    if (subscription.SubscriberAlert != null)
                    {
                        await NotifySubscriber(context, subscription);
                    }
                } catch (Exception e) {
                    // Write in the log that posting to the channel failed.
                    var guild = Context.Client.GetGuild(subscription.GuildId);
                    Console.WriteLine($"Notification for guildId={subscription.GuildId}, guildName={guild?.Name ?? "unkown"}, owner={guild?.OwnerId.ToString() ?? "unkown"} failed.");
                    Console.WriteLine(e.Message);
                    if (guild == null)
                    {
                        await RemoveSubsciption(guild, subscription);
                    }
                }
            }
        }
Exemplo n.º 5
0
 private List <BuffInstance> GetBuffsTomorrow(TuckContext context, ulong guildId)
 {
     return(context.Buffs.AsQueryable()
            .Where(t => t.GuildId == guildId && DateTime.Today.AddDays(1) == t.Time.Date)
            .OrderBy(t => t.Time).ThenBy(t => t.Type)
            .ToList());
 }
Exemplo n.º 6
0
        public async Task Alert(AlertAction action, SocketRole role)
        {
            using (var context = new TuckContext()) {
                Subscription subscription;
                try {
                    subscription = context.Subscriptions
                                   .AsQueryable()
                                   .Single(t => t.GuildId == Context.Guild.Id);
                } catch (InvalidOperationException) {
                    await ReplyAsync("You need to have an active subscription to enable alerts.");

                    return;
                }

                switch (action)
                {
                case AlertAction.add:
                    await AddAlert(context, subscription, role.Id);

                    break;

                case AlertAction.remove:
                    await RemoveAlert(context, subscription);

                    break;
                }
            }
        }
Exemplo n.º 7
0
        private Embed GetBuffPost(TuckContext context, ulong guildId)
        {
            var guild   = Context.Client.GetGuild(guildId);
            var builder = new EmbedBuilder()
                          .WithAuthor("World buff schedule", guild?.IconUrl)
                          .WithFooter("Last updated")
                          .WithCurrentTimestamp();

            var buffs    = GetBuffs(context, guildId);
            var lastPops = new Dictionary <BuffType, DateTime>();

            foreach (var buff in buffs.OrderBy(t => t.Time))
            {
                if (lastPops.ContainsKey(buff.Type) && _cooldown.ContainsKey(buff.Type))
                {
                    var lastPop  = lastPops[buff.Type];
                    var cooldown = _cooldown[buff.Type];
                    buff.Conflicting = buff.Time < lastPop.AddHours(cooldown);
                }
                lastPops[buff.Type] = buff.Time;
            }

            var buffsByDay = buffs
                             .Where(t => t.Time.Date >= DateTime.Today)
                             .GroupBy(t => t.Time.Date)
                             .OrderBy(g => g.Key);

            foreach (var day in buffsByDay)
            {
                builder.AddField($"{day.Key:dddd, dd MMMM}", GetBuffsAsString(day));
            }

            builder.AddField("\u200B", "https://discord.gg/NKNgEsp\u200B");
            return(builder.Build());
        }
Exemplo n.º 8
0
        private async Task MakeNotification(TuckContext context, ulong guildId, Embed embed)
        {
            var subscriptions = context.Subscriptions.AsQueryable()
                                .Where(s => s.TargetGuildId == guildId)
                                .ToList();

            foreach (var subscription in subscriptions)
            {
                try {
                    if (subscription.LastMessage == null)
                    {
                        await PostMessage(context, subscription, embed);
                    }
                    else
                    {
                        await UpdateMessage(context, subscription, embed);
                    }
                    if (subscription.SubscriberAlert != null)
                    {
                        await NotifySubscriber(context, subscription);
                    }
                } catch (Exception e) {
                    var guild = Context.Client.GetGuild(subscription.GuildId);
                    Console.WriteLine($"Notification for guildId={subscription.GuildId}, guildName={guild.Name}, owner={guild.OwnerId} failed.");
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 9
0
 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");
     }
 }
Exemplo n.º 10
0
        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();
        }
Exemplo n.º 11
0
        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();
        }
Exemplo n.º 12
0
        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}");
            }
        }
Exemplo n.º 13
0
        private async Task UpdateMessage(TuckContext context, Subscription subscription, Embed embed)
        {
            var channel = Context.Client.GetChannel(subscription.ChannelId) as ISocketMessageChannel;
            var message = await channel.GetMessageAsync(subscription.LastMessage.Value) as RestUserMessage;

            // Fallback incase message doesn't exist, create new one.
            if (message == null)
            {
                await PostMessage(context, subscription, embed);
            }
            else
            {
                await message.ModifyAsync(msg => msg.Embed = embed);
            }
        }
Exemplo n.º 14
0
        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));
            }
        }
Exemplo n.º 15
0
        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();
                }
            }
        }
Exemplo n.º 16
0
        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();
        }
Exemplo n.º 17
0
        private Embed GetBuffPost(TuckContext context, ulong guildId)
        {
            var guild   = Context.Client.GetGuild(guildId);
            var builder = new EmbedBuilder()
                          .WithAuthor("World buff schedule", guild?.IconUrl)
                          .WithFooter("Last updated")
                          .WithCurrentTimestamp();

            var today = GetBuffsToday(context, guildId);

            builder.AddField($"{DateTime.Today:dddd}", GetBuffsAsString(today));

            var tomorrow = GetBuffsTomorrow(context, guildId);

            if (tomorrow.Count() != 0)
            {
                builder.AddField($"{DateTime.Today.AddDays(1):dddd}", GetBuffsAsString(tomorrow));
            }

            builder.AddField("\u200B", "The world buff schedule is moderated by the guild masters and officers of Dreadmist. To queue a buff, reach out to one of the officers in your guild and have them add it in the global discord channel.\u200B");

            return(builder.Build());
        }
Exemplo n.º 18
0
        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");
            }
        }
Exemplo n.º 19
0
        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));
            }
        }
Exemplo n.º 20
0
 private ICollection <BuffInstance> GetBuffs(TuckContext context, ulong guildId)
 {
     return(context.Buffs.AsQueryable()
            .Where(t => t.GuildId == guildId && DateTime.Today.AddHours(-8) <= t.Time)
            .ToList());
 }