From 0b1387f7b9cea9c2420437b17b7f314baa32cfb5 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sat, 27 Aug 2022 21:56:02 -0600 Subject: [PATCH] add channel listing --- README.md | 2 +- src/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fe7ddad..2531bae 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ You **MUST** grant your bot all Privileged Gateway Intents. - [ ] Creation date, join date, ID, etc - [x] Room history (r) - [x] Extended room history (R) - - [ ] List channels (l) + - [x] List channels (l) - [x] List guilds (L) - [x] Clear (c) - [ ] Surf channels forwards (>) diff --git a/src/index.js b/src/index.js index 0bf8eed..04fd441 100644 --- a/src/index.js +++ b/src/index.js @@ -313,6 +313,42 @@ function listGuilds() { console.log(""); } +function listChannels() { + let longest = 0; + let longestTopic = 0; + const guild = client.guilds.get(currentGuild); + const channels = [...guild.channels.values()].filter((c) => c.type == 0); + channels.sort((a, b) => a.position - b.position); + + for (const channel of channels) { + if (channel.name.length > longest) longest = channel.name.length; + if (channel.topic != null && channel.topic.length > longestTopic) + longestTopic = channel.topic.length; + } + + console.log(""); + console.log( + " " + + "channel-name".padStart(longest, " ") + + " " + + "topic".padStart(Math.min(80 - (longest + 5), longestTopic), " ") + ); + console.log("-".repeat(80)); + for (const channel of channels) { + const topic = + channel.topic != null ? channel.topic.replace(/\n/g, " ") : ""; + console.log( + " " + + channel.name.padStart(longest, " ") + + " " + + (topic.length > 80 - longest + 9 + ? topic.substring(0, 79 - (longest + 5)) + "\u2026" + : topic.padStart(Math.min(80 - (longest + 5), longestTopic), " ")) + ); + } + console.log(""); +} + let targetGuild = ""; function gotoGuild() { targetGuild = ""; @@ -398,6 +434,7 @@ function listUsers() { function switchGuild() { targetGuild = targetGuild.trim(); if (targetGuild == "") { + listChannels(); listUsers(); guildSwitch = false; return; @@ -420,6 +457,7 @@ function switchGuild() { const topChannel = findTopChannel(target); currentChannel = topChannel.id; + listChannels(); listUsers(); } @@ -667,6 +705,7 @@ stdin.on("data", function (key) { console.log(""); break; } + listChannels(); break; } case "L": {