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.

185 lines
5.3KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Idgames search module.
  10. // .idgames
  11. //
  12. //-----------------------------------------------------------------------------
  13. using System;
  14. using System.Net;
  15. using System.Web;
  16. using System.Text;
  17. using System.Xml;
  18. using System.Xml.Linq;
  19. using System.Linq;
  20. namespace ProjectGolan.Vrobot3.Modules
  21. {
  22. //
  23. // Mod_Idgames
  24. //
  25. public class Mod_Idgames : IBotModule
  26. {
  27. private const String APIURI =
  28. "http://doomworld.com/idgames/api/api.php";
  29. private Random rnd = Utils.GetRND();
  30. //
  31. // Mod_Idgames constructor
  32. //
  33. public Mod_Idgames(Bot bot) :
  34. base(bot)
  35. {
  36. commands["idgames"] = new BotCommandStructure{
  37. cmd = cmdIdgames,
  38. help = "Gets an entry from the idgames archive.\n" +
  39. "Syntax: .idgames [name or ID[, type[, position]]]\n" +
  40. "Example: .idgames scythe, filename, 4\n" +
  41. "Example: .idgames"
  42. };
  43. postSetup();
  44. }
  45. //
  46. // cmdIdgames
  47. //
  48. public void cmdIdgames(User usr, Channel channel, String msg)
  49. {
  50. var args = Utils.GetArguments(msg, commands["idgames"].help, 0, 3);
  51. switch(args.Length)
  52. {
  53. default:
  54. case 1:
  55. int id;
  56. if(args[0].Trim().Length == 0)
  57. idgamesRandom(usr, channel);
  58. else if(Int32.TryParse(args[0], out id))
  59. idgamesID(usr, channel, id);
  60. else
  61. idgames(usr, channel, args[0]);
  62. break;
  63. case 2: idgames(usr, channel, args[0], args[1]); break;
  64. case 3:
  65. idgames(usr, channel, args[0], args[1], args[2].Trim());
  66. break;
  67. }
  68. }
  69. //
  70. // idgamesRandom
  71. //
  72. private void idgamesRandom(User usr, Channel channel)
  73. {
  74. var req = WebRequest.Create("http://doomworld.com/idgames/?random")
  75. as HttpWebRequest;
  76. if(req == null) throw new CommandArgumentException("fug it borked");
  77. req.Referer = "http://doomworld.com/idgames/";
  78. bot.message(channel,
  79. Discord.Format.Escape(req.GetResponse().ResponseUri.ToString()));
  80. }
  81. //
  82. // idgamesID
  83. //
  84. private void idgamesID(User usr, Channel channel, int id)
  85. {
  86. var req = WebRequest.Create(APIURI + "?action=get&id=" + id)
  87. as HttpWebRequest;
  88. if(req == null) throw new CommandArgumentException("fug it borked");
  89. using(var response = req.GetResponse())
  90. {
  91. var xml = XDocument.Load(response.GetResponseStream());
  92. var x_title =
  93. from item in xml.Descendants("title") select item.Value;
  94. var x_uri = from item in xml.Descendants("url") select item.Value;
  95. if(!x_title.Any())
  96. {
  97. bot.message(channel, "Nothing found.");
  98. return;
  99. }
  100. bot.message(channel,
  101. Discord.Format.Escape(x_title.First() + ": " + x_uri.First()));
  102. }
  103. }
  104. //
  105. // idgames
  106. //
  107. private void idgames(User usr, Channel channel, String inquiry,
  108. String type = "title", String pos = "1")
  109. {
  110. var ipos = 0;
  111. if(pos.ToLower() != "random")
  112. {
  113. Utils.TryParse(pos, "Invalid position.", out ipos);
  114. if(ipos < 1)
  115. throw new CommandArgumentException("Invalid position.");
  116. ipos = ipos - 1;
  117. }
  118. inquiry = HttpUtility.UrlEncode(inquiry.Trim());
  119. type = HttpUtility.UrlEncode(type.Trim().ToLower());
  120. if(type == "name") type = "title"; // >_>'
  121. String[] validtypes = {
  122. "filename", "title", "author", "email",
  123. "description", "credits", "editors", "textfile"
  124. };
  125. if(!validtypes.Contains(type))
  126. throw new CommandArgumentException("Invalid inquiry type.");
  127. var uri = APIURI + "?action=search&sort=rating&query=" + inquiry +
  128. "&type=" + type;
  129. var req = WebRequest.Create(uri);
  130. Console.WriteLine("idgames query: {0}", uri);
  131. using(var response = req.GetResponse())
  132. {
  133. var xml = XDocument.Load(response.GetResponseStream());
  134. var x_titles =
  135. from item in xml.Descendants("title") select item.Value;
  136. var x_uris = from item in xml.Descendants("url") select item.Value;
  137. if(!x_titles.Any())
  138. {
  139. bot.message(channel, "Nothing found.");
  140. return;
  141. }
  142. if(pos == "random") ipos = rnd.Next(0, x_titles.Count());
  143. if(ipos >= x_titles.Count()) ipos = x_titles.Count() - 1;
  144. var title = x_titles.ElementAtOrDefault(ipos) ?? "invalid title";
  145. if(title.Trim().Length > 0) title = "[ " + title + " ] ";
  146. bot.message(channel,
  147. Discord.Format.Escape(String.Format("({0} of {1}{4} {2}{3}",
  148. ipos + 1, x_titles.Count(), title,
  149. x_uris.ElementAtOrDefault(ipos),
  150. x_titles.Count() >= 100 ? "+)" : ")")));
  151. }
  152. }
  153. }
  154. }