discord-cli-node
Advanced tools
Comparing version
const readline = require('readline'); | ||
const ping = require('./managePing.js'); | ||
async function watchCli(client, channel) { | ||
// Create a readline interface for reading input from the CLI | ||
const rl = readline.createInterface({ | ||
@@ -10,5 +10,3 @@ input: process.stdin, | ||
// Prompt the user for input and send it to the channel when the user presses Enter | ||
rl.on('line', async (input) => { | ||
// If input contains only spaces or tab spaced than quit. | ||
if (input.trim() === '') { | ||
@@ -21,14 +19,13 @@ console.log('You can\'t send empty messages!'); | ||
} | ||
await channel.send(input); | ||
// Move the cursor to the bottom of the screen and prompt the user for more input | ||
readline.cursorTo(process.stdout, 0, process.stdout.rows - 1); | ||
rl.prompt(); | ||
else if (input === '!ping' || input === '!p') { | ||
rl.close(); | ||
await channel.send(await ping(client, channel)); | ||
watchCli(client, channel); | ||
} | ||
else { | ||
await channel.send(input); | ||
rl.prompt(); | ||
} | ||
}); | ||
// Add an event listener for the close event to exit the process | ||
rl.on('close', () => { | ||
process.exit(); | ||
}); | ||
rl.setPrompt(''); | ||
@@ -35,0 +32,0 @@ rl.prompt(); |
const chalk = require('chalk'); | ||
async function updates(client, channel) { | ||
// Find the channel to listen for new messages in | ||
// Fetch the last 50 messages in the channel | ||
const messages = await channel.messages.fetch({ limit: 50 }); | ||
// Print the last 50 messages to the console | ||
await messages.reverse().forEach(message => { | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.underline(` ${message.author.username}: `) + chalk.blue.bold(` ${message.content}`)); | ||
messages.reverse().reverse().forEach(message => { | ||
if (message.content.includes('<@')) { | ||
const userId = message.content.split('<@')[1].split('>')[0]; | ||
const user = client.users.cache.find(user => user.id === userId); | ||
const newMessage = message.content.replace(`<@${userId}>`, `@${user.username}`); | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.dim.underline(` ${chalk.bold(message.author.username)}:`) + chalk.blue.bold(` ${newMessage}`)); | ||
} | ||
else { | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.dim.underline(` ${chalk.bold(message.author.username)}:`) + chalk.blue.bold(` ${message.content}`)); | ||
} | ||
}); | ||
// Wait for new messages in the channel | ||
const collector = channel.createMessageCollector(() => true); | ||
await collector.on('collect', message => { | ||
// Short Timestamp, Username: Message | ||
// Make the timestamp lighter than the username, make the username bolder than everything else | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.dim.underline(` ${message.author.username}:`) + chalk.blue.bold(` ${message.content}`)); | ||
if (message.content.includes('<@')) { | ||
const userId = message.content.split('<@')[1].split('>')[0]; | ||
const user = client.users.cache.find(user => user.id === userId); | ||
const newMessage = message.content.replace(`<@${userId}>`, `@${user.username}`); | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.dim.underline(` ${chalk.bold(message.author.username)}:`) + chalk.blue.bold(` ${newMessage}`)); | ||
} | ||
else { | ||
console.log(chalk.yellowBright.dim.underline(`${message.createdAt.toLocaleTimeString()}`) + chalk.yellowBright.dim.underline(` ${chalk.bold(message.author.username)}:`) + chalk.blue.bold(` ${message.content}`)); | ||
} | ||
}); | ||
@@ -18,0 +30,0 @@ } |
require('dotenv').config(); | ||
const { PermissionsBitField } = require('discord.js'); | ||
const cliSelect = require('cli-select'); | ||
const { Select } = require('enquirer'); | ||
const updates = require('./messageUpdates.js'); | ||
@@ -8,39 +8,33 @@ const watchCli = require('./manageInputs.js'); | ||
async function startcli(client) { | ||
async function startCLI(client) { | ||
console.clear(); | ||
console.log('Please select the guild you will use to chat in:'); | ||
const guilds = client.guilds.cache.map(guild => guild.name + ' - ' + guild.id); | ||
cliSelect({ | ||
values: guilds, | ||
valueRenderer: (value, selected) => { | ||
if (selected) { | ||
return value + ' (Selected)'; | ||
} | ||
return value; | ||
}, | ||
}).then(response => { | ||
console.log(`You selected ${response.value}`); | ||
const guild = client.guilds.cache.find(guild => guild.id === response.value.split(' - ')[1]); | ||
console.log('Please select the channel you will use to chat in:'); | ||
const channels = guild.channels.cache.filter(channel => { | ||
const permissions = channel.permissionsFor(client.user); | ||
return channel.type == '0' && permissions.has(PermissionsBitField.Flags.SendMessages) && permissions.has(PermissionsBitField.Flags.ViewChannel); | ||
}).map(channel => channel.name + ' - ' + channel.id); | ||
cliSelect({ | ||
values: channels, | ||
valueRenderer: (value, selected) => { | ||
if (selected) { | ||
return value + ' (Selected)'; | ||
} | ||
return value; | ||
}, | ||
}).then(response2 => { | ||
// Extract the channel id from the string | ||
const channel = guild.channels.cache.find(channel => channel.id === response2.value.split(' - ')[1]); | ||
console.log(`You selected ${channel.id}`); | ||
channel.messages.fetch({ limit: 50 }); | ||
updates(client, channel); | ||
watchCli(client, channel); | ||
}); | ||
const guildPrompt = new Select({ | ||
name: 'guild', | ||
message: 'Select a guild', | ||
choices: guilds, | ||
}); | ||
const guildResponse = await guildPrompt.run(); | ||
console.log(`You selected ${guildResponse}`); | ||
const guild = client.guilds.cache.find(guild => guild.id === guildResponse.split(' - ')[1]); | ||
console.clear(); | ||
console.log('Please select the channel you will use to chat in:'); | ||
const channels = guild.channels.cache.filter(channel => { | ||
const permissions = channel.permissionsFor(client.user); | ||
return channel.type == '0' && permissions.has(PermissionsBitField.Flags.SendMessages) && permissions.has(PermissionsBitField.Flags.ViewChannel); | ||
}).map(channel => channel.name + ' - ' + channel.id); | ||
const channelPrompt = new Select({ | ||
name: 'channel', | ||
message: 'Select a channel', | ||
choices: channels, | ||
}); | ||
const channelResponse = await channelPrompt.run(); | ||
const channel = guild.channels.cache.find(channel => channel.id === channelResponse.split(' - ')[1]); | ||
console.log(`You selected ${channel.id}`); | ||
channel.messages.fetch({ limit: 50 }); | ||
updates(client, channel); | ||
watchCli(client, channel); | ||
} | ||
process.on('unhandledRejection', (err) => { | ||
@@ -51,2 +45,2 @@ console.log('Exiting', err); | ||
module.exports = startcli; | ||
module.exports = startCLI; |
38
index.js
#! /usr/bin/env node | ||
// Call in the modules and config and load the commands | ||
const startcli = require('./components/setupcli.js'); | ||
const startCLI = require('./components/setupCLI.js'); | ||
const { Client, GatewayIntentBits } = require('discord.js'); | ||
require('dotenv').config(); | ||
const { prompt } = require('enquirer'); | ||
@@ -19,6 +19,5 @@ | ||
client.once('ready', () => { | ||
startcli(client); | ||
startCLI(client); | ||
}); | ||
// check if the token exists in .env, if not ask for it, and then login to discord | ||
if (process.env.DISCORDTOKEN) { | ||
@@ -28,23 +27,12 @@ client.login(process.env.DISCORDTOKEN); | ||
else { | ||
const readline = require('readline'), | ||
rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
rl.input.on('keypress', function() { | ||
// get the number of characters entered so far: | ||
const len = rl.line.length; | ||
// move cursor back to the beginning of the input: | ||
readline.moveCursor(rl.output, -len, 0); | ||
// clear everything to the right of the cursor: | ||
readline.clearLine(rl.output, 1); | ||
// replace the original input with asterisks: | ||
for (let i = 0; i < len; i++) { | ||
rl.output.write('*'); | ||
} | ||
}); | ||
rl.question('Please enter your Discord Bot Token: ', (token) => { | ||
client.login(token); | ||
rl.close(); | ||
}); | ||
console.clear(); | ||
prompt({ | ||
type: 'password', | ||
name: 'token', | ||
message: 'Please enter your Discord Bot Token:', | ||
}) | ||
.then((answers) => { | ||
client.login(answers.token); | ||
}) | ||
.catch(console.error); | ||
} |
{ | ||
"name": "discord-cli-node", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A discord cli client written in node js.", | ||
@@ -9,6 +9,3 @@ "main": "index.js", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/kozmiknano/discord-cli-node.git" | ||
}, | ||
"repository": "git+https://github.com/kozmiknano/discord-cli-node.git", | ||
"keywords": [ | ||
@@ -32,5 +29,5 @@ "discord", | ||
"chalk": "^4.1.2", | ||
"cli-select": "^1.1.2", | ||
"discord.js": "^14.11.0", | ||
"dotenv": "^16.0.3", | ||
"enquirer": "^2.3.6", | ||
"eslint": "^8.41.0", | ||
@@ -37,0 +34,0 @@ "prompt-sync": "^4.2.0" |
# discord-cli-node | ||
This is a discord client written is node.js for the CLI. It allows you to send and read messages from a discord server. | ||
### Current limitations | ||
## Current limitations | ||
- No access to DMs | ||
@@ -11,31 +13,43 @@ - No access to servers you can't add a bot too | ||
## Usage | ||
*You can follow ##Instalation to download and install this project or use the npx package (`npx discord-cli-node`)* | ||
*You can follow ##Installation to download and install this project or use the npx package (`npx discord-cli-node`)* | ||
This project was created to help with development where it can be annoying to change windows just to try talking to a command. This can be used to send/read messages. | ||
**Commands in CLI** | ||
### Commands in CLI | ||
- !q, !e, !quit, !exit To exit the chat | ||
- !p, !ping To ping a user, you will be prompted for a user to ping (hit enter to autocomplete) and a message to send after the ping | ||
**Features** | ||
## Features | ||
- Read the last 50 messages sent in server | ||
- Send messages | ||
- View the time a message was sent, the user who sent it, and the message that was sent | ||
- Ping Server members (has autocomplete to help quickly finish names) | ||
## Instalation | ||
1. Make sure node v20+ and git is intsalled on your computer | ||
## Installation | ||
1. Make sure node v20+, yarn and git is installed on your computer | ||
2. Download and open the folder | ||
```bash | ||
git clone https://github.com/kozmiknano/discord-cli-node | ||
cd discord-cli-node | ||
``` | ||
```bash | ||
git clone https://github.com/kozmiknano/discord-cli-node | ||
cd discord-cli-node | ||
``` | ||
3. Install the deps | ||
```bash | ||
npm install | ||
``` | ||
```bash | ||
yarn | ||
``` | ||
4. Now you can [get a discord bot token](https://discordjs.guide/preparations/setting-up-a-bot-application.html#your-bot-s-token) and do one of two things | ||
- Just start the program (`npm start`) and paste your token in | ||
- Just start the program (`yarn start`) and paste your token in | ||
- Or, create a `.env` file and create a variable like so: | ||
```env | ||
DISCORDTOKEN=yourdiscordtokenhere | ||
``` | ||
Then run the program (`npm start`) | ||
Then run the program (`yarn start`) |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
12762
7.42%12
20%199
7.57%55
34.15%3
50%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed