예제 #1
0
파일: Bot.cs 프로젝트: CFTheMaster/kotori
        public void PostRandomImage()
        {
            if (Images.Count < 1)
            {
                PopulateImages();
            }

            ImageResult result = Images[(int)(RNG.RandomUInt32() % Images.Count)];

            Images.Remove(result);
            SaveCache();

            SafetyRating maxRating = Config.Get(ConfigSection, "MaxRating", SafetyRating.Safe);

            if (result.FileHash.Trim().Length < 1 || result.FileExtension.Trim().Length < 1)
            {
                throw new BotException("Result was empty");
            }

            Log.Add($"Hash: {result.FileHash}{result.FileExtension}");

            byte[] image = new byte[0];

            try
            {
                image = result.Download();
            } catch (Exception ex)
            {
                throw new BotException(ex.Message, ex);
            }

            if (image.Length < 1)
            {
                throw new BotException("File was empty");
            }

            string savePath = Config.Get($"Bot.{Name}", "SavePath", string.Empty).Trim();

            if (savePath.Length > 0)
            {
                savePath = Path.Combine(savePath, result.FileHash + result.FileExtension);
                File.WriteAllBytes(savePath, image);
                Log.Add($"Saved file to {savePath}");
            }

            if (result.Rating > maxRating)
            {
                throw new BotException($"Rating was too high: {result.Rating}");
            }

            Post(image, result.PostUrl);
        }