view src/delete_server.js.luan @ 15:d0b8e1a8fc69

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 00:12:08 -0600
parents 80105b716a62
children
line wrap: on
line source

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"] = "Disearch"  -- 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()
%>
	location.reload();
<%
end