From 14ab7af9890c3a5d6f9425a494f31bd3b3585e26 Mon Sep 17 00:00:00 2001 From: Lea Date: Sun, 26 Mar 2023 00:29:03 +0100 Subject: [PATCH] add client to apply color, small fixes --- client.html | 25 ++++++++++++++++ client.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 6 +++- index.js | 15 ++++++++-- 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 client.html create mode 100644 client.js diff --git a/client.html b/client.html new file mode 100644 index 0000000..453e51f --- /dev/null +++ b/client.html @@ -0,0 +1,25 @@ + + + + Apply gradient + + + + +

+
+

+ This tool lets you apply your new gradient to a role using a bot account. +
+ Once you enter a bot token, you will be shown a list of all roles in the servers the bot can access. +
+ The bot token is only sent to Revolt and will not be stored. +

+ + + +
+ +
+ + diff --git a/client.js b/client.js new file mode 100644 index 0000000..1ebf392 --- /dev/null +++ b/client.js @@ -0,0 +1,83 @@ +let colour; + +window.onload = () => { + colour = new URLSearchParams(window.location.search).get('colour'); + if (!colour) { + alert(`The 'colour' query parameter was not provided; cannot proceed!`); + document.getElementById('setup').hidden = true; + const statusText = document.getElementById('status'); + statusText.innerText = `The 'colour' query parameter was not provided; cannot proceed!`; + } + colour = decodeURIComponent(colour); +} + +function login() { + const statusText = document.getElementById('status'); + const token = document.getElementById('token').value; + document.getElementById('setup').hidden = true; + statusText.innerText = 'Connecting...'; + + const socket = new WebSocket(`wss://ws.revolt.chat?version=1&token=${token}`); + let success = false; + socket.onmessage = (event) => { + const data = JSON.parse(event.data); + console.log('<', data); + if (data.type == "Ready") { + success = true; + const servers = data.servers; + socket.close(); + + statusText.innerText = `Received ${servers.length} servers!`; + + const serverList = document.getElementById('servers'); + for (const server of servers) { + const title = document.createElement('h3'); + title.innerText = server.name; + serverList.appendChild(title); + + const roleDiv = document.createElement('div'); + serverList.appendChild(roleDiv); + + for (const role of Object.entries(server.roles)) { + const roleButton = document.createElement('button'); + roleButton.innerText = role[1].name; + roleDiv.appendChild(roleButton); + roleDiv.appendChild(document.createElement('br')); + + roleButton.onclick = async () => { + const response = confirm(`If your bot has access to it, this will update the color of the role "${role[1].name}" (${role[0]}) in the server "${server.name}" to your new gradient. Are you sure you want to continue?\n\nThe current color of this role is: ${role[1].colour}`); + if (!response) return; + + try { + const res = await fetch( + `https://api.revolt.chat/servers/${server._id}/roles/${role[0]}`, + { + method: 'PATCH', + headers: { + "X-Bot-Token": token, + "Content-Type": 'application/json', + }, + body: JSON.stringify({ + colour: colour, + }, null, 4), + } + ); + console.log(res); + alert(`Response: ${res.status} - ${res.statusText}`); + } catch(e) { + console.log(e); + alert('Error: ' + e); + } + } + } + } + } + } + + socket.onclose = (event) => { + if (!success) { + statusText.innerText = `Disconnected - ${event.code}`; + document.getElementById('setup').hidden = false; + } + } +} diff --git a/index.html b/index.html index 0aa6ce5..9a1c7f4 100644 --- a/index.html +++ b/index.html @@ -15,7 +15,8 @@

Revolt role gradient generator

This is a preview of your gradient.

- + +

Gradient type

@@ -62,6 +63,9 @@