Created
November 11, 2022 22:45
-
-
Save Frityet/8f4348957a6f3496af90f652c1061264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local https = require("ssl.https") | |
local ltn12 = require("ltn12") | |
local json = require("json") | |
local lgi = require("lgi") | |
local GObject = lgi.require("GObject") | |
local GTK = lgi.require("Gtk", "3.0") | |
---@param url string | |
---@return { [string] : string | number | table } | |
local function webhook_info(url) | |
local get_content, code = https.request(url) | |
if not get_content or code ~= 200 then | |
error("Could not get webhook info! Error: "..code) | |
end | |
return json.decode(get_content) | |
end | |
local embeds = { | |
{ | |
title = "Title here", | |
description = "Lots of text", | |
color = 0 | |
} | |
} | |
local EMBED_COLUMNS = { | |
TITLE = 1, | |
DESCRIPTION = 2, | |
COLOUR = 3, | |
} | |
local embed_model = GTK.ListStore.new { | |
[EMBED_COLUMNS.TITLE] = GObject.Type.STRING, | |
[EMBED_COLUMNS.DESCRIPTION] = GObject.Type.STRING, | |
[EMBED_COLUMNS.COLOUR] = GObject.Type.INT, | |
} | |
embed_model:append { | |
[EMBED_COLUMNS.TITLE] = embeds[1].title, | |
[EMBED_COLUMNS.DESCRIPTION] = embeds[1].description, | |
[EMBED_COLUMNS.COLOUR] = embeds[1].color | |
} | |
local window = GTK.Window { | |
title = "Troller", | |
default_width = 768, | |
default_height = 480, | |
GTK.VBox { | |
id = "virginitybox", | |
spacing = 8, | |
GTK.HBox { | |
spacing = 8, | |
GTK.Label { | |
label = "Webhook URL" | |
}, | |
GTK.Entry { | |
id = "webhook_url" | |
} | |
}, | |
GTK.Entry { | |
id = "mainmessage", | |
text = "Main message", | |
}, | |
GTK.VBox { | |
id = "embeds", | |
spacing = 8, | |
GTK.Label { | |
label = "Embeds" | |
}, | |
GTK.ScrolledWindow { | |
shadow_type = 'ETCHED_IN', | |
expand = true, | |
GTK.TreeView { | |
model = embed_model, | |
id = "embed_view", | |
GTK.TreeViewColumn { | |
title = "Title", | |
{ | |
GTK.CellRendererText { | |
id = "title_renderer", | |
editable = true, | |
}, | |
{ text = EMBED_COLUMNS.TITLE, } | |
}, | |
}, | |
GTK.TreeViewColumn { | |
title = "Description", | |
{ | |
GTK.CellRendererText { | |
id = "desc_renderer", | |
editable = true, | |
}, | |
{ text = EMBED_COLUMNS.DESCRIPTION, } | |
}, | |
}, | |
GTK.TreeViewColumn { | |
title = "Colour", | |
{ | |
GTK.CellRendererText { | |
id = "colour_renderer", | |
editable = true, | |
}, | |
{ text = EMBED_COLUMNS.COLOUR, } | |
}, | |
}, | |
}, | |
}, | |
GTK.HBox { | |
GTK.Button { | |
id = "add_embed", | |
label = "Add" | |
}, | |
GTK.Button { | |
id = "remove_embed", | |
label = "Remove" | |
}, | |
} | |
}, | |
GTK.Button { | |
id = "send", | |
label = "Send" | |
} | |
} | |
} | |
function window.child.title_renderer:on_edited(pathstr, new) | |
--0 indexed, convert to 1 indexed | |
embeds[tonumber(pathstr) + 1]["title"] = new | |
embed_model[GTK.TreePath.new_from_string(pathstr)][EMBED_COLUMNS.TITLE] = new | |
end | |
function window.child.desc_renderer:on_edited(pathstr, new) | |
embeds[tonumber(pathstr) + 1]["description"] = new | |
embed_model[GTK.TreePath.new_from_string(pathstr)][EMBED_COLUMNS.DESCRIPTION] = new | |
end | |
function window.child.colour_renderer:on_edited(pathstr, new) | |
embeds[tonumber(pathstr) + 1]["color"] = tonumber(new) | |
embed_model[GTK.TreePath.new_from_string(pathstr)][EMBED_COLUMNS.COLOUR] = new | |
end | |
function window.child.add_embed:on_clicked() | |
embeds[#embeds + 1] = { | |
title = embeds[1].title, | |
description = embeds[1].description, | |
color = embeds[1].color | |
} | |
embed_model:append { | |
[EMBED_COLUMNS.TITLE] = embeds[1].title, | |
[EMBED_COLUMNS.DESCRIPTION] = embeds[1].description, | |
[EMBED_COLUMNS.COLOUR] = embeds[1].color | |
} | |
end | |
local selection = window.child.embed_view:get_selection() | |
selection.mode = "SINGLE" | |
function window.child.remove_embed:on_clicked() | |
local model, sel = selection:get_selected() | |
if model and sel then model:remove(sel) end | |
end | |
function window.child.send:on_clicked() | |
---@type string | |
local url = window.child.webhook_url:get_buffer():get_text() | |
local success, bot_info = pcall(webhook_info, url) | |
if not success then | |
local dlg = GTK.MessageDialog { | |
transient_for = window, | |
modal = true, | |
message_type = "ERROR", | |
buttons = "OK", | |
text = "Could not get webhook info from URL \"" .. url .. "\"!" | |
} | |
dlg:run() | |
dlg:destroy() | |
return | |
end | |
local payload = json.encode { | |
["content"] = window.child.mainmessage:get_buffer():get_text(), | |
["embeds"] = embeds | |
} | |
local ret, code = https.request { | |
url = url, | |
method = "POST", | |
headers = { | |
["Content-Type"] = "application/json", | |
["Content-Length"] = payload:len() | |
}, | |
source = ltn12.source.string(payload) | |
} | |
if not ret or code ~= 204 then | |
local dlg = GTK.MessageDialog { | |
transient_for = window, | |
modal = true, | |
message_type = "ERROR", | |
buttons = "OK", | |
text = "Could not send message to webhook \"" .. url .. "\"\nReturn: "..ret..", Code: "..code.."!" | |
} | |
dlg:run() | |
dlg:destroy() | |
end | |
end | |
window:show_all() | |
GTK:main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment