Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Timers;
  8. using MySql.Data;
  9. using MySql.Data.MySqlClient;
  10.  
  11. namespace Kawa.ChatBot
  12. {
  13. public static class Reporter
  14. {
  15. private static string memoryFile = "furrygoat.txt";
  16. public static string ReporterURL = "localhost";
  17. public static string Channel = "#kawa";
  18. public static Timer timer;
  19.  
  20. public static void StartReporting(string origin, string channel, params string[] stuff)
  21. {
  22. if (!Bot.CheckOps(origin))
  23. {
  24. Bot.Say("[clr]4[u]Access denied.", channel);
  25. return;
  26. }
  27.  
  28. Bot.Say("[clr]02Furry goats activated, reporting to {0}...", channel, Channel);
  29. timer = new Timer();
  30. timer.Interval = 100 * 60; //One minute.
  31. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  32. timer.Start();
  33. }
  34.  
  35. private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  36. {
  37. var lastKnownTimestamp = 0;
  38. if (File.Exists(memoryFile))
  39. lastKnownTimestamp = int.Parse(File.ReadAllText(memoryFile));
  40.  
  41. var creds = "server=" + ReporterURL + ";user=helmeted_kawa;password=marshmallows;database=helmeted_nikoboard;";
  42. var conn = new MySqlConnection(creds);
  43.  
  44. DateTime d = new DateTime(1970, 1, 1);
  45. int now = (int)DateTime.Now.ToUniversalTime().Subtract(d).TotalSeconds;
  46.  
  47. if (conn.State == System.Data.ConnectionState.Closed)
  48. conn.Open();
  49.  
  50. var query = "select * from reports where time > " + (now - (2000 * 60)) + " and time > " + lastKnownTimestamp + " order by time asc limit 0, 7";
  51. var command = new MySqlCommand(query, conn);
  52. var reader = command.ExecuteReader();
  53. if (reader.HasRows)
  54. {
  55. while (reader.Read())
  56. {
  57. var timestamp = (int)reader[0];
  58. var report = (string)reader[1];
  59.  
  60. if (timestamp > lastKnownTimestamp)
  61. lastKnownTimestamp = timestamp;
  62.  
  63. var seconds = now - timestamp;
  64. var units = TimeUnits(seconds);
  65. if (seconds < 10)
  66. units = "Just now";
  67.  
  68. report = "[b][clr]14[b]" + units + ": [clr]12" + report.Replace("[b]", "[clr]11").Replace("[g]", "[clr]14").Replace("[/]", "[clr]12");
  69. Bot.Say(report, Channel);
  70. }
  71. }
  72. reader.Close();
  73. conn.Close();
  74.  
  75. timer.Interval = 1000 * 60; //Two minutes.
  76. File.WriteAllText(memoryFile, lastKnownTimestamp.ToString());
  77. }
  78.  
  79. private static string TimeUnits(int seconds)
  80. {
  81. if (seconds < 60) return seconds + " sec.";
  82. if (seconds < 3600) return Math.Floor((double)seconds / 60) + " min.";
  83. if (seconds < 86400) return Math.Floor((double)seconds / 3600) + " h.";
  84. return Math.Floor((double)seconds / 86400) + " d.";
  85. }
  86. /*
  87. function TimeUnits($sec)
  88. {
  89. if($sec < 60) return "$sec sec.";
  90. if($sec < 3600) return floor($sec/60)." min.";
  91. if($sec < 86400) return floor($sec/3600)." hour".($sec >= 7200 ? "s" : "");
  92. return floor($sec/86400)." day".($sec >= 172800 ? "s" : "");
  93. }
  94. */
  95.  
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement