Example #1
0
        private async Task Client_MessageReactionRemoved(DiscordClient discordClient, MessageReactionRemoveEventArgs e)
        {
            var message = await e.Message.HydrateAsync();

            if (!message.Author.IsCurrent)
            {
                return;
            }

            using var scope = ServiceScopeFactory.CreateScope();

            var dbContext  = scope.ServiceProvider.GetService <InciteDbContext>();
            var guildEvent = await dbContext.Events
                             .Include(x => x.EventMembers)
                             .ThenInclude(x => x.Member)
                             .ThenInclude(x => x.User)
                             .FirstOrDefaultAsync(x => x.EventMessage.DiscordId == message.Id);

            if (guildEvent == null)
            {
                return;
            }
            else if (guildEvent.DateTime.UtcDateTime - DateTimeOffset.UtcNow < TimeSpan.FromMinutes(30))
            {
                var discordMember = await e.Guild.GetMemberAsync(e.User.Id);

                var dmChannel = await discordMember.CreateDmChannelAsync();

                await dmChannel.SendMessageAsync("Signups are already closed for this event.");

                return;
            }

            var member = guildEvent.EventMembers
                         .FirstOrDefault(x => x.Member.User.DiscordId == e.User.Id);

            if (member?.EmojiDiscordName == e.Emoji.Name)
            {
                dbContext.EventMembers.Remove(member);
                await dbContext.SaveChangesAsync();
            }

            var latestSnapshot = await dbContext.EpgpSnapshots
                                 .Include(x => x.Standings)
                                 .ThenInclude(x => x.WowCharacter)
                                 .OrderByDescending(x => x.DateTime)
                                 .FirstOrDefaultAsync(x => x.GuildId == guildEvent.GuildId);

            var eventMessage = new Messages.DiscordEventMessage(discordClient, message, guildEvent, latestSnapshot);
            await eventMessage.UpdateAsync();
        }
Example #2
0
        public async Task Create(CommandContext context,
                                 string name,
                                 string description,
                                 [Description(Descriptions.DateTime)] DateTimeOffset dateTime)
        {
            if (DateTimeOffset.UtcNow > dateTime.UtcDateTime)
            {
                ResponseString = "The event cannot be in the past";
                return;
            }

            var discordMessage = await context.Message.RespondAsync("\u200b");

            var message = new Message()
            {
                DiscordId = discordMessage.Id,
            };

            var memberEvent = new Models.Event()
            {
                Name         = name,
                Description  = description,
                DateTime     = dateTime,
                GuildId      = Guild.Id,
                OwnerId      = Member.Id,
                EventMessage = message
            };

            m_dbContext.Events.Add(memberEvent);
            await m_dbContext.SaveChangesAsync();

            var discordEventMessage = new Messages.DiscordEventMessage(context.Client, discordMessage, memberEvent);
            await discordEventMessage.UpdateAsync();

            await discordEventMessage.AddReactionsToEventMessageAsync();
        }
Example #3
0
        private async Task Client_MessageReactionAdded(DiscordClient discordClient, MessageReactionAddEventArgs e)
        {
            var message = await e.Message.HydrateAsync();

            if (e.User.IsCurrent || !message.Author.IsCurrent)
            {
                return;
            }

            using var scope = ServiceScopeFactory.CreateScope();

            var dbContext  = scope.ServiceProvider.GetService <InciteDbContext>();
            var guildEvent = await dbContext.Events
                             .Include(x => x.EventMembers)
                             .ThenInclude(x => x.Member)
                             .ThenInclude(x => x.User)
                             .FirstOrDefaultAsync(x => x.EventMessage.DiscordId == message.Id);

            if (guildEvent == null)
            {
                return;
            }
            else if (guildEvent.DateTime.UtcDateTime - DateTimeOffset.UtcNow < TimeSpan.FromMinutes(30))
            {
                var discordMember = await e.Guild.GetMemberAsync(e.User.Id);

                var dmChannel = await discordMember.CreateDmChannelAsync();

                await dmChannel.SendMessageAsync("Signups are already closed for this event.");

                return;
            }

            var member = await dbContext.Members
                         .Where(x => x.User.DiscordId == e.User.Id)
                         .Include(x => x.User.WowCharacters)
                         .FirstOrDefaultAsync();

            if (member?.User.WowCharacters.Count == 0)
            {
                await message.DeleteReactionAsync(e.Emoji, e.User, "User not registered");

                var discordMember = await e.Guild.GetMemberAsync(e.User.Id);

                var dmChannel = await discordMember.CreateDmChannelAsync();

                await dmChannel.SendMessageAsync("You must first add a wow character. See '!help wow char add-character' for details");

                return;
            }

            using var messageLock = await LockOnMessageAsync(message.Id);

            var eventMember = guildEvent.EventMembers
                              .FirstOrDefault(x => x.MemberId == member.Id);

            if (eventMember == null)
            {
                eventMember = new EventMember()
                {
                    EventId          = guildEvent.Id,
                    MemberId         = member.Id,
                    EmojiDiscordName = e.Emoji.Name
                };

                guildEvent.EventMembers.Add(eventMember);
            }
            else
            {
                eventMember.EmojiDiscordName = e.Emoji.Name;
            }

            await dbContext.SaveChangesAsync();

            var latestSnapshot = await dbContext.EpgpSnapshots
                                 .Include(x => x.Standings)
                                 .ThenInclude(x => x.WowCharacter)
                                 .OrderByDescending(x => x.DateTime)
                                 .FirstOrDefaultAsync(x => x.GuildId == guildEvent.GuildId);

            var eventMessage = new Messages.DiscordEventMessage(discordClient, message, guildEvent, latestSnapshot);
            await eventMessage.RemovePreviousReactionsAsync(e.User, e.Emoji);

            await eventMessage.UpdateAsync();
        }