Ejemplo n.º 1
0
        public static async Task <Result <RestGuildUser> > GetGuildUser(this DiscordSocketRestClient client, ulong guildId, ulong userId)
        {
            var task   = client.GetGuildUserAsync(guildId, userId);
            var result = await Task.Run(() => task);

            return(result);
        }
Ejemplo n.º 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);
        }