internal static string WriteTag(KeyValTag tag, string ownerName)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(Format.Bold($"Tag: {tag.Key} (Owner: {ownerName})"));
            sb.Append(tag.Value);
            return(sb.ToString());
        }
Example #2
0
        private async Task ReactionAdded(Cacheable <IUserMessage, ulong> cacheable, ISocketMessageChannel channelParam, SocketReaction reaction)
        {
            // remove user reaction if rate limited
            if (reaction.User.IsSpecified &&
                IsRateLimited(reaction.UserId))
            {
                var msg = await cacheable.DownloadAsync();

                await msg.RemoveReactionAsync(reaction.Emote, reaction.User.Value, new RequestOptions { AuditLogReason = $"User is rate limited until {RateLimitedUsers[reaction.User.Value.Id].ToString()}" });
            }
            // vote deletion here
            else if (channelParam is SocketTextChannel channel)
            {
                GuildConfig config;
                if ((config = ConfigManager.GetManagedConfig(channel.Guild.Id)) == null)
                {
                    return;
                }

                if (reaction.Emote.Name == "⛔")
                {
                    var msg = await cacheable.DownloadAsync();

                    if (config.IsVoteDeleteImmune(msg.Author.Id) ||
                        msg.Author is SocketGuildUser gu && gu.Roles.Any(x => config.IsVoteDeleteImmune(x.Id)))
                    {
                        return;
                    }

                    // get the emote, count the total reactions, and get those users
                    var emote = msg.Reactions.FirstOrDefault(x => x.Key.Name.Equals("⛔"));
                    var count = emote.Value.ReactionCount;
                    var users = (await msg.GetReactionUsersAsync(emote.Key, limit: count))
                                .Select(x => channel.Guild.GetUser(x.Id))
                                .Where(x => x != null)
                                .Cast <IGuildUser>();

                    // if we match the requirements for vote removal, proceed
                    if (config.MatchesVoteDeleteRequirements(users.ToArray()))
                    {
                        await msg.DeleteAsync(new RequestOptions { AuditLogReason = "Message was voted to be deleted." });
                    }
                }
                else
                {
                    // tag list controlling by emoji
                    var msg = await cacheable.DownloadAsync();

                    var cachedTagList = config.TagListCache.FirstOrDefault(x => x.message.Id == msg.Id);

                    // valid reactor
                    if (cachedTagList != null && cachedTagList.originalMessage.Author.Id == reaction.UserId && reaction.UserId != _client.CurrentUser.Id)
                    {
                        // print a selected tag 0..9
                        if (TagModule._tagsNumberStrings.Values.Contains(reaction.Emote.Name))
                        {
                            var       tags   = cachedTagList.containedTags;
                            int       index  = TagModule._tagsNumberStrings.Select(x => x.Value).ToList().IndexOf(reaction.Emote.Name);
                            KeyValTag theTag = tags[index + 10 * (cachedTagList.currentPage - 1)];
                            await channel.SendMessageAsync(TagModule.WriteTag(theTag, channel.Guild.GetUser(theTag.OwnerId).FullName()));

                            await msg.DeleteAsync();

                            await cachedTagList.originalMessage.DeleteAsync();

                            config.TagListCache.Remove(cachedTagList);
                        }
                        // paginate
                        else if (reaction.Emote.Name.Equals("\u25c0") || reaction.Emote.Name.Equals("\u25b6"))
                        {
                            var  tags       = cachedTagList.containedTags.AsEnumerable();
                            int  totalPages = (int)Math.Ceiling((float)tags.Count() / 10f);
                            int  page;
                            bool flag;
                            if (reaction.Emote.Name.Equals("\u25c0"))
                            {
                                page = cachedTagList.currentPage - 1;
                                flag = page >= 1;
                            }
                            else
                            {
                                page = cachedTagList.currentPage + 1;
                                flag = page <= totalPages;
                            }

                            if (flag)
                            {
                                TagModule.Paginate(tags.Count(), ref page, ref tags, ref totalPages);

                                var sb = new StringBuilder();
                                TagModule.AppendTags(sb, tags, page, totalPages, channel.Guild);

                                await msg.ModifyAsync(x => x.Content = sb.ToString());

                                cachedTagList.currentPage = page;
                                cachedTagList.RefreshExpiry();
                            }
                        }
                    }
                }
            }
        }