Skip to content

Commit

Permalink
feat: add ban support
Browse files Browse the repository at this point in the history
2/5 for #1
  • Loading branch information
Rexogamer committed Jan 23, 2022
1 parent 8819e29 commit f9f9c63
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ To send messages with Termivolt, run `termivolt -send`. Here's the full list of
- 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)
### Kicking users (-kick)

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:
To kick members from servers with Termivolt, run `termivolt -kick`. 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)> <token> <server id (in quotes)> <user id (in quotes)>`

Expand All @@ -54,6 +54,19 @@ To kick members from servers with Termivolt, run `termivolt -send`. Note that yo
- 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.

### Banning users (-ban)

To ban members from servers with Termivolt, run `termivolt -ban`. Note that you'll need the `Ban Members` permission - if you get a 403 error, this might be why. Here's the full list of arguments:

`termivolt -ban <(--user/--bot)> <token> <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 authentication.
- 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.
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env node --no-warnings --experimental-specifier-resolution=node --experimental-json-modules

import chalk from "chalk";
import { kick, styles, sendMsg } from "./lib/index.js";
import { ban, kick, sendMsg, styles } from "./lib/index.js";
import packageinfo from "../package.json"; // assert { type: "json" };

const [nodeExec, scriptPath, command, ...args] = process.argv;
Expand All @@ -22,6 +22,11 @@ if (command) {
console.log(styles.error("You've specified more than 4 arguments."));
kick(args[0], args[1], args[2], args[3]);
break;
case "-ban":
if (args.length > 4)
console.log(styles.error("You've specified more than 4 arguments."));
ban(args[0], args[1], args[2], args[3]);
break;
case "-help":
// send help message
console.log(
Expand All @@ -41,7 +46,13 @@ if (command) {
"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.`
)} This requires the Kick Members permission - if you get a 403 error, this might be why.\n\n${chalk.bold(
"-ban:"
)} Bans a member from the specified server.\n${chalk.underline(
"Example usage:"
)} termviolt -ban <(--user/--bot)> <token> <server ID> <user ID>\n${chalk.underline(
"Notes:"
)} This requires the Ban Members permission - if you get a 403 error, this might be why.`
);
break;
default:
Expand Down
106 changes: 106 additions & 0 deletions src/lib/ban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Client } from "revolt.js";

import styles from "./styles.js";

const ban = 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 ban (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 ban (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 ban (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 {
const reason = undefined; // todo: add reason support
await server2.banUser(userid, { reason });
console.log(styles.success("The user has been banned."));
} catch (error) {
console.log(
styles.error(
`There was an issue banning the user.\n\nThe error was: ${error}`
)
);
}

// for SOME reason we need to end the process manually after banning the user - is something lingering?
process.kill(process.pid);
});
};

export { ban };
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { sendMsg } from "./send.js";
export { kick } from "./kick.js";
export { ban } from "./ban.js"
export { default as styles } from "./styles.js";
4 changes: 2 additions & 2 deletions src/lib/kick.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ const kick = function (userType, token, server, userid) {
} catch (error) {
console.log(
styles.error(
`There was an issue sending the message.\n\nThe error was: ${error}`
`There was an issue kicking the user.\n\nThe error was: ${error}`
)
);
}

// for SOME reason we need to end the process manually after sending the message - is something lingering?
// for SOME reason we need to end the process manually after kicking the user - is something lingering?
process.kill(process.pid);
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client } from "revolt.js";

import styles from "./styles.js";

const sendMsg = function (userType, token, channel, content) {
const sendMsg = function (userType, token, channel, content, apiURL = "https://api.revolt.chat/") {
// if any args are missing, throw errors before doing anything
if (!userType)
return (
Expand Down Expand Up @@ -57,7 +57,7 @@ const sendMsg = function (userType, token, channel, content) {
);

// log in
const client = new Client();
const client = new Client({apiURL});
try {
isUser ? client.useExistingSession({token}) : client.loginBot(token);
console.log(styles.info("[INFO] Logged in."));
Expand Down

0 comments on commit f9f9c63

Please sign in to comment.