Ejemplo n.º 1
0
        internal async Task <bool> HandleResponseAsync(BaseSocketClient client, T1 response)
        {
            bool valid = false;

            if (response is SocketMessage s)
            {
                valid = await RunChecksAsync(client, response).ConfigureAwait(false) && (IsUserRestricted || Users.Contains(s.Author));

                if (Deletion.HasFlag(DeletionOptions.Invalids) == true && !valid)
                {
                    await s.DeleteAsync().ConfigureAwait(false);
                }
                if (Deletion.HasFlag(DeletionOptions.Valid) == true && valid == true)
                {
                    await s.DeleteAsync().ConfigureAwait(false);
                }
            }
            if (response is SocketReaction r)
            {
                var user = r.User.Value as SocketUser ?? client.GetUser(r.UserId);
                valid = await RunChecksAsync(client, response).ConfigureAwait(false) && (IsUserRestricted || Users.Contains(user));

                if (Deletion.HasFlag(DeletionOptions.Invalids) == true && !valid)
                {
                    await r.DeleteAsync(client).ConfigureAwait(false);
                }
                if (Deletion.HasFlag(DeletionOptions.Valid) == true && valid == true)
                {
                    await r.DeleteAsync(client).ConfigureAwait(false);
                }
            }
            return(valid);
        }
Ejemplo n.º 2
0
        public async Task Feedback(ICommand command)
        {
            var user = (IUser)_client.GetUser(_botOptions.Value.OwnerID) ?? await _client.Rest.GetUserAsync(_botOptions.Value.OwnerID);

            var author = command.Message.Author;
            await user.SendMessageAsync($"Suggestion from **{author.Username}#{author.Discriminator}** ({author.Id}) on **{command.Guild.Name}**:\n\n" + command["Message"]);

            await command.ReplySuccess("Thank you for your suggestion!");
        }
Ejemplo n.º 3
0
        public static async Task DeleteAsync(this SocketReaction reaction, BaseSocketClient client)
        {
            var channel = reaction.Channel;
            var message = reaction.Message.IsSpecified
                ? reaction.Message.Value
                :await channel.GetMessageAsync(reaction.MessageId).ConfigureAwait(false) as SocketUserMessage;

            var user = reaction.User.IsSpecified
                ? reaction.User.Value
                : client.GetUser(reaction.UserId);

            await message.RemoveReactionAsync(reaction.Emote, user).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public async Task Ban(ICommand command, ILogger logger)
        {
            if (command["Users"].Repeats.Count > 10)
            {
                throw new Framework.Exceptions.IncorrectParametersCommandException("The maximum number of bans per command is 10.", false);
            }

            var banningUser        = (IGuildUser)command.Author;
            var banningUserTopRole = banningUser.GetTopRole()?.Position ?? 0;
            var result             = new StringBuilder();
            var bans = new Dictionary <ulong, (Task Task, string User, ulong UserId)>();

            foreach (var id in command["Users"].Repeats.Select(x => x.AsMentionOrId.Value))
            {
                string userName;
                var    guildUser = await command.Guild.GetUserAsync(id) ?? await _userFetcher.FetchGuildUserAsync(command.GuildId, id);

                if (guildUser != null)
                {
                    userName = $"{guildUser.GetFullName()} ({guildUser.Id})";
                    if (!banningUser.IsOwner() && (banningUserTopRole <= (guildUser.GetTopRole()?.Position ?? 0) || guildUser.IsOwner()))
                    {
                        result.AppendLine(_communicator.FormatFailure($"You don't have permission to ban user `{userName}` on this server."));
                        continue;
                    }
                }
                else
                {
                    var user = (IUser)_client.GetUser(id) ?? await _userFetcher.FetchUserAsync(id);

                    userName = user != null ? $"{user.GetFullName()} ({user.Id})" : id.ToString();
                }

                bans[id] = (command.Guild.AddBanAsync(id, Math.Min(command["DeleteDays"].AsInt ?? 0, 7), command["Reason"].HasValue ? command["Reason"].AsString : null), userName, id);
            }

            try
            {
                await Task.WhenAll(bans.Select(x => x.Value.Task));
            }
            catch (Exception)
            {
                // Handled below
            }

            foreach (var ban in bans.Values)
            {
                if (ban.Task.Exception != null && ban.Task.Exception.InnerException is Discord.Net.HttpException ex && ex.HttpCode == HttpStatusCode.Forbidden)
                {
                    result.AppendLine(_communicator.FormatFailure($"Missing permissions to ban user `{ban.User}`."));
                }
            internal static MpGameData?CopyFrom(IMpGameData?other, TGame?game, BaseSocketClient client)
            {
                if (other is null)
                {
                    return(null);
                }

                if (!(other is MpGameData data))
                {
                    data = new MpGameData
                    {
                        OpenToJoin     = other.OpenToJoin,
                        Game           = game,
                        Player         = game?.Players.SingleOrDefault(p => p.User.Id == other.PlayerUserId),
                        GameInProgress = other.GameInProgress,
                        JoinedUsers    = other.JoinedUsers.Select(id => client.GetUser(id)).ToImmutableArray()
                    };
                }
                return(data);
            }
Ejemplo n.º 6
0
        static public async Task <IUser> FindUserAsync(this BaseSocketClient client, ulong id)
        {
            var user = client.GetUser(id);

            if (user != null)
            {
                return(user);
            }

            foreach (var guild in client.Guilds)
            {
                await guild.DownloadUsersAsync();

                user = guild.GetUser(id);

                if (user != null)
                {
                    return(user);
                }
            }

            return(await client.Rest.GetUserAsync(id));
        }
Ejemplo n.º 7
0
        static public async Task <IUser> FindUserAsync(this BaseSocketClient client, string username, string discriminator)
        {
            var user = client.GetUser(username, discriminator);

            if (user != null)
            {
                return(user);
            }

            foreach (var guild in client.Guilds)
            {
                await guild.DownloadUsersAsync();

                user = guild.Users
                       .FirstOrDefault(o => o.Username == username && (string.IsNullOrEmpty(discriminator) || o.Discriminator == discriminator));

                if (user != null)
                {
                    break;
                }
            }

            return(user);
        }
Ejemplo n.º 8
0
        private static async Task <Embed> FormatLeaderboard(BaseSocketClient client, IEnumerable <FleetTrueskillRating> ranks, bool id)
        {
            async Task <string> FormatRankInfo(int count, FleetTrueskillRating info)
            {
                var skill = 100 * Math.Max(0, info.Rating.ConservativeEstimate);
                var user  = (IUser)client.GetUser(info.Fleet.OwnerId) ?? await client.Rest.GetUserAsync(info.Fleet.OwnerId);

                var name = user?.Username ?? info.Fleet.OwnerId.ToString();

                return($"{count}. **{info.Fleet.Name}** ({name}) - {skill:0000}" + (id ? $" (id:{info.Fleet.Id})" : ""));
            }

            var embed = new EmbedBuilder {
                Title  = "Yolol Fleet Leaderboard",
                Color  = Color.Purple,
                Footer = new EmbedFooterBuilder().WithText("A Cylon Project")
            };

            var count   = 1;
            var builder = new StringBuilder();

            foreach (var rank in ranks)
            {
                builder.AppendLine(await FormatRankInfo(count, rank));
                count++;
            }

            if (count == 0)
            {
                builder.AppendLine("Leaderboard is empty!");
            }

            embed.WithDescription(builder.ToString());

            return(embed.Build());
        }
Ejemplo n.º 9
0
 public static SocketUser GetOwner(this BaseSocketClient client)
 => client.GetUser(Config.Owner);
Ejemplo n.º 10
0
 public string GetUsername(BaseSocketClient client)
 {
     return(client?.GetUser(UserId)?.NameandDisc() ?? Username ?? "Unknown");
 }
        public virtual ISocketUserWrapper?GetUser(ulong id)
        {
            var rawUser = _baseSocketClient.GetUser(id);

            return(rawUser != null ? new SocketUserWrapper(rawUser) : null);
        }