Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Timers;
- using MySql.Data;
- using MySql.Data.MySqlClient;
- namespace Kawa.ChatBot
- {
- public static class Reporter
- {
- private static string memoryFile = "furrygoat.txt";
- public static string ReporterURL = "localhost";
- public static string Channel = "#kawa";
- public static Timer timer;
- public static void StartReporting(string origin, string channel, params string[] stuff)
- {
- if (!Bot.CheckOps(origin))
- {
- Bot.Say("[clr]4[u]Access denied.", channel);
- return;
- }
- Bot.Say("[clr]02Furry goats activated, reporting to {0}...", channel, Channel);
- timer = new Timer();
- timer.Interval = 100 * 60; //One minute.
- timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
- timer.Start();
- }
- private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- var lastKnownTimestamp = 0;
- if (File.Exists(memoryFile))
- lastKnownTimestamp = int.Parse(File.ReadAllText(memoryFile));
- var creds = "server=" + ReporterURL + ";user=helmeted_kawa;password=marshmallows;database=helmeted_nikoboard;";
- var conn = new MySqlConnection(creds);
- DateTime d = new DateTime(1970, 1, 1);
- int now = (int)DateTime.Now.ToUniversalTime().Subtract(d).TotalSeconds;
- if (conn.State == System.Data.ConnectionState.Closed)
- conn.Open();
- var query = "select * from reports where time > " + (now - (2000 * 60)) + " and time > " + lastKnownTimestamp + " order by time asc limit 0, 7";
- var command = new MySqlCommand(query, conn);
- var reader = command.ExecuteReader();
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- var timestamp = (int)reader[0];
- var report = (string)reader[1];
- if (timestamp > lastKnownTimestamp)
- lastKnownTimestamp = timestamp;
- var seconds = now - timestamp;
- var units = TimeUnits(seconds);
- if (seconds < 10)
- units = "Just now";
- report = "[b][clr]14[b]" + units + ": [clr]12" + report.Replace("[b]", "[clr]11").Replace("[g]", "[clr]14").Replace("[/]", "[clr]12");
- Bot.Say(report, Channel);
- }
- }
- reader.Close();
- conn.Close();
- timer.Interval = 1000 * 60; //Two minutes.
- File.WriteAllText(memoryFile, lastKnownTimestamp.ToString());
- }
- private static string TimeUnits(int seconds)
- {
- if (seconds < 60) return seconds + " sec.";
- if (seconds < 3600) return Math.Floor((double)seconds / 60) + " min.";
- if (seconds < 86400) return Math.Floor((double)seconds / 3600) + " h.";
- return Math.Floor((double)seconds / 86400) + " d.";
- }
- /*
- function TimeUnits($sec)
- {
- if($sec < 60) return "$sec sec.";
- if($sec < 3600) return floor($sec/60)." min.";
- if($sec < 86400) return floor($sec/3600)." hour".($sec >= 7200 ? "s" : "");
- return floor($sec/86400)." day".($sec >= 172800 ? "s" : "");
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement