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.

223 lines
5.7KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Memoing capabilities.
  10. // .memo
  11. //
  12. //-----------------------------------------------------------------------------
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using Sharkbite.Irc;
  17. using Newtonsoft.Json;
  18. namespace ProjectGolan.Vrobot3.Modules
  19. {
  20. //
  21. // Mod_Memo
  22. //
  23. public class Mod_Memo : IBotModule
  24. {
  25. //
  26. // MemoFlags
  27. //
  28. [Flags]
  29. enum MemoFlags
  30. {
  31. OnSeen = 1 << 0
  32. }
  33. //
  34. // MemoInfo
  35. //
  36. private struct MemoInfo
  37. {
  38. public String content;
  39. public String sender;
  40. public DateTime time;
  41. public MemoFlags flags;
  42. };
  43. // MemoDict
  44. private class MemoDict : Dictionary<String, List<MemoInfo>> {}
  45. MemoDict memos = new MemoDict();
  46. //
  47. // Mod_Memo constructor
  48. //
  49. public Mod_Memo(Bot bot_) :
  50. base(bot_)
  51. {
  52. if(File.Exists("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json"))
  53. memos = JsonConvert.DeserializeObject<MemoDict>(File.ReadAllText("/srv/irc/vrobot3/data/memos." +
  54. bot.n_groupname + ".json"));
  55. commands["memo"] = new BotCommandStructure{ cmd = Cmd_Memo,
  56. help = "Sends a message to someone later. Activates when they say something. || " +
  57. "Syntax: .memo person message || " +
  58. "Example: .memo SomeDude wow u suck at videogames"
  59. };
  60. commands["memoseen"] = new BotCommandStructure{ cmd = Cmd_MemoSeen,
  61. help = "Sends a message to someone later. Activates when they do anything. || " +
  62. "Syntax: .memoseen person message || " +
  63. "Example: .memoseen SomeDude wow u suck at videogames"
  64. };
  65. commands["memocount"] = new BotCommandStructure{ cmd = Cmd_MemoCount, flags = BotCommandFlags.AdminOnly,
  66. help = "Gets the amount of memos for this session. || " +
  67. "Syntax: @memocount"
  68. };
  69. events.OnMessage += Evt_OnMessage;
  70. events.OnDisconnected += Evt_OnDisconnected;
  71. events.OnSeen += Evt_OnSeen;
  72. postSetup();
  73. }
  74. //
  75. // Cmd_MemoCount
  76. //
  77. public void Cmd_MemoCount(UserInfo usr, String channel, String msg)
  78. {
  79. bot.Reply(usr, channel, memos.Count.ToString());
  80. }
  81. //
  82. // Cmd_Memo
  83. //
  84. public void Cmd_Memo(UserInfo usr, String channel, String msg)
  85. {
  86. String[] args = Utils.GetArguments(msg, commands["memo"].help, 2, 2, ' ');
  87. args[0] = args[0].Replace(",", "");
  88. AddMemo(args[0], new MemoInfo {
  89. content = args[1],
  90. sender = usr.Nick,
  91. time = DateTime.Now
  92. });
  93. bot.Reply(usr, channel, String.Format("Message for {0} will be sent next time they say something.", args[0]));
  94. }
  95. //
  96. // Cmd_MemoSeen
  97. //
  98. public void Cmd_MemoSeen(UserInfo usr, String channel, String msg)
  99. {
  100. String[] args = Utils.GetArguments(msg, commands["memoseen"].help, 2, 2, ' ');
  101. args[0] = args[0].Replace(",", "");
  102. AddMemo(args[0], new MemoInfo {
  103. content = args[1],
  104. sender = usr.Nick,
  105. time = DateTime.Now,
  106. flags = MemoFlags.OnSeen
  107. });
  108. bot.Reply(usr, channel, String.Format("Message for {0} will be sent next time I see them.", args[0]));
  109. }
  110. //
  111. // AddMemo
  112. //
  113. private void AddMemo(String name, MemoInfo memo)
  114. {
  115. name = name.ToLower();
  116. if(!memos.ContainsKey(name))
  117. memos[name] = new List<MemoInfo>();
  118. memos[name].Add(memo);
  119. WriteMemos();
  120. }
  121. //
  122. // OutputMemos
  123. //
  124. private void OutputMemos(String channel, String realnick, bool onseen)
  125. {
  126. String nick = realnick.ToLower();
  127. if(!memos.ContainsKey(nick))
  128. return;
  129. var arr = memos[nick];
  130. for(int i = arr.Count - 1; i >= 0; i--)
  131. {
  132. MemoInfo memo = arr[i];
  133. if(!memo.flags.HasFlag(MemoFlags.OnSeen) && onseen)
  134. continue;
  135. String outp = String.Empty;
  136. outp += String.Format("[Memo from {0}, {1}]", memo.sender, Utils.FuzzyRelativeDate(memo.time));
  137. // Wrap if it's probably going to be too long.
  138. if(memo.content.Length > 350)
  139. {
  140. bot.Message(channel, outp + ":");
  141. outp = String.Empty;
  142. }
  143. outp += String.Format(" {0}: {1}", realnick, memo.content);
  144. bot.Message(channel, outp);
  145. arr.RemoveAt(i);
  146. }
  147. if(arr.Count == 0)
  148. memos.Remove(nick);
  149. WriteMemos();
  150. }
  151. //
  152. // Evt_OnMessage
  153. //
  154. public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd)
  155. {
  156. OutputMemos(channel, usr.Nick, false);
  157. }
  158. //
  159. // Evt_OnSeen
  160. //
  161. public void Evt_OnSeen(UserInfo usr, String channel)
  162. {
  163. OutputMemos(channel, usr.Nick, true);
  164. }
  165. //
  166. // Evt_OnDisconnected
  167. //
  168. public void Evt_OnDisconnected()
  169. {
  170. WriteMemos();
  171. }
  172. //
  173. // WriteMemos
  174. //
  175. private void WriteMemos()
  176. {
  177. File.WriteAllText("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json",
  178. JsonConvert.SerializeObject(memos));
  179. }
  180. }
  181. }