join and pin messages

This commit is contained in:
Cynthia Foxwell 2023-06-20 18:45:00 -06:00
parent 50e44abaff
commit 74d92bbf6b
4 changed files with 82 additions and 23 deletions

View File

@ -4,15 +4,20 @@ const {addCommand} = require("../lib/command");
const {startPrompt} = require("../lib/prompt"); const {startPrompt} = require("../lib/prompt");
const {listUsers} = require("./listUsers"); const {listUsers} = require("./listUsers");
function startDM(user) { function startDM(channel) {
startPrompt(":msg> ", async function (input) { startPrompt(":msg> ", async function (input) {
if (input == "") { if (input == "") {
console.log(`\n<message not sent to ${user.username}>`); console.log(
`\n<message not sent to ${channel.recipent?.username ?? "group DM"}>`
);
} else { } else {
try { try {
const channel = await comcord.client.getDMChannel(user.id);
await channel.createMessage({content: input}); await channel.createMessage({content: input});
console.log(chalk.bold.green(`\n<message sent to ${user.username}>`)); console.log(
chalk.bold.green(
`\n<message sent to ${channel.recipient?.username ?? "group DM"}>`
)
);
} catch (err) { } catch (err) {
console.log(`\n<failed to send message: ${err.message}>`); console.log(`\n<failed to send message: ${err.message}>`);
} }
@ -43,7 +48,9 @@ addCommand("s", "send private", function () {
addCommand("a", "answer a send", function () { addCommand("a", "answer a send", function () {
if (comcord.state.lastDM) { if (comcord.state.lastDM) {
console.log( console.log(
chalk.bold.green(`<answering ${comcord.state.lastDM.username}>`) chalk.bold.green(
`<answering ${comcord.state.lastDM.recipient?.username ?? "group DM"}>`
)
); );
startDM(comcord.state.lastDM); startDM(comcord.state.lastDM);
} else { } else {

View File

@ -1,6 +1,6 @@
const {addCommand} = require("../lib/command"); const {addCommand} = require("../lib/command");
addCommand("q", "quit comcord", function () { addCommand("q", "quit comcord", function () {
comcord.client.disconnect(false); comcord.client.disconnect({reconnect: false});
process.exit(0); process.exit(0);
}); });

View File

@ -154,15 +154,20 @@ client.on("messageCreate", async function (msg) {
msg.author.id != client.user.id && msg.author.id != client.user.id &&
!msg.guildID !msg.guildID
) { ) {
const newChannel = await client.getDMChannel(msg.author.id); if (msg.channel.type === Constants.ChannelTypes.DM) {
if (msg.channel.id == newChannel.id) msg.channel = newChannel; 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 instanceof Channel)) return;
if ( if (
msg.channel.id == comcord.state.currentChannel || 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) { if (comcord.state.inPrompt) {
comcord.state.messageQueue.push(msg); comcord.state.messageQueue.push(msg);
@ -171,8 +176,11 @@ client.on("messageCreate", async function (msg) {
} }
} }
if (msg.channel?.recipient != null) { if (
comcord.state.lastDM = msg.author; 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) { 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.author.id != client.user.id &&
!msg.guildID !msg.guildID
) { ) {
const newChannel = await client.getDMChannel(msg.author.id); if (msg.channel.type === Constants.ChannelTypes.DM) {
if (msg.channel.id == newChannel.id) msg.channel = newChannel; 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 instanceof Channel)) return;
if ( if (
msg.channel.id == comcord.state.currentChannel || 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; if (old && msg.content == old.content) return;
@ -202,8 +215,11 @@ client.on("messageUpdate", async function (msg, old) {
} }
} }
if (msg.channel?.recipient != null) { if (
comcord.state.lastDM = msg.author; msg.channel.type === Constants.ChannelTypes.DM ||
msg.channel.type === Constants.ChannelTypes.GROUP_DM
) {
comcord.state.lastDM = msg.channel;
} }
}); });

View File

@ -1,3 +1,4 @@
const {Constants} = require("@projectdysnomia/dysnomia");
const chalk = require("chalk"); const chalk = require("chalk");
const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/i; const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/i;
@ -154,17 +155,26 @@ function replaceTimestamps(_, time, format = "f") {
} }
function formatMessage({ function formatMessage({
channel,
name, name,
content, content,
bot, bot,
attachments, attachments,
stickers, stickers,
reply, reply,
timestamp,
noColor = false, noColor = false,
dump = false, dump = false,
history = false, history = false,
dm = 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) if (name.length + 2 > comcord.state.nameLength)
comcord.state.nameLength = name.length + 2; comcord.state.nameLength = name.length + 2;
@ -263,14 +273,25 @@ function formatMessage({
content.endsWith("*")) || content.endsWith("*")) ||
(content.startsWith("_") && content.endsWith("_")) (content.startsWith("_") && content.endsWith("_"))
) { ) {
const str = `<${name} ${content.substring(1, content.length - 1)}>`;
if (noColor) { if (noColor) {
console.log(`<${name} ${content.substring(1, content.length - 1)}>`); console.log(str);
} else { } else {
console.log( console.log(chalk.bold.green(str));
chalk.bold.green( }
`<${name} ${content.substring(1, content.length - 1)}>` } 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 { } else {
if (noColor) { if (noColor) {
@ -320,15 +341,25 @@ function formatMessage({
} }
function processMessage(msg, options = {}) { 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; 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) { if (msg.time) {
console.log(msg.content); console.log(msg.content);
} else if (msg.content && msg.content.indexOf("\n") > -1) { } else if (msg.content && msg.content.indexOf("\n") > -1) {
if (msg.content.match(REGEX_CODEBLOCK)) { if (msg.content.match(REGEX_CODEBLOCK)) {
formatMessage({ formatMessage({
channel: msg.channel,
name: msg.author.username, name: msg.author.username,
bot: msg.author.bot, bot: msg.author.bot,
content: msg.content.replace( content: msg.content.replace(
@ -338,6 +369,7 @@ function processMessage(msg, options = {}) {
attachments: msg.attachments, attachments: msg.attachments,
stickers: msg.stickerItems, stickers: msg.stickerItems,
reply: msg.referencedMessage, reply: msg.referencedMessage,
timestamp: msg.timestamp,
dump: true, dump: true,
...options, ...options,
}); });
@ -346,6 +378,7 @@ function processMessage(msg, options = {}) {
for (const index in lines) { for (const index in lines) {
const line = lines[index]; const line = lines[index];
formatMessage({ formatMessage({
channel: msg.channel,
name: msg.author.username, name: msg.author.username,
bot: msg.author.bot, bot: msg.author.bot,
content: content:
@ -356,18 +389,21 @@ function processMessage(msg, options = {}) {
attachments: index == lines.length - 1 ? msg.attachments : [], attachments: index == lines.length - 1 ? msg.attachments : [],
stickers: index == lines.length - 1 ? msg.stickerItems : [], stickers: index == lines.length - 1 ? msg.stickerItems : [],
reply: index == 0 ? msg.referencedMessage : null, reply: index == 0 ? msg.referencedMessage : null,
timestamp: msg.timestamp,
...options, ...options,
}); });
} }
} }
} else { } else {
formatMessage({ formatMessage({
channel: msg.channel,
name: msg.author.username, name: msg.author.username,
bot: msg.author.bot, bot: msg.author.bot,
content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""), content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""),
attachments: msg.attachments, attachments: msg.attachments,
stickers: msg.stickerItems, stickers: msg.stickerItems,
reply: msg.referencedMessage, reply: msg.referencedMessage,
timestamp: msg.timestamp,
...options, ...options,
}); });
} }