コード例 #1
0
        private async Task ListRemindersAsync(ICommandContext e)
        {
            var instances = reminders.GetAllInstances(e.Author.Id);

            if (instances?.Count > 0)
            {
                instances = instances.OrderBy(x => x.Id).ToList();

                EmbedBuilder embed = new EmbedBuilder()
                                     .SetTitle($"⏰ {e.Locale.GetString("reminders")}")
                                     .SetColor(0.86f, 0.18f, 0.26f);

                foreach (var x in instances)
                {
                    string tx     = x.Context;
                    string status = "▶";

                    if (x.Context.Length > 30)
                    {
                        tx = new string(x.Context.Take(27).ToArray()) + "...";
                    }

                    if (x.RepeatReminder)
                    {
                        status = "🔁";
                    }

                    embed.Description +=
                        $"{status} `{x.Id.ToString().PadRight(3)} - {tx.PadRight(30)} : {x.TimeLeft.ToTimeString(e.Locale, true)}`\n";
                }
                await embed.ToEmbed().QueueToChannelAsync(e.Channel);
            }

            await e.ErrorEmbed(e.Locale.GetString("error_no_reminders"))
            .ToEmbed().QueueToChannelAsync(e.Channel);
        }
コード例 #2
0
        public async Task DoSafe(ICommandContext e)
        {
            ILinkable s = null;

            if (e.Arguments.Take(out string useArg))
            {
                string tags = e.Arguments.Pack.TakeAll();
                if (useArg.ToLower().StartsWith("use"))
                {
                    switch (useArg.Split(':')[1].ToLower())
                    {
                    case "safebooru":
                    {
                        s = await ImageboardProviderPool.GetProvider <SafebooruPost>().GetPostAsync(tags, ImageRating.SAFE);
                    }
                    break;

                    case "gelbooru":
                    {
                        s = await ImageboardProviderPool.GetProvider <GelbooruPost>().GetPostAsync(tags, ImageRating.SAFE);
                    }
                    break;

                    case "konachan":
                    {
                        s = await ImageboardProviderPool.GetProvider <KonachanPost>().GetPostAsync(tags, ImageRating.SAFE);
                    }
                    break;

                    case "e621":
                    {
                        s = await ImageboardProviderPool.GetProvider <E621Post>().GetPostAsync(tags, ImageRating.SAFE);
                    }
                    break;

                    default:
                    {
                        e.Channel.QueueMessage("I do not support this image host :(");
                    }
                    break;
                    }
                }
                else
                {
                    s = await ImageboardProviderPool.GetProvider <SafebooruPost>().GetPostAsync(tags, ImageRating.SAFE);
                }
            }
            else
            {
                string tags = e.Arguments.Pack.TakeAll();
                s = await ImageboardProviderPool.GetProvider <SafebooruPost>().GetPostAsync(tags, ImageRating.SAFE);
            }

            if (s == null)
            {
                await e.ErrorEmbed("We couldn't find an image with these tags!")
                .ToEmbed().QueueToChannelAsync(e.Channel);

                return;
            }

            e.Channel.QueueMessage(s.Url);
        }
コード例 #3
0
        public async Task NewCustomCommandAsync(ICommandContext e)
        {
            if (e.Arguments.Take(out string commandName))
            {
                if (commandName.Contains(' '))
                {
                    throw new InvalidCharacterException(" ");
                }

                if (e.EventSystem.GetCommandHandler <SimpleCommandHandler>()
                    .GetCommandByIdOrDefault(commandName) != null)
                {
                    throw new DuplicateComandException(commandName);
                }

                if (!e.Arguments.CanTake)
                {
                    // TODO (Veld): Command has no function body.
                    return;
                }

                string scriptBody = e.Arguments.Pack.TakeAll().TrimStart('`').TrimEnd('`');

                try
                {
                    var tokens = _tokenizer.Tokenize(scriptBody);
                    var values = tokens.Where(x => x.TokenType == Tokens.Argument)
                                 .Select(x => x.Value);

                    var context = new Dictionary <string, object>();
                    foreach (var v in values)
                    {
                        if (context.ContainsKey(v))
                        {
                            continue;
                        }
                        context.Add(v, "");
                    }

                    new Parser(tokens).Parse(context);
                }
                catch (Exception ex)
                {
                    await e.ErrorEmbed($"An error occurred when parsing your script: ```{ex.ToString()}```")
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                try
                {
                    var db = e.GetService <DbContext>();
                    await db.Set <CustomCommand>().AddAsync(new CustomCommand
                    {
                        CommandName = commandName.ToLowerInvariant(),
                        CommandBody = scriptBody,
                        GuildId     = e.Guild.Id.ToDbLong()
                    });

                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                await e.SuccessEmbed($"Created script '>{commandName}'")
                .QueueToChannelAsync(e.Channel);
            }
        }
コード例 #4
0
        public async Task PruneAsync(ICommandContext e, ulong target = 0, string filter = null)
        {
            IDiscordGuildUser invoker = await e.Guild.GetSelfAsync();

            if (!(await(e.Channel as IDiscordGuildChannel).GetPermissionsAsync(invoker)).HasFlag(GuildPermission.ManageMessages))
            {
                e.Channel.QueueMessage(e.Locale.GetString("miki_module_admin_prune_error_no_access"));
                return;
            }

            if (e.Arguments.Pack.Length < 1)
            {
                await new EmbedBuilder()
                .SetTitle("♻ Prune")
                .SetColor(119, 178, 85)
                .SetDescription(e.Locale.GetString("miki_module_admin_prune_no_arg"))
                .ToEmbed()
                .QueueToChannelAsync(e.Channel);
                return;
            }


            string args = e.Arguments.Pack.TakeAll();

            string[] argsSplit = args.Split(' ');
            target = e.Message.MentionedUserIds.Count > 0 ? (await e.Guild.GetMemberAsync(e.Message.MentionedUserIds.First())).Id : target;

            if (int.TryParse(argsSplit[0], out int amount))
            {
                if (amount < 0)
                {
                    await Utils.ErrorEmbed(e, e.Locale.GetString("miki_module_admin_prune_error_negative"))
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }
                if (amount > 100)
                {
                    await Utils.ErrorEmbed(e, e.Locale.GetString("miki_module_admin_prune_error_max"))
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }
            }
            else
            {
                await Utils.ErrorEmbed(e, e.Locale.GetString("miki_module_admin_prune_error_parse"))
                .ToEmbed().QueueToChannelAsync(e.Channel);

                return;
            }

            if (Regex.IsMatch(e.Arguments.Pack.TakeAll(), "\"(.*?)\""))
            {
                Regex regex = new Regex("\"(.*?)\"");
                filter = regex.Match(e.Arguments.Pack.TakeAll()).ToString().Trim('"', ' ');
            }

            await e.Message.DeleteAsync();             // Delete the calling message before we get the message history.

            IEnumerable <IDiscordMessage> messages = await e.Channel.GetMessagesAsync(amount);

            List <IDiscordMessage> deleteMessages = new List <IDiscordMessage>();

            amount = messages.Count();

            if (amount < 1)
            {
                await e.Message.DeleteAsync();

                await e.ErrorEmbed(e.Locale.GetString("miki_module_admin_prune_no_messages", ">"))
                .ToEmbed().QueueToChannelAsync(e.Channel);

                return;
            }
            for (int i = 0; i < amount; i++)
            {
                if (target != 0 && messages.ElementAt(i)?.Author.Id != target)
                {
                    continue;
                }

                if (filter != null && messages.ElementAt(i)?.Content.IndexOf(filter) < 0)
                {
                    continue;
                }

                if (messages.ElementAt(i).Timestamp.AddDays(14) > DateTime.Now)
                {
                    deleteMessages.Add(messages.ElementAt(i));
                }
            }

            if (deleteMessages.Count > 0)
            {
                await e.Channel.DeleteMessagesAsync(deleteMessages.ToArray());
            }

            string[] titles = new string[]
            {
                "POW!",
                "BANG!",
                "BAM!",
                "KAPOW!",
                "BOOM!",
                "ZIP!",
                "ZING!",
                "SWOOSH!",
                "POP!"
            };

            (await new EmbedBuilder
            {
                Title = titles[MikiRandom.Next(titles.Length - 1)],
                Description = e.Locale.GetString("miki_module_admin_prune_success", deleteMessages.Count),
                Color = new Color(1, 1, 0.5f)
            }.ToEmbed().QueueToChannelAsync(e.Channel))
            .ThenWait(5000)
            .ThenDelete();
        }