vrobot3 is a chat bot for IRC and Discord.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.7KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // .seen
  10. //
  11. //-----------------------------------------------------------------------------
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.IO;
  16. using Sharkbite.Irc;
  17. using Newtonsoft.Json;
  18. using Tarczynski.NtpDateTime;
  19. namespace ProjectGolan.Vrobot3
  20. {
  21. //
  22. // Mod_Seen
  23. //
  24. public class Mod_Seen : IBotModule
  25. {
  26. //
  27. // SeenName
  28. //
  29. private class SeenName
  30. {
  31. public String real, check;
  32. public DateTime time;
  33. }
  34. // SeenDates
  35. private class SeenDates : List<SeenName> {}
  36. private SeenDates seendates = new SeenDates();
  37. private TimeZoneInfo burb;
  38. private DateTime lastwrite = DateTime.Now;
  39. //
  40. // Mod_Seen constructor
  41. //
  42. public Mod_Seen(Bot bot_) :
  43. base(bot_)
  44. {
  45. if(File.Exists("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json"))
  46. seendates = JsonConvert.DeserializeObject<SeenDates>(File.ReadAllText("/srv/irc/vrobot3/data/seendates." +
  47. bot.n_groupname + ".json"));
  48. commands["seen"] = new BotCommandStructure { cmd = Cmd_Seen,
  49. help = "Responds with the last time I saw someone.\n" +
  50. "Syntax: .seen person\n" +
  51. "Example: .seen vrobot3"
  52. };
  53. events.OnSeen += Evt_OnSeen;
  54. events.OnDisconnected += Evt_OnDisconnected;
  55. burb = TimeZoneInfo.CreateCustomTimeZone("burb", new TimeSpan(10, -30, 0), "burb", "burb");
  56. postSetup();
  57. }
  58. //
  59. // Cmd_Seen
  60. //
  61. public void Cmd_Seen(UserInfo usr, String channel, String msg)
  62. {
  63. if(msg.Length == 0 || msg.Contains(" "))
  64. throw new CommandArgumentException("Invalid name.");
  65. String name = msg.ToLower();
  66. var seen = from sdata in seendates where sdata.check == name select sdata;
  67. if(seen.Any())
  68. {
  69. var other = seen.First();
  70. var fuzzy = Utils.FuzzyRelativeDate(other.time, DateTime.Now.FromNtp());
  71. var time = other.time.ToShortTimeString();
  72. var pidgeon = TimeZoneInfo.ConvertTime(other.time, TimeZoneInfo.Local, burb).ToShortTimeString();
  73. bot.Reply(usr, channel, $"I last saw {other.real} active {fuzzy}, at {time} CST ({pidgeon} AEST.)");
  74. }
  75. else
  76. bot.Reply(usr, channel, "I haven't seen " + msg + " before, sorry.");
  77. WriteSeenDates();
  78. }
  79. //
  80. // Evt_OnScreen
  81. //
  82. public void Evt_OnSeen(UserInfo usr, String channel)
  83. {
  84. String name = usr.Nick.ToLower();
  85. var seen = from sdata in seendates where sdata.check == name select sdata;
  86. if(seen.Any())
  87. {
  88. seen.First().time = DateTime.Now.FromNtp();
  89. seen.First().real = usr.Nick;
  90. }
  91. else
  92. seendates.Add(new SeenName{ real = usr.Nick, check = usr.Nick.ToLower(), time = DateTime.Now.FromNtp() });
  93. if(DateTime.Now.Subtract(lastwrite).Minutes >= 30)
  94. WriteSeenDates();
  95. }
  96. //
  97. // Evt_OnDisconnected
  98. //
  99. public void Evt_OnDisconnected()
  100. {
  101. WriteSeenDates();
  102. }
  103. //
  104. // WriteSeenDates
  105. //
  106. private void WriteSeenDates()
  107. {
  108. File.WriteAllText("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json",
  109. JsonConvert.SerializeObject(seendates));
  110. }
  111. }
  112. }