Exemplo n.º 1
0
        ParseAsync(Parameter parameter, string value, CommandContext context)
        {
            if (LocalCustomEmoji.TryParse(value, out var localCustomEmoji))
            {
                return(TypeParserResult <IEmoji> .Successful(localCustomEmoji));
            }

            if (emojis == null)
            {
                await Locker.WaitAsync();

                try
                {
                    await GetEmojis();
                }
                finally
                {
                    Locker.Release();
                }
            }

            var match = emojis.FirstOrDefault(x => x.Surrogates == value ||
                                              x.NamesWithColons.Any(n => n.Equals(value, System.StringComparison.OrdinalIgnoreCase)) ||
                                              x.Names.Any(n => n.Equals(value, System.StringComparison.OrdinalIgnoreCase)));

            if (match != null)
            {
                var localEmoji = new LocalEmoji(match.Surrogates);
                return(TypeParserResult <IEmoji> .Successful(localEmoji));
            }

            return(TypeParserResult <IEmoji> .Unsuccessful("Invalid custom emoji format."));
        }
Exemplo n.º 2
0
        public static bool TryParse(string input, out IEmoji result)
        {
            result = null;

            if (LocalCustomEmoji.TryParse(input, out var emoji))
            {
                result = emoji;
                return(true);
            }

            var utf32 = Encoding.UTF32.GetBytes(input);

            for (var i = 0; i < utf32.Length; i += 4)
            {
                var codepoint = BitConverter.ToInt32(utf32, i);
                var any       = false;

                for (var j = 0; j < CodePoints.Length; j++)
                {
                    if (CodePoints[j] == codepoint)
                    {
                        any = true;
                        break;
                    }
                }

                if (!any)
                {
                    return(false);
                }
            }

            result = new LocalEmoji(input);
            return(true);
        }
        public override ValueTask <CheckResult> CheckAsync(object argument, CommandContext ctx)
        {
            var context = (AdminCommandContext)ctx;

            return(!LocalCustomEmoji.TryParse(((IEmoji)argument).MessageFormat, out _)
                ? CheckResult.Unsuccessful(context.Localize("require_custom_emoji"))
                : CheckResult.Successful);
        }
Exemplo n.º 4
0
        public async Task GenerateCardAsync()
        {
            await using (var db = new DataContext())
            {
                var roles = db.Roles.Where(x => x.GuildId == Context.Guild.Id.RawValue);

                StringBuilder roleBuilder = new StringBuilder();
                foreach (var roleDef in roles)
                {
                    if (Context.Guild.Roles.TryGetValue(roleDef.RoleId, out var role))
                    {
                        if (roleDef.IsCustomEmoji())
                        {
                            roleBuilder.AppendLine($"<{roleDef.GetEmojiString()}> {role.Mention}");
                        }
                        else
                        {
                            roleBuilder.AppendLine($"{roleDef.GetEmojiString()} {role.Mention}");
                        }
                    }
                }

                if (roleBuilder.Length == 0)
                {
                    await ReplyAsync("There are no roles... it's all a lie");

                    return;
                }

                var message = await ReplyAsync("", false,
                                               new LocalEmbedBuilder().WithDescription(roleBuilder.ToString()).Build());

                foreach (var role in roles)
                {
                    IEmoji emoji;
                    if (role.IsCustomEmoji())
                    {
                        Debug.Assert(role.EmojiId != null, "role.EmojiId != null");
                        emoji = new LocalCustomEmoji(role.EmojiId.Value, role.EmojiName, role.Animated);
                    }
                    else
                    {
                        emoji = new LocalEmoji(role.EmojiName);
                    }

                    await message.AddReactionAsync(emoji);
                }

                await db.RoleMessages.AddAsync(new ReactableRoleMessage
                {
                    ChannelId = message.ChannelId.RawValue,
                    GuildId   = Context.Guild.Id.RawValue,
                    MessageId = message.Id.RawValue
                });

                await db.SaveChangesAsync();
            }
        }
        /// <inheritdoc/>
        public override ValueTask <TypeParserResult <ICustomEmoji> > ParseAsync(Parameter parameter, string value, DiscordCommandContext context)
        {
            if (LocalCustomEmoji.TryParse(value, out var emoji))
            {
                return(Success(emoji));
            }

            return(Failure("Invalid custom emoji."));
        }
Exemplo n.º 6
0
        public ButtonAttribute(string emoji)
        {
            if (emoji == null)
            {
                throw new ArgumentNullException(nameof(emoji));
            }

            Emoji = LocalCustomEmoji.TryParse(emoji, out var customEmoji)
                ? customEmoji as IEmoji
                : new LocalEmoji(emoji);
        }
Exemplo n.º 7
0
 public static string GetUrl(IEmoji emoji)
 {
     return(emoji switch
     {
         Emoji e => FormatEmojiUrl(GetUnicodeCodePoints(e)),
         LocalEmoji localEmoji => FormatEmojiUrl(GetUnicodeCodePoints(localEmoji)),
         LocalCustomEmoji localCustomEmoji => localCustomEmoji.GetUrl(),
         CustomEmoji customEmoji => customEmoji.GetUrl(),
         CachedGuildEmoji cachedEmoji => cachedEmoji.GetUrl(),
         RestGuildEmoji restEmoji => restEmoji.GetUrl(),
         _ => throw new ArgumentOutOfRangeException(nameof(emoji))
     });
        /// <inheritdoc/>
        public override ValueTask <TypeParserResult <IGuildEmoji> > ParseAsync(Parameter parameter, string value, DiscordGuildCommandContext context)
        {
            if (LocalCustomEmoji.TryParse(value, out var emoji))
            {
                if (context.Guild.Emojis.TryGetValue(emoji.Id, out var guildEmoji))
                {
                    return(Success(guildEmoji));
                }

                return(Failure("The provided custom emoji is not from this guild."));
            }

            return(Failure("Invalid custom emoji."));
        }
Exemplo n.º 9
0
        public async Task SummonerAsync(Region region, [Remainder] string summonerName)
        {
            summonerName ??= DbContext.User.LeagueProfile.Username;

            if (string.IsNullOrWhiteSpace(summonerName))
            {
                await RespondEmbedAsync("You need to specify a nickname or set yours up.");

                return;
            }

            var summoner = await _riot.Summoner.GetSummonerByNameAsync(region, summonerName);

            if (summoner is null)
            {
                await RespondEmbedAsync("Unknown summoner. Try another region.");

                return;
            }

            var versions = await _staticEndpoints.Versions.GetAllAsync();

            var embed = EmbedHelper.New(Context, $"`{summoner.Name}` summoner's profile.");

            embed.ThumbnailUrl =
                $"http://ddragon.leagueoflegends.com/cdn/{versions.First()}/img/profileicon/{summoner.ProfileIconId}.png";
            embed.AddField("Level", summoner.Level);

            var leagues = await _riot.League.GetLeagueEntriesBySummonerAsync(region, summoner.Id);

            var trophy = new LocalEmoji("🏆").MessageFormat;
            var @new   = new LocalEmoji("🆕").MessageFormat;
            var fire   = new LocalEmoji("🔥").MessageFormat;

            var blackTrophy = new LocalCustomEmoji(556901212029452308, "black_trophy").MessageFormat;
            var blackNew    = new LocalCustomEmoji(556902437257216040, "black_new").MessageFormat;
            var blackFire   = new LocalCustomEmoji(556912883154550823, "black_fire").MessageFormat;

            foreach (var league in leagues)
            {
                embed.AddField($"{league.QueueType}",
                               $"`{league.Tier} {league.Rank} {league.LeaguePoints}LP`" +
                               $"\n`{league.Wins}W {league.Losses}D` (`{Math.Round((double) league.Wins / (league.Wins + league.Losses), 2) * 100}%WR`)" +
                               $"\n{(league.Veteran ? trophy : blackTrophy)}, {(league.HotStreak ? fire : blackFire)}, {(league.FreshBlood ? @new : blackNew)}{(league.Inactive ? "\n\n" + "You're inactive. Warning, you are able to lose some LP or ranks in this league." : "")}",
                               true);
            }

            await RespondAsync(embed.Build());
        }
Exemplo n.º 10
0
            private async Task <ICustomEmoji> GetEmojiAsync(string value)
            {
                if (LocalCustomEmoji.TryParse(value, out var emoji))
                {
                    return(Context.Guild !.Emojis.FirstOrDefault(x => x.Value.Id == emoji.Id).Value
                           ?? (ICustomEmoji)await Context.Guild !.GetEmojiAsync(emoji.Id));
                }

                if (Snowflake.TryParse(value, out var emojiId))
                {
                    return(Context.Guild !.Emojis.FirstOrDefault(x => x.Value.Id == emojiId).Value
                           ?? (ICustomEmoji)await Context.Guild !.GetEmojiAsync(emojiId));
                }

                value = value.Replace(" ", "_");
                return(Context.Guild !.Emojis.FirstOrDefault(e => string.Equals(e.Value.Name, value, StringComparison.OrdinalIgnoreCase)).Value);
            }