Mercurial Hosting > disearch
changeset 14:80105b716a62
start bot
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 31 Oct 2023 23:56:48 -0600 |
parents | af2b95854072 |
children | d0b8e1a8fc69 |
files | src/add_server.html.luan src/delete_server.js.luan src/lib/Config.luan src/lib/Db.luan src/lib/Server.luan src/lib/Shared.luan src/servers.html.luan src/tools/params.txt.luan |
diffstat | 8 files changed, 113 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/add_server.html.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/add_server.html.luan Tue Oct 31 23:56:48 2023 -0600 @@ -2,20 +2,68 @@ local error = Luan.error local Html = require "luan:Html.luan" local html_encode = Html.encode or error() +local Parsers = require "luan:Parsers.luan" +local json_parse = Parsers.json_parse or error() local Io = require "luan:Io.luan" +local uri = Io.uri or error() local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" local head = Shared.head or error() local header = Shared.header or error() local footer = Shared.footer or error() +local Server = require "site:/lib/Server.luan" +local Config = require "site:/lib/Config.luan" +local function error_page(msg) + Io.stdout = Http.response.text_writer() +%> +<!doctype html> +<html> + <head> +<% head() %> + <title>Disearch</title> + </head> + <body> +<% header() %> + <p><%=msg%></p> +<% footer() %> + </body> +</html> +<% +end + return function() local parameters = Http.request.parameters - local id = parameters.id or error() - local name = parameters.name or error() + if parameters.error == "access_denied" then + Http.response.send_redirect("/servers.html") + return + end + local guild_id = parameters.guild_id or error() + if Server.get_by_discord_id(guild_id) ~= nil then + error_page("already added") + return + end + + local url = "https://discord.com/api/guilds/"..guild_id + local config = Config.get() + local options = { + headers = { + ["User-Agent"] = "fuck you" -- for retarded Cloudflare + Authorization = "Bot "..config.discord.bot_token + } + } + local result = uri(url,options).read_text() + --logger.info(result) + result = json_parse(result) + + local name = result.name or error() name = html_encode(name) - local icon = parameters.icon or error() + local icon = result.icon + if icon == nil then + error_page("server must have icon") + return + end Io.stdout = Http.response.text_writer() %> <!doctype html> @@ -35,11 +83,11 @@ <h1>Add server</h1> <form page onsubmit="ajaxForm('/add_server.js',this); return false"> - <input type=hidden name=id value="<%=id%>"> + <input type=hidden name=id value="<%=guild_id%>"> <input type=hidden name=name value="<%=name%>"> <input type=hidden name=icon value="<%=icon%>"> <p><%=name%></p> - <p><img icon src="https://cdn.discordapp.com/icons/<%=id%>/<%=icon%>.png"></p> + <p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p> <p> Invite URL:<br> <input type=url required name=invite>
--- a/src/delete_server.js.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/delete_server.js.luan Tue Oct 31 23:56:48 2023 -0600 @@ -1,16 +1,38 @@ local Luan = require "luan:Luan.luan" local error = Luan.error +local stringify = Luan.stringify or error() local Io = require "luan:Io.luan" +local uri = Io.uri or error() local Http = require "luan:http/Http.luan" local Server = require "site:/lib/Server.luan" local Utils = require "site:/lib/Utils.luan" local get_user = Utils.get_user or error() +local Config = require "site:/lib/Config.luan" +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "delete_server.js" return function() local server_id = Http.request.parameters.server or error() local server = Server.get_by_id(server_id) server.owner_discord_id == get_user() or error() + + local url = "https://discord.com/api/users/@me/guilds/"..server.discord_id + local config = Config.get() + local options = { + method = "DELETE" + headers = { + ["User-Agent"] = "fuck you" -- for retarded Cloudflare + Authorization = "Bot "..config.discord.bot_token + } + } + try + local result = uri(url,options).read_text() + catch e + -- logger.info(stringify(e)) + e.response_code == 404 or e.throw() + end + server.delete() Io.stdout = Http.response.text_writer() %>
--- a/src/lib/Config.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/lib/Config.luan Tue Oct 31 23:56:48 2023 -0600 @@ -20,6 +20,7 @@ config.discord = config.discord or {} config.discord.client_id = config.discord.client_id or "replace" config.discord.client_secret = config.discord.client_secret or "replace" + config.discord.bot_token = config.discord.bot_token or "replace" return config end
--- a/src/lib/Db.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/lib/Db.luan Tue Oct 31 23:56:48 2023 -0600 @@ -18,6 +18,7 @@ name = "lucene" } ) +Db.indexed_fields.server_discord_id = Lucene.type.string Db.indexed_fields.server_owner_discord_id = Lucene.type.string function Db.not_in_transaction()
--- a/src/lib/Server.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/lib/Server.luan Tue Oct 31 23:56:48 2023 -0600 @@ -12,8 +12,8 @@ doc.type == "server" or error "wrong type" return Server.new { id = doc.id + discord_id = doc.server_discord_id owner_discord_id = doc.server_owner_discord_id - discord_id = doc.discord_id name = doc.name icon = doc.icon invite = doc.invite @@ -24,8 +24,8 @@ return { type = "server" id = server.id + server_discord_id = server.discord_id or error() server_owner_discord_id = server.owner_discord_id or error() - discord_id = server.discord_id or error() name = server.name or error() icon = server.icon or error() invite = server.invite or error() @@ -66,6 +66,11 @@ return doc and from_doc(doc) end +function Server.get_by_discord_id(discord_id) + local doc = Db.get_document("server_discord_id:"..discord_id) + return doc and from_doc(doc) +end + local function search(query,sort,rows) rows = rows or 1000000 local docs = Db.search(query,1,rows,{sort=sort})
--- a/src/lib/Shared.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/lib/Shared.luan Tue Oct 31 23:56:48 2023 -0600 @@ -52,9 +52,14 @@ <% end -function Shared.discord_redirect_uri() +local function base_url() local request = Http.request - return request.scheme.."://"..request.headers["Host"].."/login2.html" + return request.scheme.."://"..request.headers["Host"] +end +Shared.base_url = base_url + +function Shared.discord_redirect_uri() + return base_url().."/login2.html" end function Shared.js_error(field,message)
--- a/src/servers.html.luan Mon Oct 30 21:32:42 2023 -0600 +++ b/src/servers.html.luan Tue Oct 31 23:56:48 2023 -0600 @@ -3,13 +3,18 @@ local ipairs = Luan.ipairs or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() +local url_encode = Html.url_encode or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" local head = Shared.head or error() local header = Shared.header or error() local footer = Shared.footer or error() +local base_url = Shared.base_url or error() local Server = require "site:/lib/Server.luan" +local Config = require "site:/lib/Config.luan" +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "servers.html" return function() @@ -36,9 +41,13 @@ <a href="javascript:ajax('/delete_server.js?server=<%=server.id%>')">delete</a> </p> <% - end + end + local config = Config.get() + local redirect_uri = base_url().."/add_server.html" + local url = "https://discord.com/oauth2/authorize?client_id="..config.discord.client_id.."&scope=bot&response_type=code&redirect_uri="..url_encode(redirect_uri) + %> - <p><a href="choose_server.html">add server</a></p> + <p><a href="<%=url%>">add server</a></p> <% footer() %> </body>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/params.txt.luan Tue Oct 31 23:56:48 2023 -0600 @@ -0,0 +1,11 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local stringify = Luan.stringify or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" + + +return function() + Io.stdout = Http.response.text_writer() + %><%= stringify(Http.request.parameters) %><% +end