示例#1
0
 public async Task ForumCheck()
 {
     if (Xml.CommandAllowed("update", Context))
     {
         await Webscraper.NewForumPostCheck((IGuildChannel)Context.Channel);
     }
     try { await Context.Channel.SendMessageAsync(""); } catch { }
 }
示例#2
0
        public static async Task NewForumPostCheck(IGuildChannel channel)
        {
            if (!File.Exists("threads.txt"))
            {
                File.CreateText("threads.txt").Close();
            }                                               //Create threads.txt

            var defaultChannel     = (ITextChannel)channel; //Bot logs channel or wherever command is used
            var modShowcaseChannel = await channel.Guild.GetTextChannelAsync(UserSettings.Channels.ModShowcaseChannelId(channel.GuildId));

            int newSubmissions = 0; //Increments when new threads are detected

            await defaultChannel.SendMessageAsync("Beginning forum check, please wait...");

            //Create stringbuilder and add existing lines from threads.txt
            StringBuilder sb = new StringBuilder();

            foreach (var line in File.ReadLines("threads.txt"))
            {
                sb.AppendLine(line);
            }

            //Update threads.txt stringbuilder
            List <List <string> > threadList = Webscraper.DownloadForumPosts(channel); //Get threads in format: url|title|tsv|

            foreach (List <string> thread in threadList)
            {
                bool containsThread = false; //If threads.txt already contains entry
                foreach (var line in File.ReadLines("threads.txt"))
                {
                    if (line.StartsWith(thread[0]))
                    {
                        containsThread = true;
                    }
                }
                if (!containsThread)
                {
                    newSubmissions++; //tsv line starting with matching ID found
                    //Update TSV & announce mod showcase if line exists
                    if (thread[2] != "")
                    {
                        await NotifyAsync(modShowcaseChannel, thread.ToArray());
                    }
                    //Get ready to add line to threads.txt
                    sb.Append($"{thread[0]}|{thread[1]}|{thread[2]}" + Environment.NewLine);
                }
            }

            //Once it's done comparing, overwrite original local txt doc with new one
            using (StreamWriter writer = new StreamWriter("threads2.txt", false))
                writer.Write(sb.ToString());
            File.Delete("threads.txt");
            File.Move("threads2.txt", "threads.txt");

            //Update TSV even if there's no new submissions
            await defaultChannel.SendMessageAsync("Forum check complete! Updating TSV...");

            UpdateTSV(channel);
            Thread.Sleep(4000); //Wait for TSV to be updated

            //If there are new submissions...
            if (newSubmissions > 0)
            {
                await defaultChannel.SendMessageAsync("TSV update complete! Building HTML...");

                BuildHtml(channel.GuildId);
                Thread.Sleep(8000); //Wait for html to be built
                await defaultChannel.SendMessageAsync("HTML finished buildiing! Committing changes...");

                Thread.Sleep(8000); //Wait for git to finish committing
                GitCommit(channel.GuildId);
                await defaultChannel.SendMessageAsync("Changes committed! Pushing to Github...");

                Thread.Sleep(12000); //Wait for commit to be pushed
                GitPush(channel.GuildId);
                await defaultChannel.SendMessageAsync($"Update complete! Found {newSubmissions} new submissions.");
            }
            else
            {
                await defaultChannel.SendMessageAsync("Update complete! No new submissions found.");
            }
        }