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.

183 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. }
  44. //
  45. // cmdIdgames
  46. //
  47. public void cmdIdgames(User usr, Channel channel, String msg)
  48. {
  49. var args = Utils.GetArguments(msg, commands["idgames"].help, 0, 3);
  50. switch(args.Length)
  51. {
  52. default:
  53. case 1:
  54. int id;
  55. if(args[0].Trim().Length == 0)
  56. idgamesRandom(usr, channel);
  57. else if(Int32.TryParse(args[0], out id))
  58. idgamesID(usr, channel, id);
  59. else
  60. idgames(usr, channel, args[0]);
  61. break;
  62. case 2: idgames(usr, channel, args[0], args[1]); break;
  63. case 3:
  64. idgames(usr, channel, args[0], args[1], args[2].Trim());
  65. break;
  66. }
  67. }
  68. //
  69. // idgamesRandom
  70. //
  71. private void idgamesRandom(User usr, Channel channel)
  72. {
  73. var req = WebRequest.Create("http://doomworld.com/idgames/?random")
  74. as HttpWebRequest;
  75. if(req == null) throw new CommandArgumentException("fug it borked");
  76. req.Referer = "http://doomworld.com/idgames/";
  77. bot.message(channel,
  78. Discord.Format.Escape(req.GetResponse().ResponseUri.ToString()));
  79. }
  80. //
  81. // idgamesID
  82. //
  83. private void idgamesID(User usr, Channel channel, int id)
  84. {
  85. var req = WebRequest.Create(APIURI + "?action=get&id=" + id)
  86. as HttpWebRequest;
  87. if(req == null) throw new CommandArgumentException("fug it borked");
  88. using(var response = req.GetResponse())
  89. {
  90. var xml = XDocument.Load(response.GetResponseStream());
  91. var x_title =
  92. from item in xml.Descendants("title") select item.Value;
  93. var x_uri = from item in xml.Descendants("url") select item.Value;
  94. if(!x_title.Any())
  95. {
  96. bot.message(channel, "Nothing found.");
  97. return;
  98. }
  99. bot.message(channel,
  100. Discord.Format.Escape(x_title.First() + ": " + x_uri.First()));
  101. }
  102. }
  103. //
  104. // idgames
  105. //
  106. private void idgames(User usr, Channel channel, String inquiry,
  107. String type = "title", String pos = "1")
  108. {
  109. var ipos = 0;
  110. if(pos.ToLower() != "random")
  111. {
  112. Utils.TryParse(pos, "Invalid position.", out ipos);
  113. if(ipos < 1)
  114. throw new CommandArgumentException("Invalid position.");
  115. ipos = ipos - 1;
  116. }
  117. inquiry = HttpUtility.UrlEncode(inquiry.Trim());
  118. type = HttpUtility.UrlEncode(type.Trim().ToLower());
  119. if(type == "name") type = "title"; // >_>'
  120. String[] validtypes = {
  121. "filename", "title", "author", "email",
  122. "description", "credits", "editors", "textfile"
  123. };
  124. if(!validtypes.Contains(type))
  125. throw new CommandArgumentException("Invalid inquiry type.");
  126. var uri = APIURI + "?action=search&sort=rating&query=" + inquiry +
  127. "&type=" + type;
  128. var req = WebRequest.Create(uri);
  129. Console.WriteLine("idgames query: {0}", uri);
  130. using(var response = req.GetResponse())
  131. {
  132. var xml = XDocument.Load(response.GetResponseStream());
  133. var x_titles =
  134. from item in xml.Descendants("title") select item.Value;
  135. var x_uris = from item in xml.Descendants("url") select item.Value;
  136. if(!x_titles.Any())
  137. {
  138. bot.message(channel, "Nothing found.");
  139. return;
  140. }
  141. if(pos == "random") ipos = rnd.Next(0, x_titles.Count());
  142. if(ipos >= x_titles.Count()) ipos = x_titles.Count() - 1;
  143. var title = x_titles.ElementAtOrDefault(ipos) ?? "invalid title";
  144. if(title.Trim().Length > 0) title = "[ " + title + " ] ";
  145. bot.message(channel,
  146. Discord.Format.Escape(String.Format("({0} of {1}{4} {2}{3}",
  147. ipos + 1, x_titles.Count(), title,
  148. x_uris.ElementAtOrDefault(ipos),
  149. x_titles.Count() >= 100 ? "+)" : ")")));
  150. }
  151. }
  152. }
  153. }