Example #1
0
        public async Task ListAsync()
        {
            var embedBuilder = new EmbedBuilder
            {
                Color = Color.Green,
                Title = $"Waiting list{(guildData.IsEnabled ? "" : " (NOT ACTIVE)")}"
            };

            var sortedList  = guildData.GetSortedList();
            var description = "";
            int counter     = 0;

            foreach (var player in sortedList)
            {
                IGuildUser guildUser = Context.Guild.GetUser(player.UserId);
                description += $"**{++counter}.** {player.Name} ({guildUser?.Mention}) {(player.IsSub ? "(Sub) " : "")}";
                if (player.PlayCount > 0)
                {
                    description += $"(Played { player.PlayCount} time{ (player.PlayCount > 1 ? "s" : "")})";
                }
                description += "\r\n";
            }
            embedBuilder.Description = description;
            embedBuilder.AddField("\u200B", "[View this list in real time](https://wl.pdelvo.com/WaitingList/" + Context.Guild.Id + ")");

            Embed embed = embedBuilder.Build();
            await Context.Message.ReplyAsync($"Here are the next players in line:", embed : embed, allowedMentions : AllowedMentions.None);
        }
Example #2
0
        public async Task <(CommandResult commandResult, Invite?invite)> GetInvite(string[] arguments, int numberOfPlayers, ulong?inviteRole = null, bool?isInviteRolePositive = null, bool removeFromList = true)
        {
            IAsyncEnumerable <UserInGuild> asyncList = guildData.GetSortedList().ToAsyncEnumerable();

            if (inviteRole is ulong roleId)
            {
                asyncList = asyncList.WhereAwait(async x =>
                {
                    var restGuildUser = await restClient.GetGuildUserAsync(guildId, x.UserId);

                    var hasRole = restGuildUser.RoleIds.Contains(roleId);

                    return(hasRole == isInviteRolePositive);
                });
            }

            var list = await asyncList.ToListAsync();

            if (list.Count < numberOfPlayers)
            {
                return(CommandResult.FromError($"Did not send invites. There are only {list.Count} players in the list."), null);
            }

            string message;

            try
            {
                message = string.Format(guildData.DMMessageFormat, arguments);
            }
            catch (Exception)
            {
                return(CommandResult.FromError("The arguments had the wrong format"), null);
            }

            var invite = new Invite
            {
                FormatData           = arguments,
                Guild                = guildData,
                InvitedUsers         = new List <InvitedUser>(),
                InviteTime           = DateTime.Now,
                NumberOfInvitedUsers = numberOfPlayers,
                InviteRole           = inviteRole,
                IsInviteRolePositive = isInviteRolePositive
            };

            dataContext.Invites.Add(invite);
            dataContext.SaveChanges();

            StringBuilder warnings = new StringBuilder();

            // Send invites
            for (int i = 0; i < numberOfPlayers; i++)
            {
                var player = list[i];

                player.IsInWaitingList = false;
                player.PlayCount++;

                dataContext.Update(player);
                dataContext.SaveChanges();

                var restGuildUser = await restClient.GetGuildUserAsync(guildId, player.UserId);

                try
                {
                    ComponentBuilder componentBuilder = new ComponentBuilder();
                    componentBuilder.WithButton("Yes", customId: $"joinYes;{invite.Id}");
                    componentBuilder.WithButton("No", customId: $"joinNo;{invite.Id}");


                    var userMessage = await restGuildUser.SendMessageAsync($"Are you ready to join? You have 1 minute to respond.", component : componentBuilder.Build());


                    InvitedUser invitedUser = new InvitedUser
                    {
                        Invite              = invite,
                        InviteTime          = DateTime.Now,
                        DmQuestionMessageId = userMessage.Id,
                        User = player
                    };

                    dataContext.InvitedUsers.Add(invitedUser);
                }
                catch (Exception ex)
                {
                    warnings.AppendLine($"Could not invite {restGuildUser?.Mention ?? player.Name}. Exception: {ex.Message}");
                }
            }

            this.dataContext.Update(invite);

            await this.dataContext.SaveChangesAsync();

            return(CommandResult.FromSuccess("Players have been invited." + (warnings.Length > 0 ? "\r\n" + warnings.ToString() : "")), invite);
        }