Beispiel #1
0
        public async Task <CommandResult> AddUserAsync(IGuildUser guildUser)
        {
            if (!guildData.IsEnabled)
            {
                return(CommandResult.FromError("Waiting list is not open"));
            }

            if (guildData.UsersInList.Any(x => x.UserId == guildUser.Id))
            {
                return(CommandResult.FromError("You are already on the waiting list!"));
            }
            else
            {
                var userInList = guildData.GetOrCreateGuildUser(guildUser.Id, guildUser.Nickname ?? guildUser.Username);

                userInList.IsInWaitingList = true;
                userInList.JoinTime        = DateTime.Now;
                userInList.IsSub           = guildUser.RoleIds.Contains(guildData.SubRoleId);

                dataContext.Update(userInList);

                await dataContext.SaveChangesAsync();

                return(CommandResult.FromSuccess($"Waiting list joined!"));
            }
        }
Beispiel #2
0
        private async void CheckForTimeout(object?state)
        {
            if (timerRunning)
            {
                return;
            }
            timerRunning = true;
            try
            {
                using (var waitingListDataContext = new WaitingListDataContext())
                {
                    foreach (var invitedUser in waitingListDataContext.InvitedUsers
                             .Include(iu => iu.User).ThenInclude(iu => iu.Guild).Include(iu => iu.Invite).ToList())
                    {
                        if (invitedUser.InviteAccepted == null)
                        {
                            if (invitedUser.InviteTime + TimeSpan.FromMinutes(1) < DateTime.Now)
                            {
                                var guild = client.GetGuild(invitedUser.User.Guild.GuildId);
                                if (guild != null)
                                {
                                    var waitingList = new CommandWaitingList(waitingListDataContext !, client.Rest, guild.Id);

                                    try
                                    {
                                        await DeclineInviteAsync(client.Rest, guild, waitingList, invitedUser);

                                        var dmChannel = await(await client.Rest.GetUserAsync(invitedUser.User.UserId)).CreateDMChannelAsync();

                                        var message = await dmChannel.GetMessageAsync(invitedUser.DmQuestionMessageId);

                                        await message.DeleteAsync();

                                        await dmChannel.SendMessageAsync("Time ran out. Invite has been declined");
                                    }
                                    catch (Exception ex)
                                    {
                                        logger.LogError("Error declining invite", ex);
                                    }

                                    waitingListDataContext.Update(invitedUser);

                                    waitingListDataContext.SaveChanges();

                                    await CommandWaitingListModule.UpdateInviteMessageAsync(guild, invitedUser.Invite);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Timeout timer failed to run");
            }
            timerRunning = false;
        }
Beispiel #3
0
        public async Task Start()
        {
            var message = await GetMessageAsync(Context.Guild, guildData);

            if (message != null)
            {
                await Context.Message.ReplyAsync("Waiting list is already open");

                return;
            }

            waitingList.ClearUsers();

            var waitingListChannel = Context.Guild.GetTextChannel(guildData.WaitingListChannelId);

            message = await waitingListChannel.SendMessageAsync("Join the waiting list now!");

            guildData.PublicMessageId = message.Id;
            guildData.IsEnabled       = true;
            guildData.IsPaused        = false;
            dataContext.Update(guildData);
            dataContext.SaveChanges();
            // await message.AddReactionAsync(storage.ReactionEmote);

            await UpdatePublicMessageAsync(waitingList, Context.Guild, guildData);

            var modMessage = await Context.Message.ReplyAsync("Waiting list has been started");

            ComponentBuilder componentBuilder = new ComponentBuilder();

            componentBuilder.WithButton("Clear counters", customId: "clearcounters");

            await modMessage.ModifyAsync(p =>
            {
                p.Components = componentBuilder.Build();
            });
        }
Beispiel #4
0
 public async Task SetAsSubRole([Summary("The role of subscribers.")] IRole role)
 {
     guildData.SubRoleId = role.Id;
     dataContext.Update(guildData);
     dataContext.SaveChanges();
     await Context.Message.ReplyAsync("Sub role has been set");
 }
Beispiel #5
0
        private async Task Client_InteractionCreated(SocketInteraction arg)
        {
            try
            {
                using (var waitingListDataContext = new WaitingListDataContext())
                {
                    if (arg.Type == InteractionType.MessageComponent)
                    {
                        var parsedArg = (SocketMessageComponent)arg;

                        var customId = parsedArg.Data.CustomId;

                        var guild = (parsedArg.Channel as IGuildChannel)?.Guild;


                        var guildData = guild != null?waitingListDataContext.GetGuild(guild.Id) : null;

                        var waitingList = guild != null ? new CommandWaitingList(waitingListDataContext, client.Rest, guild.Id) : null;

                        if (arg.User.IsBot)
                        {
                            return;
                        }
                        if (parsedArg.Message.Id == guildData?.PublicMessageId)
                        {
                            if (guild == null || guildData == null)
                            {
                                logger.LogCritical("Guild or guildData was null in InteractionCreated");
                                return;
                            }
                            if (customId == "join")
                            {
                                if ((await waitingList !.AddUserAsync((IGuildUser)parsedArg.User)).Success)
                                {
                                    // await parsedArg.RespondAsync("Joined Waiting list.", ephemeral: true);
                                }
                                else
                                {
                                    // await arg.RespondAsync("Failed");
                                    logger.LogError("Failed to join " + parsedArg.User);
                                }
                            }
                            else if (customId == "leave")
                            {
                                await waitingList !.RemoveUserAsync(parsedArg.User.Id);
                                //await parsedArg.RespondAsync("Left waiting list.", ephemeral: true);
                            }

                            waitingListDataContext.SaveChanges();
                            guildData = waitingListDataContext.GetGuild(guild.Id);

                            await ButtonWaitingListModule.UpdatePublicMessageAsync(waitingList !, guild !, guildData);

                            await parsedArg.AcknowledgeAsync();
                        }
                        else
                        {
                            if (customId == "unpause")
                            {
                                guildData !.IsPaused = false;
                                waitingListDataContext.Update(guildData);
                                waitingListDataContext.SaveChanges();

                                await ButtonWaitingListModule.UpdatePublicMessageAsync(waitingList, guild !, guildData);

                                await parsedArg.AcknowledgeAsync();

                                await parsedArg.Message.DeleteAsync();
                            }
                            else if (customId == "clearcounters")
                            {
                                foreach (var user in guildData !.UsersInGuild)
                                {
                                    user.PlayCount = 0;
                                    waitingListDataContext.Update(user);
                                }
                                waitingListDataContext.SaveChanges();

                                await ButtonWaitingListModule.UpdatePublicMessageAsync(waitingList !, guild !, guildData);

                                await parsedArg.RespondAsync("Counters have been cleared");
                            }
                            else if (customId.StartsWith("joinYes") || customId.StartsWith("joinNo"))
                            {
                                var parts = customId.Split(new[] { ';' }, 2);

                                var inviteId = int.Parse(parts[1]);

                                var invite = waitingListDataContext.Invites.Include(i => i.Guild).Include(i => i.InvitedUsers).ThenInclude(iu => iu.User).Single(i => i.Id == inviteId);

                                var invitedUser = invite.InvitedUsers.Last(x => x.User.UserId == parsedArg.User.Id);

                                guildData   = invite.Guild;
                                waitingList = new CommandWaitingList(waitingListDataContext !, client.Rest, guildData.GuildId);

                                guild = client.GetGuild(guildData.GuildId);

                                if (invitedUser.InviteAccepted != null)
                                {
                                    return;
                                }

                                if (parts[0] == "joinYes")
                                {
                                    await parsedArg.Message.DeleteAsync();

                                    await parsedArg.Channel.SendMessageAsync(string.Format(guildData.DMMessageFormat, invite.FormatData));

                                    invitedUser.InviteAccepted = true;
                                    waitingListDataContext.Update(invitedUser);
                                    waitingListDataContext.SaveChanges();
                                    await CommandWaitingListModule.UpdateInviteMessageAsync(guild, invite);
                                }
                                else
                                {
                                    await parsedArg.Message.DeleteAsync();

                                    await parsedArg.Channel.SendMessageAsync("Invite has been declined");
                                    await DeclineInviteAsync(client.Rest, guild, waitingList, invitedUser);

                                    waitingListDataContext.Update(invitedUser);
                                    waitingListDataContext.SaveChanges();
                                    await CommandWaitingListModule.UpdateInviteMessageAsync(guild, invite);
                                }
                            }
                        }
                    }
                }
            }