static async Task RequestBeat(DateTime time)
        {
            if (time > CheckTime)
            {
                //Abbybot.print("running abc!!");
                CheckTime = CheckTime.AddMinutes(1);
                SocketTextChannel channel    = null;
                List <Character>  characters = await AbbyBooruCharacterSql.GetListFromSqlAsync();

                foreach (var character in characters)
                {
                    //Abbybot.print("{}");
                    //AbbybotGuild
                    bool          safe = true;
                    List <string> tags = new List <string>();
                    try
                    {
                        var Gl = Apis.Discord.__client.Guilds.ToList().Any(x => x.Id == character.guildId);

                        if (!Gl)
                        {
                            continue;
                        }
                        //Console.WriteLine($"{character.tag}, guild found!");
                        var G = Apis.Discord.__client.GetGuild(character.guildId);

                        var pref = await GuildSql.GetGuild(character.guildId);

                        if (pref.PrefAbbybot != Apis.Discord.__client.CurrentUser.Id)
                        {
                            continue;
                        }
                        channel = G.GetTextChannel(character.channelId);

                        tags.Add(character.tag);
                        //Console.WriteLine($"channel nsfw? {channel.IsNsfw}, character lewd? {character.IsLewd}");
                        safe = !channel.IsNsfw;
                    }
                    catch (Exception e)
                    {
                        //Console.WriteLine(e.ToString());
                        continue;
                    }
                    if (safe)
                    {
                        tags.Add("rating:safe");
                    }


                    Post[] charpicx = new Post[0];
                    try
                    {
                        charpicx = (await Apis.AbbyBooru.GetLatest(tags.ToArray())).ToArray();
                        if (charpicx == null)
                        {
                            continue;
                        }
                        if (charpicx.Length > 5)
                        {
                            charpicx = charpicx.Take(5).ToArray();
                        }
                        else
                        {
                            continue;
                        }
                    } catch {
                        //Console.WriteLine("aaaa");
                    }
                    List <img> nngs    = new List <img>();
                    var        postIds = (await AbbyBooruCharacterSql.GetLatestPostIdsAsync(character));

                    Post[] eeee = charpicx.Where(x => !postIds.Contains((ulong)x.id)).Take(5).ToArray();

                    foreach (var ex in eeee)
                    {
                        img nng = new img()
                        {
                            Id     = (ulong)ex.id,
                            imgurl = ex.fileUrl,
                            source = ex.source,
                            safe   = (ex.rating.Contains($"safe")),
                            GelId  = ex.id,
                            md5    = ex.md5
                        };
                        nngs.Add(nng);
                    }

                    for (int i = 0; i < nngs.Count; i++)
                    {
                        img sr = nngs[i];

                        EmbedBuilder embededodizer = new EmbedBuilder
                        {
                            ImageUrl = sr.imgurl
                        };

                        string fixedsource = "no source found";
                        try
                        {
                            if (sr.source != null)
                            {
                                fixedsource = sr.source.Replace("/member_illust.php?mode=medium&amp;illust_id=", "/en/artworks/");
                            }

                            embededodizer.AddField($"New picture of {character.tag.Replace("_", " ")} :)", $"[Source]({fixedsource})");
                            embededodizer.Color = Color.LightOrange;

                            await channel.SendMessageAsync("", false, embededodizer.Build());
                        }
                        catch { }

                        await AbbyBooruCharacterSql.AddLatestPostIdAsync(character.Id, sr.Id, sr.GelId);

                        if (character.Id == 3)
                        {
                            var dl = await FunAbbybotFactsSql.GetFactsList(true, "twitter");

                            Random r      = new Random();
                            var    ranfac = dl.Count > 0? dl[r.Next(0, dl.Count)].fact:"";

                            Tweet tweet = new()
                            {
                                message   = $"A new tweet just came in from gelbooru!!\n{ranfac}",
                                url       = sr.imgurl,
                                sourceurl = fixedsource,
                                GelId     = sr.GelId
                            };

                            await TweetQueueSql.Add(tweet, true);
                        }
                    }
                }
            }
        }
    }
示例#2
0
        public static async Task SendTweet(Action onSucceed = null, Action <string> onFail = null)
        {
            Abbybot.print("ayo");
            var abbybotchannels = await AbbybotSql.GetAbbybotChannelIdAsync();

            bool  tellnano = abbybotchannels.Count > 0;
            ulong guildId = 0, channelId = 0;
            var   er = r.Next(0, abbybotchannels.Count);

            if (tellnano)
            {
                var(g, ch) = abbybotchannels[er];
                guildId    = g; channelId = ch;
            }
            var c = Discord.__client?.GetGuild(guildId)?.GetTextChannel(channelId);

            if (abbybotchannels.Count > 0)
            {
                await c.SendMessageAsync("nano gonna work");
            }
            PingAbbybotClock.o = 6; //6 is the working state
            Tweet tweet = null;

            Task <Tweet>[] tweets = new Task <Tweet>[] {
                TweetQueueSql.Peek(),
                           TweetArchiveSql.Peek()
            };
            string location = "";
            int    i = 0, olo = 0;

            do
            {
                try {
                    tweet = await tweets[i];
                } catch (Exception e) { i++; if (i < tweets.Length)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            break;
                                        } }
                await AbbyBooru.GetPictureById((ulong)tweet.GelId, post =>
                {
                    tweet.url       = post.fileUrl;
                    tweet.sourceurl = post.source;
                    location        = post.fileUrl;
                });

                if (olo++ > 3)
                {
                    i++;
                    olo = 0;
                }
            } while (location == "" && i < tweets.Length);
            Console.Write($"location found? {location!=""}, i was bigger than or equal to dbs? {i>=tweets.Length}");

            if (i >= tweets.Length)
            {
                onFail?.Invoke("I was not able to find a picture...");
                return;
            }

            if (tweet == null)
            {
                P("no tweet was found");
                if (tellnano)
                {
                    await c.SendMessageAsync("I'm done working nano");

                    PingAbbybotClock.o = 1;
                }
                onFail?.Invoke("I was not able to find a picture...");
                return;
            }

            var tempfilepath = await ImageDownloader.DownloadImage(tweet.url);

            Console.WriteLine(tempfilepath);
            if (tempfilepath == null)
            {
                P("no image found.");
                if (tellnano)
                {
                    await c.SendMessageAsync("I'm done working nano");

                    PingAbbybotClock.o = 1;
                }

                onFail?.Invoke("I was not able to find a picture...");
                return;
            }

            TwitterUploadedMedia me;
            int tsrs = 0;

            do
            {
                FileStream file = new FileStream(tempfilepath, FileMode.Open);

                MediaFile m = new MediaFile {
                    Content = file
                };

                tsrs++;
                TwitterAsyncResult <TwitterUploadedMedia> media = await Twitter.ts.UploadMediaAsync(new UploadMediaOptions { Media = m });

                if (media == null)
                {
                    Abbybot.print("failed to upload media");
                    if (tellnano)
                    {
                        await c.SendMessageAsync("I'm done working nano");

                        PingAbbybotClock.o = 1;
                    }
                    onFail?.Invoke("I twitter didn't let me to upload the picture...");
                    return;
                }
                file.Dispose();
                Abbybot.print(media.Response.Response);

                if (media.Response.Response.ToLower().Contains("unrecognized"))
                {
                    Abbybot.print("media type unregognized");
                    await TweetQueueSql.Remove(tweet);
                    await SendTweet();

                    if (tellnano)
                    {
                        await c.SendMessageAsync("I'm done working nano");

                        PingAbbybotClock.o = 1;
                    }
                    onFail?.Invoke("twitter didn't know what i was trying to post... it was not an image...");
                    return;
                }
                me = media.Value;
            } while ((me == null) && (tsrs < 3));

            if (me == null)
            {
                await TweetArchiveSql.Add(tweet, true);

                await TweetQueueSql.Remove(tweet);
                await SendTweet();

                if (tellnano)
                {
                    await c.SendMessageAsync("I'm done working nano");

                    PingAbbybotClock.o = 1;
                }
                onFail?.Invoke("the media i tried to upload did not upload...");
                return;
            }

            string[]      s     = new string[] { me.Media_Id };
            int           tries = 0;
            TwitterStatus tweetvalue;

            do
            {
                tries++;
                Task <TwitterAsyncResult <TwitterStatus> > o;
                var added          = $"\n\n{tweet.sourceurl} #abigailwilliams #abbybot";
                int tweetCharLimit = 240;

                int characterLimit = tweetCharLimit - added.Length;
                var chrsToRemove   = tweet.message.Length - characterLimit;
                //string manipulation
                StringBuilder sb = new StringBuilder(tweet.message);
                if (chrsToRemove > 0)
                {
                    sb.Remove((sb.Length) - chrsToRemove, chrsToRemove);
                    sb.Remove(sb.Length - 3, 3).Append("...");
                }
                sb.Append(added);


                var containsTwitter  = tweet.sourceurl.Contains("twitter");
                var sendTweetOptions = containsTwitter ?
                                       new SendTweetOptions {
                    Status = sb.ToString()
                } :
                new SendTweetOptions {
                    Status = sb.ToString(), MediaIds = s
                };
                o = Twitter.ts.SendTweetAsync(sendTweetOptions);

                TwitterAsyncResult <TwitterStatus> tweeto = await o;


                string a = tweeto?.Response?.Error?.Message switch {
                    "Status is a duplicate." => await Archive(),
                    _ => tweeto?.Response?.Error?.Message
                };
                if (a == "archived")
                {
                    onFail?.Invoke("twitter said i already posted that tweet");
                    return;
                }
                if (a != null)
                {
                    Console.WriteLine(a);
                }
                tweetvalue = tweeto.Value;
            } while ((tweetvalue == null) && (tries <= 3));

            if ((tweetvalue == null) || (tries >= 3))
            {
                await Archive();

                onFail?.Invoke("no tweet was posted.");
                return;
            }

            if (tweetvalue != null)
            {
                Abbybot.print("sent tweet");
                onSucceed?.Invoke();
                SaveTweet(tweetvalue, tweet);
            }

            await Archive();

            if (tweetvalue == null)
            {
                await SendTweet();

                if (tellnano)
                {
                    await c.SendMessageAsync("I'm done working nano");

                    PingAbbybotClock.o = 1;
                }
                return;
            }
            if (tellnano)
            {
                await c.SendMessageAsync("I'm done working nano");

                PingAbbybotClock.o = 1; //6 is the working state
            }
            async Task <string> Archive()
            {
                try {
                    await TweetArchiveSql.Add(tweet, true);

                    await TweetQueueSql.Remove(tweet);

                    return("archived");
                } catch (Exception e) {
                    return($"Failed to archive {e}");
                }
            }
        }