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 {listUsers} = require("./listUsers");
function startDM(user) {
function startDM(channel) {
startPrompt(":msg> ", async function (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 {
try {
const channel = await comcord.client.getDMChannel(user.id);
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) {
console.log(`\n<failed to send message: ${err.message}>`);
}
@ -43,7 +48,9 @@ addCommand("s", "send private", function () {
addCommand("a", "answer a send", function () {
if (comcord.state.lastDM) {
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);
} else {

View File

@ -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);
});

View File

@ -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;
}
});

View File

@ -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,
});
}