guild switching and user listing

This commit is contained in:
Cynthia Foxwell 2022-08-27 19:53:55 -06:00
parent b6a4e5edff
commit 8591f3e002
2 changed files with 211 additions and 17 deletions

View file

@ -23,9 +23,9 @@ You **MUST** grant your bot all Privileged Gateway Intents.
## TODO ## TODO
- [x] Commands - [x] Commands
- [x] Quit (q) - [x] Quit (q)
- [ ] Switch guilds (G) - [x] Switch guilds (G)
- [ ] Switch channels (g) - [ ] Switch channels (g)
- [ ] List online users in guild (w) - [x] List online users in guild (w)
- [ ] Emote (e) - [ ] Emote (e)
- Just sends message surrounded in `*`'s - Just sends message surrounded in `*`'s
- [ ] Finger (f) - [ ] Finger (f)
@ -51,3 +51,4 @@ You **MUST** grant your bot all Privileged Gateway Intents.
- [ ] Default guild/channel - [ ] Default guild/channel
- [ ] Threads - [ ] Threads
- [ ] Not have the token just be in argv - [ ] Not have the token just be in argv
- [ ] Not have everything in one file

View file

@ -12,6 +12,8 @@ stdin.setEncoding("utf8");
let currentGuild, let currentGuild,
currentChannel, currentChannel,
inSendMode = false, inSendMode = false,
guildSwitch = false,
channelSwitch = false,
nameLength = 2; nameLength = 2;
const messageQueue = []; const messageQueue = [];
@ -45,6 +47,8 @@ client.once("ready", function () {
) )
); );
nameLength = client.user.username.length + 2; nameLength = client.user.username.length + 2;
listGuilds();
}); });
function processMessage({name, content, bot}) { function processMessage({name, content, bot}) {
@ -58,9 +62,8 @@ function processMessage({name, content, bot}) {
} else { } else {
// TODO: markdown // TODO: markdown
console.log( console.log(
chalk.bold.cyan( chalk.bold.cyan(`[${name}]`).padEnd(nameLength, " ") +
`[${name}]` + " ".repeat(nameLength - (name.length + 2)) chalk.reset(" " + content)
) + chalk.reset(" " + content)
); );
} }
} }
@ -112,22 +115,32 @@ client.on("messageCreate", function (msg) {
}); });
let toSend = ""; let toSend = "";
function setupSendMode() { async function setupSendMode() {
inSendMode = true; inSendMode = true;
toSend = ""; toSend = "";
const name = `[${client.user.username}]`;
stdout.write( stdout.write(
chalk.bold.cyan(name) + chalk.bold.cyan(`[${client.user.username}]`).padEnd(nameLength, " ") +
" ".repeat(nameLength - name.length) +
chalk.reset(" ") chalk.reset(" ")
); );
try {
await client.guilds
.get(currentGuild)
.channels.get(currentChannel)
.sendTyping();
} catch (err) {
//
}
} }
function sendMessage() { async function sendMessage() {
toSend = toSend.trim(); toSend = toSend.trim();
if (toSend === "") { if (toSend === "") {
stdout.write("<no message sent>\n"); stdout.write("<no message sent>\n");
} else { } else {
client.createMessage(currentChannel, toSend); try {
await client.createMessage(currentChannel, toSend);
} catch (err) {
console.log("<failed to send message: " + err.message + ">");
}
} }
inSendMode = false; inSendMode = false;
processQueue(); processQueue();
@ -142,13 +155,12 @@ function showHelp() {
let index = 0; let index = 0;
for (const key of keys) { for (const key of keys) {
const desc = commands[key]; const desc = commands[key];
const length = ` ${key} - ${desc}`.length;
stdout.write( stdout.write(
" " + (" " + chalk.bold.yellow(key) + chalk.reset(" - " + desc)).padEnd(
chalk.bold.yellow(key) + 25,
chalk.reset(" - " + desc) + " "
" ".repeat(Math.abs(25 - length)) )
); );
index++; index++;
@ -159,9 +171,160 @@ function showHelp() {
console.log("\nTo begin TALK MODE, press [SPACE]\n"); console.log("\nTo begin TALK MODE, press [SPACE]\n");
} }
function listGuilds() {
let longest = 0;
const guilds = [];
for (const guild of client.guilds.values()) {
if (guild.name.length > longest) longest = guild.name.length;
const online = [...guild.members.values()].filter((m) => m.status).length;
guilds.push({name: guild.name, members: guild.memberCount, online});
}
console.log("");
console.log(" " + "guild-name".padStart(longest, " ") + " online total");
console.log("-".repeat(80));
for (const guild of guilds) {
console.log(
" " +
guild.name.padStart(longest, " ") +
" " +
guild.online.toString().padStart(6, " ") +
" " +
guild.members.toString().padStart(5, " ")
);
}
console.log("");
}
let targetGuild = "";
function gotoGuild() {
targetGuild = "";
guildSwitch = true;
stdout.write(":guild> ");
}
function findTopChannel(guildId) {
const guild = client.guilds.get(guildId);
const channels = [...guild.channels.values()].filter((c) => c.type == 0);
channels.sort((a, b) => a.position - b.position);
return channels[0];
}
function getStatus(status) {
let color;
switch (status) {
case "online":
color = chalk.bold.green;
break;
case "idle":
color = chalk.bold.yellow;
break;
case "dnd":
color = chalk.bold.red;
break;
default:
color = chalk.bold;
break;
}
return color(" \u2022 ");
}
function listUsers() {
const guild = client.guilds.get(currentGuild);
const channel = guild.channels.get(currentChannel);
console.log(
`\n[you are in '${guild.name}' in '${channel.name}' among ${guild.memberCount}]\n`
);
const online = [...guild.members.values()].filter((m) => m.status);
online.sort((a, b) => a.name - b.name);
let longest = 0;
for (const member of online) {
const name = member.user.username + "#" + member.user.discriminator;
if (name.length + 3 > longest) longest = name.length + 3;
}
const columns = Math.ceil(stdout.columns / longest);
let index = 0;
for (const member of online) {
const name = member.user.username + "#" + member.user.discriminator;
const status = getStatus(member.status);
const nameAndStatus = chalk.reset(name) + status;
index++;
stdout.write(
nameAndStatus +
" ".repeat(
index % columns == 0 ? 0 : Math.abs(longest - (name.length + 3))
)
);
if (index % columns == 0) stdout.write("\n");
}
if (index % columns != 0) stdout.write("\n");
console.log("");
if (channel.topic != null) {
console.log("--Topic".padEnd(80, "-"));
console.log(channel.topic);
console.log("-".repeat(80));
console.log("");
}
}
function switchGuild() {
let target;
for (const guild of client.guilds.values()) {
if (guild.name.toLowerCase().indexOf(targetGuild.toLowerCase()) > -1) {
target = guild.id;
break;
}
}
if (target == null) {
console.log("<guild not found>");
} else {
currentGuild = target;
// TODO: store last visited channel
const topChannel = findTopChannel(target);
currentChannel = topChannel.id;
listUsers();
}
guildSwitch = false;
}
stdin.on("data", function (key) { stdin.on("data", function (key) {
if (inSendMode) { if (guildSwitch) {
if (key === "\r") { if (key === "\r") {
console.log("");
switchGuild();
} else {
if (key === "\b") {
if (targetGuild.length > 0) {
stdout.moveCursor(-1);
stdout.write(" ");
stdout.moveCursor(-1);
targetGuild = targetGuild.substring(0, targetGuild.length - 1);
}
} else {
stdout.write(key);
targetGuild += key;
}
}
} else if (inSendMode) {
if (key === "\r") {
console.log("");
sendMessage(); sendMessage();
} else { } else {
if (key === "\b") { if (key === "\b") {
@ -188,6 +351,36 @@ stdin.on("data", function (key) {
showHelp(); showHelp();
break; break;
} }
case "g": {
if (currentGuild == null) {
console.log("<not in a guild>");
break;
}
break;
}
case "G": {
gotoGuild();
break;
}
case "l": {
if (currentGuild == null) {
console.log("<not in a guild>");
break;
}
break;
}
case "L": {
listGuilds();
break;
}
case "w": {
if (currentGuild == null) {
console.log("<not in a guild>");
break;
}
listUsers();
break;
}
case " ": case " ":
case "\r": case "\r":
default: { default: {