From 74d92bbf6bb0ba786d2bbdd7feb5d2462932a51b Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 18:45:00 -0600 Subject: [PATCH] join and pin messages --- src/commands/privateMessages.js | 17 +++++++---- src/commands/quit.js | 2 +- src/index.js | 36 +++++++++++++++++------- src/lib/messages.js | 50 ++++++++++++++++++++++++++++----- 4 files changed, 82 insertions(+), 23 deletions(-) diff --git a/src/commands/privateMessages.js b/src/commands/privateMessages.js index a85d0ea..eed1aff 100644 --- a/src/commands/privateMessages.js +++ b/src/commands/privateMessages.js @@ -4,15 +4,20 @@ const {addCommand} = require("../lib/command"); const {startPrompt} = require("../lib/prompt"); const {listUsers} = require("./listUsers"); -function startDM(user) { +function startDM(channel) { startPrompt(":msg> ", async function (input) { if (input == "") { - console.log(`\n`); + console.log( + `\n` + ); } else { try { - const channel = await comcord.client.getDMChannel(user.id); await channel.createMessage({content: input}); - console.log(chalk.bold.green(`\n`)); + console.log( + chalk.bold.green( + `\n` + ) + ); } catch (err) { console.log(`\n`); } @@ -43,7 +48,9 @@ addCommand("s", "send private", function () { addCommand("a", "answer a send", function () { if (comcord.state.lastDM) { console.log( - chalk.bold.green(``) + chalk.bold.green( + `` + ) ); startDM(comcord.state.lastDM); } else { diff --git a/src/commands/quit.js b/src/commands/quit.js index aafaac5..931d5a2 100644 --- a/src/commands/quit.js +++ b/src/commands/quit.js @@ -1,6 +1,6 @@ const {addCommand} = require("../lib/command"); addCommand("q", "quit comcord", function () { - comcord.client.disconnect(false); + comcord.client.disconnect({reconnect: false}); process.exit(0); }); diff --git a/src/index.js b/src/index.js index 8a95668..0dbab9f 100644 --- a/src/index.js +++ b/src/index.js @@ -154,15 +154,20 @@ client.on("messageCreate", async function (msg) { msg.author.id != client.user.id && !msg.guildID ) { - const newChannel = await client.getDMChannel(msg.author.id); - if (msg.channel.id == newChannel.id) msg.channel = newChannel; + if (msg.channel.type === Constants.ChannelTypes.DM) { + const newChannel = await client.getDMChannel(msg.author.id); + if (msg.channel.id == newChannel.id) msg.channel = newChannel; + } else if (msg.channel.type === Constants.ChannelTypes.GROUP_DM) { + // TODO + } } if (!(msg.channel instanceof Channel)) return; if ( msg.channel.id == comcord.state.currentChannel || - msg.channel?.recipient != null + msg.channel.type === Constants.ChannelTypes.DM || + msg.channel.type === Constants.ChannelTypes.GROUP_DM ) { if (comcord.state.inPrompt) { comcord.state.messageQueue.push(msg); @@ -171,8 +176,11 @@ client.on("messageCreate", async function (msg) { } } - if (msg.channel?.recipient != null) { - comcord.state.lastDM = msg.author; + if ( + msg.channel.type === Constants.ChannelTypes.DM || + msg.channel.type === Constants.ChannelTypes.GROUP_DM + ) { + comcord.state.lastDM = msg.channel; } }); client.on("messageUpdate", async function (msg, old) { @@ -183,15 +191,20 @@ client.on("messageUpdate", async function (msg, old) { msg.author.id != client.user.id && !msg.guildID ) { - const newChannel = await client.getDMChannel(msg.author.id); - if (msg.channel.id == newChannel.id) msg.channel = newChannel; + if (msg.channel.type === Constants.ChannelTypes.DM) { + const newChannel = await client.getDMChannel(msg.author.id); + if (msg.channel.id == newChannel.id) msg.channel = newChannel; + } else if (msg.channel.type === Constants.ChannelTypes.GROUP_DM) { + // TODO + } } if (!(msg.channel instanceof Channel)) return; if ( msg.channel.id == comcord.state.currentChannel || - msg.channel?.recipient != null + msg.channel.type === Constants.ChannelTypes.DM || + msg.channel.type === Constants.ChannelTypes.GROUP_DM ) { if (old && msg.content == old.content) return; @@ -202,8 +215,11 @@ client.on("messageUpdate", async function (msg, old) { } } - if (msg.channel?.recipient != null) { - comcord.state.lastDM = msg.author; + if ( + msg.channel.type === Constants.ChannelTypes.DM || + msg.channel.type === Constants.ChannelTypes.GROUP_DM + ) { + comcord.state.lastDM = msg.channel; } }); diff --git a/src/lib/messages.js b/src/lib/messages.js index ab26ade..d4917f8 100644 --- a/src/lib/messages.js +++ b/src/lib/messages.js @@ -1,3 +1,4 @@ +const {Constants} = require("@projectdysnomia/dysnomia"); const chalk = require("chalk"); const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/i; @@ -154,17 +155,26 @@ function replaceTimestamps(_, time, format = "f") { } function formatMessage({ + channel, name, content, bot, attachments, stickers, reply, + timestamp, noColor = false, dump = false, history = false, dm = false, + join = false, + pin = false, }) { + const dateObj = new Date(timestamp); + const hour = dateObj.getUTCHours().toString().padStart(2, "0"), + minutes = dateObj.getUTCMinutes().toString().padStart(2, "0"), + seconds = dateObj.getUTCSeconds().toString().padStart(2, "0"); + if (name.length + 2 > comcord.state.nameLength) comcord.state.nameLength = name.length + 2; @@ -263,14 +273,25 @@ function formatMessage({ content.endsWith("*")) || (content.startsWith("_") && content.endsWith("_")) ) { + const str = `<${name} ${content.substring(1, content.length - 1)}>`; if (noColor) { - console.log(`<${name} ${content.substring(1, content.length - 1)}>`); + console.log(str); } else { - console.log( - chalk.bold.green( - `<${name} ${content.substring(1, content.length - 1)}>` - ) - ); + console.log(chalk.bold.green(str)); + } + } else if (join) { + const str = `[${hour}:${minutes}:${seconds}] ${name} has joined ${channel.guild.name}`; + if (noColor) { + console.log(str); + } else { + console.log(chalk.bold.yellow(str)); + } + } else if (pin) { + const str = `[${hour}:${minutes}:${seconds}] ${name} pinned a message to this channel`; + if (noColor) { + console.log(str); + } else { + console.log(chalk.bold.yellow(str)); } } else { if (noColor) { @@ -320,15 +341,25 @@ function formatMessage({ } function processMessage(msg, options = {}) { - if (msg.channel?.recipient) { + if ( + msg.channel?.type === Constants.ChannelTypes.DM || + msg.channel?.type === Constants.ChannelTypes.GROUP_DM + ) { options.dm = true; } + if (msg.type === Constants.MessageTypes.USER_JOIN) { + options.join = true; + } else if (msg.type === Constants.MessageTypes.CHANNEL_PINNED_MESSAGE) { + options.pin = true; + } + if (msg.time) { console.log(msg.content); } else if (msg.content && msg.content.indexOf("\n") > -1) { if (msg.content.match(REGEX_CODEBLOCK)) { formatMessage({ + channel: msg.channel, name: msg.author.username, bot: msg.author.bot, content: msg.content.replace( @@ -338,6 +369,7 @@ function processMessage(msg, options = {}) { attachments: msg.attachments, stickers: msg.stickerItems, reply: msg.referencedMessage, + timestamp: msg.timestamp, dump: true, ...options, }); @@ -346,6 +378,7 @@ function processMessage(msg, options = {}) { for (const index in lines) { const line = lines[index]; formatMessage({ + channel: msg.channel, name: msg.author.username, bot: msg.author.bot, content: @@ -356,18 +389,21 @@ function processMessage(msg, options = {}) { attachments: index == lines.length - 1 ? msg.attachments : [], stickers: index == lines.length - 1 ? msg.stickerItems : [], reply: index == 0 ? msg.referencedMessage : null, + timestamp: msg.timestamp, ...options, }); } } } else { formatMessage({ + channel: msg.channel, name: msg.author.username, bot: msg.author.bot, content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""), attachments: msg.attachments, stickers: msg.stickerItems, reply: msg.referencedMessage, + timestamp: msg.timestamp, ...options, }); }