From 10e8f732ef4fbdab9322571eec242090861fc509 Mon Sep 17 00:00:00 2001 From: Ed L Date: Wed, 1 Dec 2021 22:28:40 +0000 Subject: [PATCH] feat: add support for kicking members from servers partial fix for #1 (no support for groups yet) --- README.md | 19 +++++++-- src/index.js | 27 +++++++++++- src/lib/index.js | 1 + src/lib/kick.js | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 src/lib/kick.js diff --git a/README.md b/README.md index 725f959..f0a8539 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,27 @@ To send messages with Termivolt, run `termivolt -send`. Here's the full list of #### Arguments - `--user/--bot` determines whether the token is a bot or session token. These require different methods of authenticating. Session tokens are currently broken - a fix will be released in the future. -- The token is provided as-is (ie as copied from Revolt). Bot tokens can be found in your bot settings page - to get session tokens, [follow this guide](https://infi.sh/post/revolt-tokens). -- The channel ID should be provided as a string (ie in quotes). You can find it in the URL when using Revite (the official Revolt client) or by right-clicking the channel's entry on the channel list and selecting "Copy channel ID". +- The token is provided as-is (i.e. as copied from Revolt). Bot tokens can be found in your bot settings page - to get session tokens, [follow this guide](https://infi.sh/post/revolt-tokens). +- The channel ID should be provided as a string (i.e. in quotes). You can find it in the URL when using Revite (the official Revolt client) or by right-clicking the channel's entry on the channel list and selecting "Copy channel ID". - The message itself should be fully encased in double quotes - if you want to use double quotes in the message itself, escape them with a backslash. Note that message formatting may be messed up in some cases - I'm still investigating as to why, but it seems backticks and \newlines break. +### Kicking users (-send) + +To kick members from servers with Termivolt, run `termivolt -send`. Note that you'll need the `Kick Members` permission - if you get a 403 error, this might be why. Here's the full list of arguments: + +`termivolt -kick <(--user/--bot)> ` + +#### Arguments + +- `--user/--bot` determines whether the token is a bot or session token. These require different methods of authenticating. Session tokens are currently broken - a fix will be released in the future. +- The token is provided as-is (i.e. as copied from Revolt). Bot tokens can be found in your bot settings page - to get session tokens, [follow this guide](https://infi.sh/post/revolt-tokens). +- The server ID should be provided as a string (i.e. in quotes). You can find it in the URL when using Revite (the official Revolt client) or by right-clicking the server's entry on the server list and selecting "Copy server ID". +- The user ID should aslo be provided as a string. + ### Help (-help) If you need help, or want to see a list of commands, run `termivolt -help`. This will also show you what version of Termivolt you're using, which is useful for bug reports and such. ## Support -If you want to report a bug, suggest a feature or get help with using Termivolt, you can [open an issue](https://github.com/rexogamer/termivolt/issues/new) or join [Termivolt's support server](https://rvlt.gg/ra9dr2Rd) on Revolt. \ No newline at end of file +If you want to report a bug, suggest a feature or get help with using Termivolt, you can [open an issue](https://github.com/rexogamer/termivolt/issues/new) or join [Termivolt's support server](https://rvlt.gg/ra9dr2Rd) on Revolt. diff --git a/src/index.js b/src/index.js index 9df9aa1..1bcb779 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ #! /usr/bin/env node import chalk from "chalk"; -import { styles, sendMsg } from "./lib/index.js"; +import { kick, styles, sendMsg } from "./lib/index.js"; import { promises as fs } from "fs"; const [nodeExec, scriptPath, command, ...args] = process.argv; @@ -17,6 +17,11 @@ if (command) { ); sendMsg(args[0], args[1], args[2], args[3]); break; + case "-kick": + if (args.length > 4) + console.log(styles.error("You've specified more than 4 arguments.")); + kick(args[0], args[1], args[2], args[3]); + break; case "-help": // get version data from package.json try { @@ -30,7 +35,25 @@ if (command) { const data = JSON.parse(await fs.readFile("package.json")); // send help message - console.log(`${styles.title(`Termivolt v${data.version}`)}\nTermivolt is a simple utility to interact with the Revolt API via the command line.\n\n${styles.header("Commands:\n")}${chalk.bold("-send:")} Sends a message via the specified account.\n${chalk.underline("Example usage:")} termviolt -send <(--user/--bot)> \n${chalk.underline("Notes:")} Formatting may be broken in some cases. In addition, sending via user accounts (using session tokens) currently seems to be broken - a fix will be released in the future.`); + console.log( + `${styles.title( + `Termivolt v${data.version}` + )}\nTermivolt is a simple utility to interact with the Revolt API via the command line.\n\n${styles.header( + "Commands:\n" + )}${chalk.bold( + "-send:" + )} Sends a message via the specified account.\n${chalk.underline( + "Example usage:" + )} termviolt -send <(--user/--bot)> \n${chalk.underline( + "Notes:" + )} Formatting may be broken in some cases. In addition, sending via user accounts (using session tokens) currently seems to be broken - a fix will be released in the future.\n\n${chalk.bold( + "-kick:" + )} Kicks a member from the specified server.\n${chalk.underline( + "Example usage:" + )} termviolt -kick <(--user/--bot)> \n${chalk.underline( + "Notes:" + )} This requires the Kick Members permission - if you get a 403 error, this might be why. In addition, sending via user accounts (using session tokens) currently seems to be broken - a fix will be released in the future.` + ); break; default: console.log( diff --git a/src/lib/index.js b/src/lib/index.js index 2586809..9677a65 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -1,2 +1,3 @@ export { sendMsg } from "./send.js"; +export { kick } from "./kick.js"; export { default as styles } from "./styles.js"; diff --git a/src/lib/kick.js b/src/lib/kick.js new file mode 100644 index 0000000..8519dc8 --- /dev/null +++ b/src/lib/kick.js @@ -0,0 +1,105 @@ +import { Client } from "revolt.js"; + +import styles from "./styles.js"; + +const kick = function (userType, token, server, userid) { + // if any args are missing, throw errors before doing anything + if (!userType) + return ( + console.log( + styles.error( + "You need to specify if the account is a user or a bot (--user or --bot), a token, a server ID and the ID of the user to kick (all in quotes)." + ) + ) && process.exit() + ); + if (!token) + return ( + console.log( + styles.error( + "You need to specify a token, a server ID and the ID of the user to kick (the last two in quotes)." + ) + ) && process.exit() + ); + if (!server) + return ( + console.log( + styles.error( + "You need to specify a server ID and the ID of the user to kick (both in quotes)." + ) + ) && process.exit() + ); + if (!userid) + return ( + console.log( + styles.error( + "You need to specify the content of the message (in quotes)." + ) + ) && process.exit() + ); + + // check and get user type + const checkIfUser = function (type) { + switch (type) { + case "--user": + return true; + case "--bot": + return false; + default: + return "invalid"; + } + }; + const isUser = checkIfUser(userType); + if (isUser === "invalid") + console.log( + styles.error( + "The user type was invalid - make sure to specify if you're using a seisson token (--user) or a bot token (--bot)." + ) + ); + + // log in + const client = new Client(); + try { + isUser ? client.useExistingSession(token) : client.loginBot(token); + console.log(styles.info("[INFO] Logged in.")); + } catch (error) { + console.log( + styles.error( + `There was an issue logging in - are your token and user type correct?\nThe issue was: ${error}` + ) + ); + } + + client.on("ready", async () => { + try { + const srv = client.servers?.get(server); + if (srv === undefined) throw error; + } catch (error) { + console.log( + styles.error( + `There was an issue getting the server - is the ID correct?\nThe error was: ${error}` + ) + ) && client.logout(); + process.exit(); + } + + const server2 = client.servers?.get(server); + console.log(styles.info("[INFO] The server has been found.")); + + // send the message + try { + await (await server2.fetchMember(userid)).kick(); + console.log(styles.success("The user has been kicked.")); + } catch (error) { + console.log( + styles.error( + `There was an issue sending the message.\n\nThe error was: ${error}` + ) + ); + } + + // for SOME reason we need to end the process manually after sending the message - is something lingering? + process.kill(process.pid); + }); +}; + +export { kick };