switch to typescript

This commit is contained in:
Matthieu Jolimaitre 2024-11-24 17:07:26 +01:00
parent 0c313f9f48
commit cdec693d39
8 changed files with 104 additions and 32 deletions

68
src/discord-status-set.ts Executable file
View file

@ -0,0 +1,68 @@
#!/usr/bin/env -S deno run --allow-net=discord.com --allow-import=deno.land
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";
async function main() {
const args = await parse_args();
const r = await set_status(args.token, args.status, args.emote);
if (!r.ok) {
console.error("[discord-status-set.ts] API Response failure", r.status);
console.error(await r.text());
Deno.exit(1);
}
}
async function parse_args() {
const { args, options } = await new Command()
.name("discord-status-set")
.description("Set the status of your discord profile.")
.arguments("<token> <status>")
.option(
"-e, --emote <emote>",
"Emote to set the status to (format is '<name>:<id>').",
)
.parse();
const [token, status] = args;
let emote = undefined as Emote | undefined;
if (options.emote) {
const [name, id] = options.emote.split(":");
if (id === undefined) {
throw new Error(
`Incorrect emote format: '${options.emote}' instead of '<name>:<id>'`,
);
}
emote = { name, id };
}
return { token, status, emote };
}
async function set_status(
token: string,
status: string,
emote: Emote | undefined = undefined,
) {
return await fetch("https://discord.com/api/v10/users/@me/settings", {
method: "PATCH",
headers: {
"Content-Type": "application/json",
"authorization": token,
},
body: JSON.stringify({
"custom_status": {
"text": status,
...emote
? {
"emoji_name": emote.name,
"emoji_id": emote.id,
}
: {},
},
"activities": [],
"status": "online",
}),
});
}
type Emote = { name: string; id: string };
if (import.meta.main) await main();