コード例 #1
0
ファイル: DiscordBot.cs プロジェクト: Davudi2134/Karuta
        public void SaveData()
        {
            string output = "";

            foreach (DiscordCommand C in interpreter.GetCommands())
            {
                if (C.GetType() == typeof(DiscordImageCommand))
                {
                    DiscordImageCommand cmd = (DiscordImageCommand)C;
                    if (output == "")
                    {
                        output += cmd.ToString();
                    }
                    else
                    {
                        output += "`" + cmd.ToString();
                    }
                }
            }
            //Karuta.REGISTY.SetValue("discordImageCommands", (from DiscordCommand c in interpreter.GetCommands() where c.GetType() == typeof(DiscordImageCommand) select c));
        }
コード例 #2
0
        async void Add()
        {
            //Validate
            if (string.IsNullOrWhiteSpace(_name) && string.IsNullOrWhiteSpace(_url))
            {
                await _channel.SendMessage($"An image name and url must be specified, use !help {name} for more info");

                return;
            }

            if ((_url.Contains('`') || _url.Contains('{')))
            {
                await _channel.SendMessage("Invalid URL");

                return;
            }

            List <string> imageLinks = new List <string>();
            Uri           url        = new Uri(_url);

            if (url.Host == "i.imgur.com")
            {
                if (_bot.validExtensions.Contains(Path.GetExtension(url.AbsolutePath)))
                {
                    imageLinks.Add(_url);
                }
            }
            else if (url.Host != "imgur.com")
            {
                await _channel.SendMessage("Only imgur images are allowed");

                return;
            }

            //Resolve Link
            if (imageLinks.Count == 0)
            {
                imageLinks = await _bot.ResolveImgurUrl(url);
            }
            //Add Command
            if (_bot.interpreter.CommandExists(_name))
            {
                DiscordCommand c = _bot.interpreter.GetCommand(_name);
                if (c?.GetType() == typeof(DiscordImageCommand))
                {
                    DiscordImageCommand cmd = (DiscordImageCommand)c;
                    int dupeCount           = 0;
                    int addCount            = 0;
                    if (!string.IsNullOrWhiteSpace(_help))
                    {
                        cmd.SetHelpMessage(_help);
                    }
                    foreach (string link in imageLinks)
                    {
                        if (cmd.images.Contains(link))
                        {
                            dupeCount++;
                            continue;
                        }
                        cmd.AddImage(link);
                        addCount++;
                    }
                    if (dupeCount > 0)
                    {
                        await _channel.SendMessage($"{dupeCount} Image{((dupeCount > 1) ? "s" : "")} already existed and were not added");
                    }
                    await _channel.SendMessage($"{addCount} Image{((addCount > 1) ? "s" : "")} Added");

                    //await _channel.SendMessage(cmd.ToString());
                }
                else
                {
                    await _channel.SendMessage("This name cannot be used");
                }
            }
            else
            {
                //Karuta.Write(count);
                DiscordImageCommand img = (_help != default(string)) ? new DiscordImageCommand(_name, _help)
                {
                    images = imageLinks
                } :new DiscordImageCommand(_name)
                {
                    images = imageLinks
                };
                _bot.interpreter.RegisterCommand(img);
                img.init?.Invoke();
                _bot.interpreter.GetCommandOfType <DiscordHelpCommand>()?.init();
                await _channel.SendMessage($"{imageLinks.Count} Image{((imageLinks.Count > 1) ? "s" : "")} Command Added");

                Karuta.Write(img.ToString());
            }
            //foreach (string c in bot.commands.Keys)
            //	_channel.SendMessage(c);
            _bot.SaveData();
            Karuta.InvokeCommand("save", new List <string>());
            _url = _name = null;
        }
コード例 #3
0
        async void Remove()
        {
            //Validate
            List <string> imageLinks = new List <string>();
            Uri           url        = new Uri(_url);

            if (!_removeEntirely)
            {
                if (_name == null || _url == null || _url == "" || _name == "")
                {
                    await _channel.SendMessage("An image name and url must be specified");

                    return;
                }
                if (_url.Contains('`') || _url.Contains('{'))
                {
                    await _channel.SendMessage("Invalid URL");

                    return;
                }

                if (url.Host == "i.imgur.com")
                {
                    if (_bot.validExtensions.Contains(Path.GetExtension(url.AbsolutePath)))
                    {
                        imageLinks.Add(_url);
                    }
                }
                else if (url.Host != "imgur.com")
                {
                    await _channel.SendMessage("Only imgur images are allowed");

                    return;
                }
            }
            else if (_name == null || _name == "")
            {
                await _channel.SendMessage("An image name must be specified");

                return;
            }
            //Resolve Link
            if (imageLinks.Count == 0 && !_removeEntirely)
            {
                imageLinks = await _bot.ResolveImgurUrl(url);
            }
            if (_bot.interpreter.CommandExists(_name))
            {
                int removed = 0, skipped = 0;
                if (_removeEntirely)
                {
                    _bot.interpreter.RemoveCommand(_name);
                }
                else
                {
                    if (_bot.interpreter.GetCommand(_name).GetType() != typeof(DiscordImageCommand))
                    {
                        await _channel.SendMessage("This is not an image command");
                    }
                    DiscordImageCommand cmd = ((DiscordImageCommand)_bot.interpreter.GetCommand(_name));
                    foreach (string i in cmd.images)
                    {
                        Karuta.Write(i);
                    }
                    foreach (string u in imageLinks)
                    {
                        if (cmd.RemoveImage(u))
                        {
                            removed++;
                        }
                        else
                        {
                            skipped++;
                        }
                    }
                    foreach (string i in cmd.images)
                    {
                        Karuta.Write(i);
                    }
                    if (cmd.images.Count == 0)
                    {
                        _bot.interpreter.RemoveCommand(_name);
                    }
                }
                await _channel.SendMessage($"{removed} Image{((removed > 1) ? "s" : "")} removed");

                if (skipped != 0)
                {
                    await _channel.SendMessage($"{skipped} Image{((skipped > 1) ? "s" : "")} were not found, and skipped");
                }
            }
            else
            {
                await _channel.SendMessage("that image command does not exsist");
            }
            _bot.interpreter.GetCommandOfType <HelpCommand>()?.init();
            _bot.SaveData();
            Karuta.InvokeCommand("save", new List <string>());
            _removeEntirely = false;
            _url            = _name = null;
        }