コード例 #1
0
        public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
        {
            input = input.Replace("`", "").Replace(@"\", "").Trim();

            // Find by name
            foreach (var emote in EmotesHelper.Emotes)
            {
                if (input.Equals($":{EmotesHelper.GetEmojiName(emote)}:", StringComparison.CurrentCultureIgnoreCase))
                {
                    return(Task.FromResult(TypeReaderResult.FromSuccess(EmotesHelper.GetEmoji(emote))));
                }
            }

            // Find by unicode value
            foreach (var emote in EmotesHelper.Emojis)
            {
                if (input.Equals(emote.Name, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(Task.FromResult(TypeReaderResult.FromSuccess(emote)));
                }
            }

            // Check guild-emotes
            var guildEmote = context.Guild.Emotes.FirstOrDefault(e => string.Equals($"<:{e.Name}:{e.Id}>", input, StringComparison.CurrentCultureIgnoreCase) || string.Equals($"<a:{e.Name}:{e.Id}>", input, StringComparison.CurrentCultureIgnoreCase));

            if (guildEmote != null)
            {
                return(Task.FromResult(TypeReaderResult.FromSuccess(guildEmote)));
            }
            return(Task.FromResult(TypeReaderResult.FromError(CommandError.Unsuccessful, "Unable to find a valid emote")));
        }
コード例 #2
0
        public async Task <bool> Fix(bool silent = false)
        {
            if (!Permissions.IsBotOwner(Context))
            {
                if (!silent)
                {
                    await Context.ApplyResultReaction(CommandResult.FailedUserPermission).ConfigureAwait(false);
                }
                return(false);
            }

            int fixedCount = 0;

            foreach (var link in _links)
            {
                if (link.Links.Count > 0 && ulong.TryParse(link.Value, out ulong messageId))
                {
                    var channelPermissions = (await link.Guild.GetCurrentUserAsync().ConfigureAwait(false))?.GetPermissions(link.Channel);
                    if (channelPermissions?.AddReactions == true)
                    {
                        var message = await GetMessageAsync(link.Guild, messageId, link.Channel).ConfigureAwait(false);

                        if (message != null)
                        {
                            foreach (var messageLink in link.Links)
                            {
                                var emoteValue = messageLink.Identity.Split('=', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
                                if (message != null)
                                {
                                    var emote = ulong.TryParse(emoteValue, out ulong emoteId)
                                        ? EmotesHelper.GetGuildEmoteById(link.Guild, emoteId)
                                        : EmotesHelper.GetEmojiFromString(emoteValue);
                                    if (emote != null)
                                    {
                                        if (!message.Reactions.Any(r => r.Value.IsMe && string.Equals(r.Key.Name, emote.Name, StringComparison.CurrentCultureIgnoreCase)))
                                        {
                                            // Check if an identical emote exists but in a different capital casing.
                                            var emoteCaseInsensitive = message.Reactions.FirstOrDefault(r => string.Equals(r.Key.Name, emote.Name, StringComparison.CurrentCultureIgnoreCase)).Key;

                                            await message.AddReactionAsync(emoteCaseInsensitive ?? emote).ConfigureAwait(false);

                                            fixedCount++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!silent)
            {
                await Context.ApplyResultReaction(fixedCount == 0?CommandResult.SuccessAlt1 : CommandResult.Success).ConfigureAwait(false);
            }
            return(true);
        }
コード例 #3
0
        public async Task Info(string fromHash = null)
        {
            if (Permissions.IsAdministratorOrBotOwner(Context))
            {
                var buildInfo = await CheckForUpdates(false).ConfigureAwait(false);

                if (buildInfo.LocalHash != null && buildInfo.RemoteHash != null)
                {
                    var embedBuilder = new EmbedBuilder()
                                       .WithTitle($"\\{EmotesHelper.GetString(Emotes.HammerPick)} Build Info")
                                       .WithOkColour(Context.Guild)
                    ;

                    var updateList = await UpdateList(fromHash, false).ConfigureAwait(false);

                    if (updateList.Count() > 0)
                    {
                        embedBuilder.WithDescription($"You are {updateList.Count()} commits behind.").WithFields(
                            updateList.Select(x => new EmbedFieldBuilder().WithName(x.Name).WithValue(x.Value))
                            );
                    }
                    else
                    {
                        embedBuilder.WithDescription($"Ditto is running on the last available version \\{EmotesHelper.GetString(Emotes.HeavyCheckMark)}");
                    }

                    await Context.Channel.EmbedAsync(embedBuilder, options : new RequestOptions()
                    {
                        RetryMode = RetryMode.RetryRatelimit
                    }).ConfigureAwait(false);
                }
                else
                {
                    await Context.ApplyResultReaction(CommandResult.Failed).ConfigureAwait(false);
                }
            }
            else
            {
                await Context.ApplyResultReaction(CommandResult.FailedUserPermission).ConfigureAwait(false);
            }
        }
コード例 #4
0
        public static async Task AddReactionsAsync(this IMessage message, CancellationToken cancellationToken, params Emotes[] emotes)
        {
            var errorCount = 0;

            for (int i = 0; i < emotes.Length;)
            {
                var emote = emotes[i];
                try
                {
                    await message.AddReactionAsync(
                        EmotesHelper.GetEmoji(emote),
                        new RequestOptions()
                    {
                        CancelToken = cancellationToken,
                        RetryMode   = RetryMode.AlwaysRetry
                    }
                        ).ConfigureAwait(false);

                    i++;
                    errorCount = 0;
                }
                catch (Exception ex)
                {
                    if (errorCount > 2)
                    {
                        Log.Warn($"AddReactionsAsync: Skipping {i}:{emote}");
                        i++;
                    }
                    else
                    {
                        if (!(ex is RateLimitedException))
                        {
                            Log.Error($"AddReactionsAsync, {i}:{emote} | {ex}");
                            errorCount++;
                        }
                    }
                }
            }
        }
コード例 #5
0
        public async Task Prune(int count = 100, IUser user = null, [Multiword] string pattern = null)
        {
            if (!Permissions.IsAdministratorOrBotOwner(Context))
            {
                await Context.ApplyResultReaction(CommandResult.FailedUserPermission).ConfigureAwait(false);

                return;
            }

            await Context.Message.DeleteAsync().ConfigureAwait(false);

            await Task.Delay(100).ConfigureAwait(false);

            if (count > 10)
            {
                var usedReaction = await Context.SendOptionDialogueAsync(
                    $"{Context.User.Mention} Please verify. Do you wish to delete {count} messages from this channel?",
                    new List <string>(), true,
                    new[] { EmotesHelper.GetEmoji(Emotes.WhiteCheckMark), EmotesHelper.GetEmoji(Emotes.NoEntrySign) }, null, 60000, 0
                    ).ConfigureAwait(false);

                if (usedReaction != 1)
                {
                    return;
                }
            }

            if (count > 100)
            {
                count = 100;
            }
            else if (count <= 0)
            {
                count = 1;
            }

            var list = (await Context.Channel.GetMessagesAsync(count).ToListAsync())
                       .SelectMany(i => i);

            if (!string.IsNullOrEmpty(pattern))
            {
                list = list.Where(i => i.Content.Contains(pattern));
            }

            if (user != null)
            {
                list = list.Where(i => i.Author?.Id == user.Id);
            }

            foreach (var msg in list)
            {
                try
                {
                    if (!msg.IsPinned)
                    {
                        await msg.DeleteAsync().ConfigureAwait(false);
                    }
                }
                catch { }
            }
        }