Ejemplo n.º 1
0
        public void RenderPage(DiscordEmbedBuilder eb, DateTimeZone zone, IEnumerable <ListedMember> members)
        {
            string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(zone));

            foreach (var m in members)
            {
                var profile = $"**ID**: {m.Hid}";
                if (_fields.ShowDisplayName && m.DisplayName != null)
                {
                    profile += $"\n**Display name**: {m.DisplayName}";
                }
                if (_fields.ShowPronouns && m.Pronouns != null)
                {
                    profile += $"\n**Pronouns**: {m.Pronouns}";
                }
                if (_fields.ShowBirthday && m.Birthday != null)
                {
                    profile += $"\n**Birthdate**: {m.BirthdayString}";
                }
                if (_fields.ShowPronouns && m.ProxyTags.Count > 0)
                {
                    profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
                }
                if (_fields.ShowMessageCount && m.MessageCount > 0)
                {
                    profile += $"\n**Message count:** {m.MessageCount}";
                }
                if (_fields.ShowLastMessage && m.LastMessage != null)
                {
                    profile += $"\n**Last message:** {FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value))}";
                }
                if (_fields.ShowLastSwitch && m.LastSwitchTime != null)
                {
                    profile += $"\n**Last switched in:** {FormatTimestamp(m.LastSwitchTime.Value)}";
                }
                if (_fields.ShowDescription && m.Description != null)
                {
                    profile += $"\n\n{m.Description}";
                }
                if (_fields.ShowPrivacy && m.MemberPrivacy == PrivacyLevel.Private)
                {
                    profile += "\n*(this member is private)*";
                }

                eb.AddField(m.Name, profile.Truncate(1024));
            }
        }
Ejemplo n.º 2
0
        public void RenderPage(DiscordEmbedBuilder eb, DateTimeZone zone, IEnumerable <ListedMember> members, LookupContext ctx)
        {
            foreach (var m in members)
            {
                var profile = $"**ID**: {m.Hid}";
                if (_fields.ShowDisplayName && m.DisplayName != null && m.NamePrivacy.CanAccess(ctx))
                {
                    profile += $"\n**Display name**: {m.DisplayName}";
                }
                if (_fields.ShowPronouns && m.PronounsFor(ctx) is {} pronouns)
                {
                    profile += $"\n**Pronouns**: {pronouns}";
                }
                if (_fields.ShowBirthday && m.BirthdayFor(ctx) != null)
                {
                    profile += $"\n**Birthdate**: {m.BirthdayString}";
                }
                if (_fields.ShowProxyTags && m.ProxyTags.Count > 0)
                {
                    profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
                }
                if (_fields.ShowMessageCount && m.MessageCountFor(ctx) is {} count&& count > 0)
                {
                    profile += $"\n**Message count:** {count}";
                }
                if (_fields.ShowLastMessage && m.MetadataPrivacy.TryGet(ctx, m.LastMessage, out var lastMsg))
                {
                    profile += $"\n**Last message:** {DiscordUtils.SnowflakeToInstant(lastMsg.Value).FormatZoned(zone)}";
                }
                if (_fields.ShowLastSwitch && m.MetadataPrivacy.TryGet(ctx, m.LastSwitchTime, out var lastSw))
                {
                    profile += $"\n**Last switched in:** {lastSw.Value.FormatZoned(zone)}";
                }
                if (_fields.ShowDescription && m.DescriptionFor(ctx) is {} desc)
                {
                    profile += $"\n\n{desc}";
                }
                if (_fields.ShowPrivacy && m.MemberVisibility == PrivacyLevel.Private)
                {
                    profile += "\n*(this member is hidden)*";
                }

                eb.AddField(m.NameFor(ctx), profile.Truncate(1024));
            }
        }
Ejemplo n.º 3
0
        private bool IsLatchExpired(MessageContext ctx)
        {
            if (ctx.LastMessage == null)
            {
                return(true);
            }
            if (ctx.LatchTimeout == 0)
            {
                return(false);
            }

            var timeout = ctx.LatchTimeout.HasValue
                ? Duration.FromSeconds(ctx.LatchTimeout.Value)
                : DefaultLatchExpiryTime;

            var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value);

            return(_clock.GetCurrentInstant() - timestamp > timeout);
        }
Ejemplo n.º 4
0
        private async Task <PKMessage?> FindRecentMessage(Context ctx)
        {
            await using var conn = await _db.Obtain();

            var lastMessage = await _repo.GetLastMessage(conn, ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id);

            if (lastMessage == null)
            {
                return(null);
            }

            var timestamp = DiscordUtils.SnowflakeToInstant(lastMessage.Mid);

            if (_clock.GetCurrentInstant() - timestamp > EditTimeout)
            {
                return(null);
            }

            return(lastMessage);
        }
Ejemplo n.º 5
0
        public static Task <Channel> MatchChannel(this Context ctx)
        {
            if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var id))
            {
                return(Task.FromResult <Channel>(null));
            }

            if (!ctx.Cache.TryGetChannel(id, out var channel))
            {
                return(Task.FromResult <Channel>(null));
            }

            if (!DiscordUtils.IsValidGuildChannel(channel))
            {
                return(Task.FromResult <Channel>(null));
            }

            ctx.PopArgument();
            return(Task.FromResult(channel));
        }
Ejemplo n.º 6
0
        private bool ShouldProxy(Channel channel, Message msg, MessageContext ctx)
        {
            // Make sure author has a system
            if (ctx.SystemId == null)
            {
                return(false);
            }

            // Make sure channel is a guild text channel and this is a normal message
            if (!DiscordUtils.IsValidGuildChannel(channel))
            {
                return(false);
            }
            if (msg.Type != Message.MessageType.Default && msg.Type != Message.MessageType.Reply)
            {
                return(false);
            }

            // Make sure author is a normal user
            if (msg.Author.System == true || msg.Author.Bot || msg.WebhookId != null)
            {
                return(false);
            }

            // Make sure proxying is enabled here
            if (!ctx.ProxyEnabled || ctx.InBlacklist)
            {
                return(false);
            }

            // Make sure we have either an attachment or message content
            var isMessageBlank = msg.Content == null || msg.Content.Trim().Length == 0;

            if (isMessageBlank && msg.Attachments.Length == 0)
            {
                return(false);
            }

            // All good!
            return(true);
        }
Ejemplo n.º 7
0
        public static async Task <T> BusyIndicator <T>(this Context ctx, Func <Task <T> > f, string emoji = "\u23f3" /* hourglass */)
        {
            var task = f();

            // If we don't have permission to add reactions, don't bother, and just await the task normally.
            if (!DiscordUtils.HasReactionPermissions(ctx))
            {
                return(await task);
            }

            try
            {
                await Task.WhenAll(ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = emoji }), task);

                return(await task);
            }
            finally
            {
                var _ = ctx.Rest.DeleteOwnReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = emoji });
            }
        }
Ejemplo n.º 8
0
        public async Task Handle(Shard shard, MessageUpdateEvent evt)
        {
            if (evt.Author.Value?.Id == _client.User?.Id)
            {
                return;
            }

            // Edit message events sometimes arrive with missing data; double-check it's all there
            if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
            {
                return;
            }

            var channel = _cache.GetChannel(evt.ChannelId);

            if (!DiscordUtils.IsValidGuildChannel(channel))
            {
                return;
            }
            var guild       = _cache.GetGuild(channel.GuildId !.Value);
            var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId);

            // Only react to the last message in the channel
            if (lastMessage?.Id != evt.Id)
            {
                return;
            }

            // Just run the normal message handling code, with a flag to disable autoproxying
            MessageContext ctx;

            await using (var conn = await _db.Obtain())
                using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
                    ctx = await _repo.GetMessageContext(conn, evt.Author.Value !.Id, channel.GuildId !.Value, evt.ChannelId);

            var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);

            var botPermissions = _bot.PermissionsIn(channel.Id);
            await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy : false, guild : guild, channel : channel, botPermissions : botPermissions);
        }
Ejemplo n.º 9
0
        public async Task <DiscordEmbed> CreateMemberEmbed(PKSystem system, PKMember member, DiscordGuild guild, LookupContext ctx)
        {
            // string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));

            var name = member.NameFor(ctx);

            if (system.Name != null)
            {
                name = $"{name} ({system.Name})";
            }

            DiscordColor color;

            try
            {
                color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
            }
            catch (ArgumentException)
            {
                // Bad API use can cause an invalid color string
                // TODO: fix that in the API
                // for now we just default to a blank color, yolo
                color = DiscordUtils.Gray;
            }

            await using var conn = await _db.Obtain();

            var guildSettings = guild != null ? await conn.QueryOrInsertMemberGuildConfig(guild.Id, member.Id) : null;

            var guildDisplayName = guildSettings?.DisplayName;
            var avatar           = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);

            var groups = (await conn.QueryMemberGroups(member.Id)).Where(g => g.Visibility.CanAccess(ctx)).ToList();

            var eb = new DiscordEmbedBuilder()
                     // TODO: add URL of website when that's up
                     .WithAuthor(name, iconUrl: DiscordUtils.WorkaroundForUrlBug(avatar))
                     // .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
                     .WithColor(color)
                     .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}":"")}");

            var description = "";

            if (member.MemberVisibility == PrivacyLevel.Private)
            {
                description += "*(this member is hidden)*\n";
            }
            if (guildSettings?.AvatarUrl != null)
            {
                if (member.AvatarFor(ctx) != null)
                {
                    description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
                }
                else
                {
                    description += "*(this member has a server-specific avatar set)*\n";
                }
            }
            if (description != "")
            {
                eb.WithDescription(description);
            }

            if (avatar != null)
            {
                eb.WithThumbnail(avatar);
            }

            if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx))
            {
                eb.AddField("Display Name", member.DisplayName.Truncate(1024), true);
            }
            if (guild != null && guildDisplayName != null)
            {
                eb.AddField($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true);
            }
            if (member.BirthdayFor(ctx) != null)
            {
                eb.AddField("Birthdate", member.BirthdayString, true);
            }
            if (member.PronounsFor(ctx) is {} pronouns&& !string.IsNullOrWhiteSpace(pronouns))
            {
                eb.AddField("Pronouns", pronouns.Truncate(1024), true);
            }
            if (member.MessageCountFor(ctx) is {} count&& count > 0)
            {
                eb.AddField("Message Count", member.MessageCount.ToString(), true);
            }
            if (member.HasProxyTags)
            {
                eb.AddField("Proxy Tags", member.ProxyTagsString("\n").Truncate(1024), true);
            }
            // --- For when this gets added to the member object itself or however they get added
            // if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
            // if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
            // if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
            if (!member.Color.EmptyOrNull())
            {
                eb.AddField("Color", $"#{member.Color}", true);
            }

            if (groups.Count > 0)
            {
                // More than 5 groups show in "compact" format without ID
                var content = groups.Count > 5
                    ? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
                    : string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
                eb.AddField($"Groups ({groups.Count})", content.Truncate(1000));
            }

            if (member.DescriptionFor(ctx) is {} desc)
            {
                eb.AddField("Description", member.Description.NormalizeLineEndSpacing(), false);
            }

            return(eb.Build());
        }
Ejemplo n.º 10
0
        public async Task <DiscordEmbed> CreateMessageInfoEmbed(DiscordClient client, FullMessage msg)
        {
            var ctx     = LookupContext.ByNonOwner;
            var channel = await _client.GetChannel(msg.Message.Channel);

            var serverMsg = channel != null ? await channel.GetMessage(msg.Message.Mid) : null;

            // Need this whole dance to handle cases where:
            // - the user is deleted (userInfo == null)
            // - the bot's no longer in the server we're querying (channel == null)
            // - the member is no longer in the server we're querying (memberInfo == null)
            DiscordMember memberInfo = null;
            DiscordUser   userInfo   = null;

            if (channel != null)
            {
                memberInfo = await channel.Guild.GetMember(msg.Message.Sender);
            }
            if (memberInfo != null)
            {
                userInfo = memberInfo;                     // Don't do an extra request if we already have this info from the member lookup
            }
            else
            {
                userInfo = await client.GetUser(msg.Message.Sender);
            }

            // Calculate string displayed under "Sent by"
            string userStr;

            if (memberInfo != null && memberInfo.Nickname != null)
            {
                userStr = $"**Username:** {memberInfo.NameAndMention()}\n**Nickname:** {memberInfo.Nickname}";
            }
            else if (userInfo != null)
            {
                userStr = userInfo.NameAndMention();
            }
            else
            {
                userStr = $"*(deleted user {msg.Message.Sender})*";
            }

            // Put it all together
            var eb = new DiscordEmbedBuilder()
                     .WithAuthor(msg.Member.NameFor(ctx), iconUrl: DiscordUtils.WorkaroundForUrlBug(msg.Member.AvatarFor(ctx)))
                     .WithDescription(serverMsg?.Content?.NormalizeLineEndSpacing() ?? "*(message contents deleted or inaccessible)*")
                     .WithImageUrl(serverMsg?.Attachments?.FirstOrDefault()?.Url)
                     .AddField("System",
                               msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true)
                     .AddField("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true)
                     .AddField("Sent by", userStr, inline: true)
                     .WithTimestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset());

            var roles = memberInfo?.Roles?.ToList();

            if (roles != null && roles.Count > 0)
            {
                eb.AddField($"Account roles ({roles.Count})", string.Join(", ", roles.Select(role => role.Name)));
            }

            return(eb.Build());
        }
Ejemplo n.º 11
0
        public async ValueTask HandleLoggerBotCleanup(DiscordMessage msg)
        {
            if (msg.Channel.Type != ChannelType.Text)
            {
                return;
            }
            if (!msg.Channel.BotHasAllPermissions(Permissions.ManageMessages))
            {
                return;
            }

            // If this message is from a *webhook*, check if the name matches one of the bots we know
            // TODO: do we need to do a deeper webhook origin check, or would that be too hard on the rate limit?
            // If it's from a *bot*, check the bot ID to see if we know it.
            LoggerBot bot = null;

            if (msg.WebhookMessage)
            {
                _botsByWebhookName.TryGetValue(msg.Author.Username, out bot);
            }
            else if (msg.Author.IsBot)
            {
                _bots.TryGetValue(msg.Author.Id, out bot);
            }

            // If we didn't find anything before, or what we found is an unsupported bot, bail
            if (bot == null)
            {
                return;
            }

            try
            {
                // We try two ways of extracting the actual message, depending on the bots
                if (bot.FuzzyExtractFunc != null)
                {
                    // Some bots (Carl, Circle, etc) only give us a user ID and a rough timestamp, so we try our best to
                    // "cross-reference" those with the message DB. We know the deletion event happens *after* the message
                    // was sent, so we're checking for any messages sent in the same guild within 3 seconds before the
                    // delete event timestamp, which is... good enough, I think? Potential for false positives and negatives
                    // either way but shouldn't be too much, given it's constrained by user ID and guild.
                    var fuzzy = bot.FuzzyExtractFunc(msg);
                    if (fuzzy == null)
                    {
                        return;
                    }

                    using var conn = await _db.Obtain();

                    var mid = await conn.QuerySingleOrDefaultAsync <ulong?>(
                        "select mid from messages where sender = @User and mid > @ApproxID and guild = @Guild limit 1",
                        new
                    {
                        fuzzy.Value.User,
                        Guild    = msg.Channel.GuildId,
                        ApproxId = DiscordUtils.InstantToSnowflake(
                            fuzzy.Value.ApproxTimestamp - TimeSpan.FromSeconds(3))
                    });

                    if (mid == null)
                    {
                        return;              // If we didn't find a corresponding message, bail
                    }
                    // Otherwise, we can *reasonably assume* that this is a logged deletion, so delete the log message.
                    await msg.DeleteAsync();
                }
                else if (bot.ExtractFunc != null)
                {
                    // Other bots give us the message ID itself, and we can just extract that from the database directly.
                    var extractedId = bot.ExtractFunc(msg);
                    if (extractedId == null)
                    {
                        return;                      // If we didn't find anything, bail.
                    }
                    using var conn = await _db.Obtain();

                    // We do this through an inline query instead of through DataStore since we don't need all the joins it does
                    var mid = await conn.QuerySingleOrDefaultAsync <ulong?>(
                        "select mid from messages where original_mid = @Mid", new { Mid = extractedId.Value });

                    if (mid == null)
                    {
                        return;
                    }

                    // If we've gotten this far, we found a logged deletion of a trigger message. Just yeet it!
                    await msg.DeleteAsync();
                } // else should not happen, but idk, it might
            }
            catch (NotFoundException)
            {
                // Sort of a temporary measure: getting an error in Sentry about a NotFoundException from D#+ here
                // The only thing I can think of that'd cause this are the DeleteAsync() calls which 404 when
                // the message doesn't exist anyway - so should be safe to just ignore it, right?
            }
        }
Ejemplo n.º 12
0
        public async Task <DiscordEmbed> CreateMemberEmbed(PKSystem system, PKMember member, DiscordGuild guild, LookupContext ctx)
        {
            var name = member.Name;

            if (system.Name != null)
            {
                name = $"{member.Name} ({system.Name})";
            }

            DiscordColor color;

            try
            {
                color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
            }
            catch (ArgumentException)
            {
                // Bad API use can cause an invalid color string
                // TODO: fix that in the API
                // for now we just default to a blank color, yolo
                color = DiscordUtils.Gray;
            }

            var guildSettings = guild != null ? await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(guild.Id, member.Id)) : null;

            var guildDisplayName = guildSettings?.DisplayName;
            var avatar           = guildSettings?.AvatarUrl ?? member.AvatarUrl;

            var proxyTagsStr = string.Join('\n', member.ProxyTags.Select(t => $"`{t.ProxyString}`"));

            var eb = new DiscordEmbedBuilder()
                     // TODO: add URL of website when that's up
                     .WithAuthor(name, iconUrl: DiscordUtils.WorkaroundForUrlBug(avatar))
                     .WithColor(member.MemberPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
                     .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Created on {DateTimeFormats.ZonedDateTimeFormat.Format(member.Created.InZone(system.Zone))}");

            var description = "";

            if (member.MemberPrivacy == PrivacyLevel.Private)
            {
                description += "*(this member is private)*\n";
            }
            if (guildSettings?.AvatarUrl != null)
            {
                if (member.AvatarUrl != null)
                {
                    description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
                }
                else
                {
                    description += "*(this member has a server-specific avatar set)*\n";
                }
            }
            if (description != "")
            {
                eb.WithDescription(description);
            }

            if (avatar != null)
            {
                eb.WithThumbnailUrl(avatar);
            }

            if (!member.DisplayName.EmptyOrNull())
            {
                eb.AddField("Display Name", member.DisplayName.Truncate(1024), true);
            }
            if (guild != null && guildDisplayName != null)
            {
                eb.AddField($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true);
            }
            if (member.Birthday != null && member.MemberPrivacy.CanAccess(ctx))
            {
                eb.AddField("Birthdate", member.BirthdayString, true);
            }
            if (!member.Pronouns.EmptyOrNull() && member.MemberPrivacy.CanAccess(ctx))
            {
                eb.AddField("Pronouns", member.Pronouns.Truncate(1024), true);
            }
            if (member.MessageCount > 0 && member.MemberPrivacy.CanAccess(ctx))
            {
                eb.AddField("Message Count", member.MessageCount.ToString(), true);
            }
            if (member.HasProxyTags)
            {
                eb.AddField("Proxy Tags", string.Join('\n', proxyTagsStr).Truncate(1024), true);
            }
            if (!member.Color.EmptyOrNull() && member.MemberPrivacy.CanAccess(ctx))
            {
                eb.AddField("Color", $"#{member.Color}", true);
            }
            if (!member.Description.EmptyOrNull() && member.MemberPrivacy.CanAccess(ctx))
            {
                eb.AddField("Description", member.Description.NormalizeLineEndSpacing(), false);
            }

            return(eb.Build());
        }
Ejemplo n.º 13
0
        public async Task <Embed> CreateMessageInfoEmbed(FullMessage msg)
        {
            var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel);

            var ctx = LookupContext.ByNonOwner;

            Message serverMsg = null;

            try
            {
                serverMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
            }
            catch (ForbiddenException)
            {
                // no permission, couldn't fetch, oh well
            }

            // Need this whole dance to handle cases where:
            // - the user is deleted (userInfo == null)
            // - the bot's no longer in the server we're querying (channel == null)
            // - the member is no longer in the server we're querying (memberInfo == null)
            // TODO: optimize ordering here a bit with new cache impl; and figure what happens if bot leaves server -> channel still cached -> hits this bit and 401s?
            GuildMemberPartial memberInfo = null;
            User userInfo = null;

            if (channel != null)
            {
                GuildMember member = null;
                try
                {
                    member = await _rest.GetGuildMember(channel.GuildId !.Value, msg.Message.Sender);
                }
                catch (ForbiddenException)
                {
                    // no permission, couldn't fetch, oh well
                }

                if (member != null)
                {
                    // Don't do an extra request if we already have this info from the member lookup
                    userInfo = member.User;
                }
                memberInfo = member;
            }

            if (userInfo == null)
            {
                userInfo = await _cache.GetOrFetchUser(_rest, msg.Message.Sender);
            }

            // Calculate string displayed under "Sent by"
            string userStr;

            if (memberInfo != null && memberInfo.Nick != null)
            {
                userStr = $"**Username:** {userInfo.NameAndMention()}\n**Nickname:** {memberInfo.Nick}";
            }
            else if (userInfo != null)
            {
                userStr = userInfo.NameAndMention();
            }
            else
            {
                userStr = $"*(deleted user {msg.Message.Sender})*";
            }

            // Put it all together
            var eb = new EmbedBuilder()
                     .Author(new(msg.Member.NameFor(ctx), IconUrl: DiscordUtils.WorkaroundForUrlBug(msg.Member.AvatarFor(ctx))))
                     .Description(serverMsg?.Content?.NormalizeLineEndSpacing() ?? "*(message contents deleted or inaccessible)*")
                     .Image(new(serverMsg?.Attachments?.FirstOrDefault()?.Url))
                     .Field(new("System",
                                msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true))
                     .Field(new("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true))
                     .Field(new("Sent by", userStr, true))
                     .Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));

            var roles = memberInfo?.Roles?.ToList();

            if (roles != null && roles.Count > 0)
            {
                // TODO: what if role isn't in cache? figure out a fallback
                var rolesString = string.Join(", ", roles.Select(id => _cache.GetRole(id))
                                              .OrderByDescending(role => role.Position)
                                              .Select(role => role.Name));
                eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));
            }

            return(eb.Build());
        }
Ejemplo n.º 14
0
        public async Task <Embed> CreateGroupEmbed(Context ctx, PKSystem system, PKGroup target)
        {
            await using var conn = await _db.Obtain();

            var pctx        = ctx.LookupContextFor(system);
            var memberCount = ctx.MatchPrivateFlag(pctx) ? await _repo.GetGroupMemberCount(conn, target.Id, PrivacyLevel.Public) : await _repo.GetGroupMemberCount(conn, target.Id);

            var nameField = target.Name;

            if (system.Name != null)
            {
                nameField = $"{nameField} ({system.Name})";
            }

            uint color;

            try
            {
                color = target.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
            }
            catch (ArgumentException)
            {
                // There's no API for group colors yet, but defaulting to a blank color regardless
                color = DiscordUtils.Gray;
            }

            var eb = new EmbedBuilder()
                     .Author(new(nameField, IconUrl: DiscordUtils.WorkaroundForUrlBug(target.IconFor(pctx))))
                     .Color(color)
                     .Footer(new($"System ID: {system.Hid} | Group ID: {target.Hid} | Created on {target.Created.FormatZoned(system)}"));

            if (target.DisplayName != null)
            {
                eb.Field(new("Display Name", target.DisplayName, true));
            }

            if (!target.Color.EmptyOrNull())
            {
                eb.Field(new("Color", $"#{target.Color}", true));
            }

            if (target.ListPrivacy.CanAccess(pctx))
            {
                if (memberCount == 0 && pctx == LookupContext.ByOwner)
                {
                    // Only suggest the add command if this is actually the owner lol
                    eb.Field(new("Members (0)", $"Add one with `pk;group {target.Reference()} add <member>`!", false));
                }
                else
                {
                    eb.Field(new($"Members ({memberCount})", $"(see `pk;group {target.Reference()} list`)", false));
                }
            }

            if (target.DescriptionFor(pctx) is { } desc)
            {
                eb.Field(new("Description", desc));
            }

            if (target.IconFor(pctx) is {} icon)
            {
                eb.Thumbnail(new(icon));
            }

            return(eb.Build());
        }
Ejemplo n.º 15
0
        public static async Task RenderMemberList(this Context ctx, LookupContext lookupCtx, IDatabase db, SystemId system, string embedTitle, MemberListOptions opts)
        {
            // We take an IDatabase instead of a IPKConnection so we don't keep the handle open for the entire runtime
            // We wanna release it as soon as the member list is actually *fetched*, instead of potentially minutes later (paginate timeout)
            var members = (await db.Execute(conn => conn.QueryMemberList(system, opts.ToQueryOptions())))
                          .SortByMemberListOptions(opts, lookupCtx)
                          .ToList();

            var itemsPerPage = opts.Type == ListType.Short ? 25 : 5;
            await ctx.Paginate(members.ToAsyncEnumerable(), members.Count, itemsPerPage, embedTitle, Renderer);

            // Base renderer, dispatches based on type
            Task Renderer(DiscordEmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                // Add a global footer with the filter/sort string + result count
                eb.WithFooter($"{opts.CreateFilterString()}. {"result".ToQuantity(members.Count)}.");

                // Then call the specific renderers
                if (opts.Type == ListType.Short)
                {
                    ShortRenderer(eb, page);
                }
                else
                {
                    LongRenderer(eb, page);
                }

                return(Task.CompletedTask);
            }

            void ShortRenderer(DiscordEmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                // We may end up over the description character limit
                // so run it through a helper that "makes it work" :)
                eb.WithSimpleLineContent(page.Select(m =>
                {
                    if (m.HasProxyTags)
                    {
                        var proxyTagsString = m.ProxyTagsString();
                        if (proxyTagsString.Length > 100) // arbitrary threshold for now, tweak?
                        {
                            proxyTagsString = "tags too long, see member card";
                        }
                        return($"[`{m.Hid}`] **{m.NameFor(ctx)}** *(*{proxyTagsString}*)*");
                    }

                    return($"[`{m.Hid}`] **{m.NameFor(ctx)}**");
                }));
            }

            void LongRenderer(DiscordEmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                var zone = ctx.System?.Zone ?? DateTimeZone.Utc;

                foreach (var m in page)
                {
                    var profile = new StringBuilder($"**ID**: {m.Hid}");

                    if (m.DisplayName != null && m.NamePrivacy.CanAccess(lookupCtx))
                    {
                        profile.Append($"\n**Display name**: {m.DisplayName}");
                    }

                    if (m.PronounsFor(lookupCtx) is {} pronouns)
                    {
                        profile.Append($"\n**Pronouns**: {pronouns}");
                    }

                    if (m.BirthdayFor(lookupCtx) != null)
                    {
                        profile.Append($"\n**Birthdate**: {m.BirthdayString}");
                    }

                    if (m.ProxyTags.Count > 0)
                    {
                        profile.Append($"\n**Proxy tags**: {m.ProxyTagsString()}");
                    }

                    if (opts.IncludeMessageCount && m.MessageCountFor(lookupCtx) is {} count&& count > 0)
                    {
                        profile.Append($"\n**Message count:** {count}");
                    }

                    if (opts.IncludeLastMessage && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessage, out var lastMsg))
                    {
                        profile.Append($"\n**Last message:** {DiscordUtils.SnowflakeToInstant(lastMsg.Value).FormatZoned(zone)}");
                    }

                    if (opts.IncludeLastSwitch && m.MetadataPrivacy.TryGet(lookupCtx, m.LastSwitchTime, out var lastSw))
                    {
                        profile.Append($"\n**Last switched in:** {lastSw.Value.FormatZoned(zone)}");
                    }

                    if (opts.IncludeCreated && m.MetadataPrivacy.TryGet(lookupCtx, m.Created, out var created))
                    {
                        profile.Append($"\n**Created on:** {created.FormatZoned(zone)}");
                    }

                    if (m.DescriptionFor(lookupCtx) is {} desc)
                    {
                        profile.Append($"\n\n{desc}");
                    }

                    if (m.MemberVisibility == PrivacyLevel.Private)
                    {
                        profile.Append("\n*(this member is hidden)*");
                    }

                    eb.AddField(m.NameFor(ctx), profile.ToString().Truncate(1024));
                }
            }
        }
Ejemplo n.º 16
0
        private async ValueTask TryHandleProxyMessageReactions(MessageReactionAddEvent evt)
        {
            // Sometimes we get events from users that aren't in the user cache
            // We just ignore all of those for now, should be quite rare...
            if (!_cache.TryGetUser(evt.UserId, out var user))
            {
                return;
            }

            var channel = _cache.GetChannel(evt.ChannelId);

            // check if it's a command message first
            // since this can happen in DMs as well
            if (evt.Emoji.Name == "\u274c")
            {
                await using var conn = await _db.Obtain();

                var commandMsg = await _commandMessageService.GetCommandMessage(conn, evt.MessageId);

                if (commandMsg != null)
                {
                    await HandleCommandDeleteReaction(evt, commandMsg);

                    return;
                }
            }

            // Proxied messages only exist in guild text channels, so skip checking if we're elsewhere
            if (!DiscordUtils.IsValidGuildChannel(channel))
            {
                return;
            }

            // Ignore reactions from bots (we can't DM them anyway)
            if (user.Bot)
            {
                return;
            }

            switch (evt.Emoji.Name)
            {
            // Message deletion
            case "\u274C":     // Red X
            {
                await using var conn = await _db.Obtain();

                var msg = await _repo.GetMessage(conn, evt.MessageId);

                if (msg != null)
                {
                    await HandleProxyDeleteReaction(evt, msg);
                }

                break;
            }

            case "\u2753":     // Red question mark
            case "\u2754":     // White question mark
            {
                await using var conn = await _db.Obtain();

                var msg = await _repo.GetMessage(conn, evt.MessageId);

                if (msg != null)
                {
                    await HandleQueryReaction(evt, msg);
                }

                break;
            }

            case "\U0001F514": // Bell
            case "\U0001F6CE": // Bellhop bell
            case "\U0001F3D3": // Ping pong paddle (lol)
            case "\u23F0":     // Alarm clock
            case "\u2757":     // Exclamation mark
            {
                await using var conn = await _db.Obtain();

                var msg = await _repo.GetMessage(conn, evt.MessageId);

                if (msg != null)
                {
                    await HandlePingReaction(evt, msg);
                }
                break;
            }
            }
        }