Skip to content

Commit

Permalink
feat: add support for kicking members from servers
Browse files Browse the repository at this point in the history
partial fix for #1 (no support for groups yet)
  • Loading branch information
Rexogamer committed Dec 1, 2021
1 parent 30866c2 commit 10e8f73
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 5 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)> <server id (in quotes)> <user id (in quotes)>`

#### 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.
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.
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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)> <token> <channel> <message content (in quotes)>\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)> <token> <channel ID> <message content (in quotes)>\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)> <token> <server ID> <user ID>\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(
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { sendMsg } from "./send.js";
export { kick } from "./kick.js";
export { default as styles } from "./styles.js";
105 changes: 105 additions & 0 deletions src/lib/kick.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 10e8f73

Please sign in to comment.