Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _exit=Exit;Exit=function(ret) GetKey(); return ret+1; end
- local intervalseconds = 3600;
- --412400345206226946
- local channelid = "412400345206226946";--"463010367853821953";
- local dumbxml = require('dumbxml')
- local kitsunebot = dofile("KitsunebotLib.lua");
- local urlencoder = dofile("urlencode.lua");
- math.randomseed(os.time())
- math.random(); math.random(); math.random();
- local connectionstring = nil;
- local function GetConnectionString()
- if connectionstring then
- return connectionstring;
- end
- local f = io.open("connectionstring.txt", "r");
- if not f then
- error("Unable to open connectionstring.txt");
- end
- connectionstring = f:read("*all");
- f:close();
- return connectionstring;
- end
- local function Rand(max)
- local str = tostring(math.random());
- return tonumber(str:match("%.(.+)")) % max;
- end
- local function padding(cnt)
- local result = "";
- for n=1,cnt do
- result = result .. " ";
- end
- return result;
- end
- local function PrintTable(tbl, n)
- n = n or 0;
- for k,v in pairs(tbl) do
- print(padding(n)..tostring(k),v);
- if type(v) == "table" then
- PrintTable(v,n+1);
- end
- end
- end
- local function GetCount(tag)
- local request = Http.Start("GET", "https://safebooru.org/index.php?page=dapi&s=post&q=index&tags="..urlencoder.encode(tag).."&limit=1", nil, nil);
- local code, ok, contents, header = request:GetResult()
- if code ~= 200 then
- return 0;
- else
- local result = dumbxml.fromString(contents);
- for v in result:iter() do
- if v.name == "posts" then
- return tonumber(v.attrs.count);
- end
- end
- end
- return 0;
- end
- local function GetPost(tag, pid)
- 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);
- local code, ok, contents, header = request:GetResult()
- if code ~= 200 then
- return nil;
- else
- local result = dumbxml.fromString(contents);
- for v in result:iter() do
- if v.name == "post" then
- return v.attrs.file_url, v.attrs.tags, v.attrs.id;
- end
- end
- end
- return nil;
- end
- local function GetRandom(tag)
- tag = tag or "*";
- local cnt = GetCount(tag);
- if cnt <= 0 then
- return nil;
- end
- local pid = Rand(cnt);
- local img, rawtags, id = GetPost(tag , pid);
- if not img then
- return nil;
- end
- local tags = {};
- for tag in rawtags:gmatch("(.-)%s") do
- tag = tag:gsub("_"," ");
- table.insert(tags, tag);
- end
- return "http:"..img, tags, id;
- end
- kitsunebot:Connect(GetConnectionString());
- local channel = assert(kitsunebot:GetChannelIdById(channelid),"Channel with ID "..tostring(channelid).." not found");
- local selfid = kitsunebot:SelfUserId();
- local selfinfo = kitsunebot:GetUser(selfid,channel.GuildId);
- local pattern = "^<@"..selfid..">";
- PrintTable(channel);
- PrintTable(selfinfo);
- local function GetRandomByTag(tag, authorid)
- kitsunebot:Typing(channel.Id);
- local usertag = "";
- if authorid then
- local auth = kitsunebot:GetUser(authorid, channel.GuildId);
- if auth then
- usertag = auth.Tag;
- end
- end
- io.write("Retriving... ");
- local url, tags, pid = GetRandom(tag);
- if url then
- print(url);
- local footer = "";
- for n=1,#tags do
- footer = footer .. tags[n].." ";
- end
- local embed = kitsunebot:BuildEmbed("#FF00FF", nil, pid, authorid, channel.GuildId, tag);
- kitsunebot:SetImgUrl(embed,url);
- kitsunebot:SendMessageToChannel(channel.Id, "", embed);
- else
- kitsunebot:SendMessageToChannel(channel.Id, usertag.." found nothing with tags: "..tag, nil);
- print("Failed!");
- end
- end
- local function Msg(msg)
- print(msg.Parameter3,msg.Parameter5);
- if msg.Parameter5:match(pattern) then
- local tags = msg.Parameter5:match(pattern.."%s(.-)$");
- tags = tags or "*";
- print("["..tags.."]");
- GetRandomByTag(tags, msg.UserId);
- return true;
- else
- return false;
- end
- end
- while true do
- reset = true;
- while reset do
- reset = false;
- for n=1,intervalseconds do
- local poll = kitsunebot:PollChannel(channelid);
- for n=1,#poll do
- if Msg(poll[n]) then
- reset = true;
- end
- end
- if reset then
- break;
- end
- Sleep(1000);
- SetTitle(tostring(intervalseconds-n).."...");
- end
- end
- GetRandomByTag("*");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement