Advertisement
Terrah

Untitled

Aug 10th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. local _exit=Exit;Exit=function(ret) GetKey(); return ret+1; end
  2.  
  3. local intervalseconds = 3600;
  4. --412400345206226946
  5. local channelid = "412400345206226946";--"463010367853821953";
  6. local dumbxml = require('dumbxml')
  7. local kitsunebot = dofile("KitsunebotLib.lua");
  8. local urlencoder = dofile("urlencode.lua");
  9.  
  10. math.randomseed(os.time())
  11. math.random(); math.random(); math.random();
  12.  
  13. local connectionstring = nil;
  14. local function GetConnectionString()
  15.  
  16. if connectionstring then
  17. return connectionstring;
  18. end
  19.  
  20. local f = io.open("connectionstring.txt", "r");
  21. if not f then
  22. error("Unable to open connectionstring.txt");
  23. end
  24.  
  25. connectionstring = f:read("*all");
  26. f:close();
  27. return connectionstring;
  28. end
  29.  
  30. local function Rand(max)
  31.  
  32. local str = tostring(math.random());
  33.  
  34. return tonumber(str:match("%.(.+)")) % max;
  35. end
  36.  
  37. local function padding(cnt)
  38.  
  39. local result = "";
  40.  
  41. for n=1,cnt do
  42. result = result .. " ";
  43. end
  44.  
  45. return result;
  46. end
  47.  
  48. local function PrintTable(tbl, n)
  49.  
  50. n = n or 0;
  51.  
  52. for k,v in pairs(tbl) do
  53.  
  54. print(padding(n)..tostring(k),v);
  55.  
  56. if type(v) == "table" then
  57. PrintTable(v,n+1);
  58. end
  59.  
  60. end
  61. end
  62.  
  63. local function GetCount(tag)
  64.  
  65. local request = Http.Start("GET", "https://safebooru.org/index.php?page=dapi&s=post&q=index&tags="..urlencoder.encode(tag).."&limit=1", nil, nil);
  66. local code, ok, contents, header = request:GetResult()
  67.  
  68. if code ~= 200 then
  69. return 0;
  70. else
  71.  
  72. local result = dumbxml.fromString(contents);
  73. for v in result:iter() do
  74. if v.name == "posts" then
  75. return tonumber(v.attrs.count);
  76. end
  77. end
  78. end
  79.  
  80. return 0;
  81. end
  82.  
  83. local function GetPost(tag, pid)
  84. local request = Http.Start("GET", "https://safebooru.org/index.php?page=dapi&s=post&q=index&tags="..urlencoder.encode(tag).."&limit=1&pid="..tostring(pid), nil, nil);
  85. local code, ok, contents, header = request:GetResult()
  86.  
  87. if code ~= 200 then
  88. return nil;
  89. else
  90.  
  91. local result = dumbxml.fromString(contents);
  92. for v in result:iter() do
  93. if v.name == "post" then
  94. return v.attrs.file_url, v.attrs.tags, v.attrs.id;
  95. end
  96. end
  97. end
  98.  
  99. return nil;
  100. end
  101.  
  102. local function GetRandom(tag)
  103.  
  104. tag = tag or "*";
  105.  
  106. local cnt = GetCount(tag);
  107. if cnt <= 0 then
  108. return nil;
  109. end
  110.  
  111. local pid = Rand(cnt);
  112. local img, rawtags, id = GetPost(tag , pid);
  113.  
  114. if not img then
  115. return nil;
  116. end
  117.  
  118. local tags = {};
  119. for tag in rawtags:gmatch("(.-)%s") do
  120. tag = tag:gsub("_"," ");
  121. table.insert(tags, tag);
  122. end
  123.  
  124. return "http:"..img, tags, id;
  125. end
  126.  
  127. kitsunebot:Connect(GetConnectionString());
  128. local channel = assert(kitsunebot:GetChannelIdById(channelid),"Channel with ID "..tostring(channelid).." not found");
  129. local selfid = kitsunebot:SelfUserId();
  130. local selfinfo = kitsunebot:GetUser(selfid,channel.GuildId);
  131. local pattern = "^<@"..selfid..">";
  132.  
  133. PrintTable(channel);
  134. PrintTable(selfinfo);
  135.  
  136. local function GetRandomByTag(tag, authorid)
  137.  
  138. kitsunebot:Typing(channel.Id);
  139.  
  140. local usertag = "";
  141.  
  142. if authorid then
  143.  
  144. local auth = kitsunebot:GetUser(authorid, channel.GuildId);
  145.  
  146. if auth then
  147. usertag = auth.Tag;
  148. end
  149. end
  150.  
  151. io.write("Retriving... ");
  152. local url, tags, pid = GetRandom(tag);
  153.  
  154. if url then
  155. print(url);
  156. local footer = "";
  157.  
  158. for n=1,#tags do
  159. footer = footer .. tags[n].." ";
  160. end
  161.  
  162. local embed = kitsunebot:BuildEmbed("#FF00FF", nil, pid, authorid, channel.GuildId, tag);
  163.  
  164. kitsunebot:SetImgUrl(embed,url);
  165.  
  166. kitsunebot:SendMessageToChannel(channel.Id, "", embed);
  167. else
  168. kitsunebot:SendMessageToChannel(channel.Id, usertag.." found nothing with tags: "..tag, nil);
  169. print("Failed!");
  170. end
  171. end
  172.  
  173. local function Msg(msg)
  174.  
  175. print(msg.Parameter3,msg.Parameter5);
  176.  
  177. if msg.Parameter5:match(pattern) then
  178.  
  179. local tags = msg.Parameter5:match(pattern.."%s(.-)$");
  180. tags = tags or "*";
  181. print("["..tags.."]");
  182. GetRandomByTag(tags, msg.UserId);
  183. return true;
  184. else
  185. return false;
  186. end
  187. end
  188.  
  189. while true do
  190.  
  191. reset = true;
  192.  
  193. while reset do
  194.  
  195. reset = false;
  196.  
  197. for n=1,intervalseconds do
  198.  
  199. local poll = kitsunebot:PollChannel(channelid);
  200.  
  201. for n=1,#poll do
  202. if Msg(poll[n]) then
  203. reset = true;
  204. end
  205. end
  206.  
  207. if reset then
  208. break;
  209. end
  210.  
  211. Sleep(1000);
  212. SetTitle(tostring(intervalseconds-n).."...");
  213. end
  214. end
  215.  
  216. GetRandomByTag("*");
  217. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement