Skip to content

Commit

Permalink
✨ implements faux dms
Browse files Browse the repository at this point in the history
  • Loading branch information
laquasicinque committed Feb 14, 2022
1 parent 39c02e3 commit bfc59cc
Show file tree
Hide file tree
Showing 8 changed files with 5,027 additions and 5,182 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": "16.13.0 || ^17.0.0"
},
"scripts": {
"dev": "TS_NODE_FILES=true TS_NODE_PROJECT=\"./tsconfig.json\" nodemon -r dotenv/config -x node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
"dev": "cross-env TS_NODE_FILES=true TS_NODE_PROJECT=\"./tsconfig.json\" nodemon -r dotenv/config -x node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
"start": "node build/index.js",
"build": "tsc",
"lint": "eslint src && tsc --noEmit",
Expand All @@ -26,8 +26,9 @@
"@mdn/browser-compat-data": "4.1.3",
"@sentry/node": "6.16.1",
"compare-versions": "4.1.2",
"discord.js": "13.4.0",
"cross-env": "^7.0.3",
"date-fns": "2.28.0",
"discord.js": "13.4.0",
"dom-parser": "0.1.6",
"domyno": "1.0.1",
"fuse.js": "6.5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/v2/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { filter } from 'domyno';
import { isEqual } from 'lodash-es';

import type { CommandDataWithHandler } from '../../types';
import { commands } from '../modules/modmail';
import { modmailCommands } from '../modules/modmail';
import { asyncCatch } from '../utils/asyncCatch.js';
import { map, mapʹ } from '../utils/map.js';
import { merge } from '../utils/merge.js';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const guildCommands = new Map(
shitpostInteraction,
npmInteraction,
whynoInteraction,
...commands,
...modmailCommands,
// warn // Not used atm
].map(command => [command.name, command])
); // placeholder for now
Expand Down
13 changes: 11 additions & 2 deletions src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { registerCommands } from './commands/index.js';
import pointDecaySystem, { loadLastDecayFromDB } from './helpful_role/point_decay.js';
import { registerMessageContextMenu } from './message_context/index.js';
import { handleDM } from './modules/modmail/index.js';
import { handleDmThread } from './modules/modmail/util/handleDmThread.js';
import { isDmThread } from './modules/modmail/util/isDmThread.js';
import { isModMailThread } from './modules/modmail/util/isModMailThread.js';
import { registerUserContextMenu } from './user_context/index.js';
import { stripMarkdownQuote } from './utils/content_format.js';
import { purge as purgeDMLocks } from './utils/dmLock.js';
Expand Down Expand Up @@ -161,8 +164,14 @@ const handleNonCommandGuildMessages = async (msg: Message) => {
if (msg.author.bot) {
return;
}
if (isWebdevAndWebDesignServer(msg) && isThanksMessage(quoteLessContent)) {
handleThanks(msg);

if (isWebdevAndWebDesignServer(msg)) {
if(isThanksMessage(quoteLessContent)) {
handleThanks(msg);
}
if(isDmThread(msg)) {
handleDmThread(msg)
}
}
await detectDeprecatedCommands(msg);
await detectJustAsk(msg);
Expand Down
2 changes: 1 addition & 1 deletion src/v2/modules/modmail/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import { reportUser } from './reportUser';
import { sendCommand } from './send';


export const commands = [sendCommand, reportMessage, initiateModmail, reportUser, closeCommand ];
export const modmailCommands = [sendCommand, reportMessage, initiateModmail, reportUser, closeCommand];
81 changes: 61 additions & 20 deletions src/v2/modules/modmail/commands/send.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type {
DMChannel,
Guild,
GuildMember,
Message,
MessageOptions,
MessagePayload,
TextChannel,
ThreadChannel} from 'discord.js';
import {
MessageActionRow,
MessageButton,
MessageEmbed
ThreadChannel,
} from 'discord.js';
import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';

import type { CommandDataWithHandler } from '../../../../types';
import { SERVER_ID, MOD_ROLE_ID, DM_ALT_CHANNEL_ID } from '../../../env';
Expand Down Expand Up @@ -104,7 +102,7 @@ export const sendCommand: CommandDataWithHandler = {
],
});
} catch (error) {
console.error(error)
console.error(error);
}
},
onAttach(client) {
Expand All @@ -126,8 +124,12 @@ export const sendCommand: CommandDataWithHandler = {
return;
}

const dmChannel = await member.createDM();
const message = await dmChannel.messages.fetch(msgId);
const message = await getDmMessage({
member,
msgId,
guild: interaction.guild,
});

try {
await message.delete();
if ('edit' in interaction.message) {
Expand Down Expand Up @@ -179,7 +181,7 @@ async function sendFakeDM(
{ upsert: true, new: true }
).exec();

console.log(dmThread)
console.log(dmThread);

const channel = (await guild.channels.fetch(
dmThread.channelId
Expand All @@ -190,20 +192,59 @@ async function sendFakeDM(
thread = await channel.threads.fetch(dmThread.threadId);
} else {
try {
thread = await channel.threads.create({
name: `${dmChannel.recipient.username}_${dmChannel.recipient.discriminator} [${userId}]`,
type: 'GUILD_PRIVATE_THREAD',
});
} catch {
thread = await channel.threads.create({
name: `${dmChannel.recipient.username}_${dmChannel.recipient.discriminator} [${userId}]`,
type: 'GUILD_PUBLIC_THREAD',
});
}
thread = await channel.threads.create({
name: `${dmChannel.recipient.username}_${dmChannel.recipient.discriminator} [${userId}]`,
type: 'GUILD_PRIVATE_THREAD',
});
} catch {
thread = await channel.threads.create({
name: `${dmChannel.recipient.username}_${dmChannel.recipient.discriminator} [${userId}]`,
type: 'GUILD_PUBLIC_THREAD',
});
}

dmThread.updateOne({ threadId: thread.id }).exec();
}

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return thread.send(typeof content === 'string' ? `${dmChannel.recipient} ${content}` : { content: `${dmChannel.recipient} ${(content as {content?: string }).content ?? ''}`, ...content})
return thread.send(
typeof content === 'string'
? `${dmChannel.recipient} ${content}`
: {
content: `${dmChannel.recipient} ${
(content as { content?: string }).content ?? ''
}`,
...content,
}
);
}
async function getDmMessage({
member,
guild,
msgId,
}: {
member: GuildMember;
guild: Guild;
msgId: string;
}): Promise<Message | null> {
const userId = member.user.id;
const dmChannelReplacement = await DMThread.findOne({
guildId: SERVER_ID,
userId,
closedAt: { $exists: false },
}).exec();

console.log(dmChannelReplacement)
if (dmChannelReplacement) {
const channel = (await guild.channels.fetch(
dmChannelReplacement.channelId
)) as TextChannel;

const thread = await channel.threads.fetch(dmChannelReplacement.threadId);
return thread.messages.fetch(msgId);
}

const dmChannel = await member.createDM();

return await dmChannel.messages.fetch(msgId);
}
37 changes: 37 additions & 0 deletions src/v2/modules/modmail/util/handleDmThread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Message} from 'discord.js';
import { MessageEmbed } from 'discord.js';

import { ModMailThread } from '../db/modmail_thread';
import { getModMailThread } from './getModMailThread';

export const handleDmThread = async (msg: Message) => {
const modmailThreadData = await ModMailThread.findOne({
userId: msg.author.id,
guildId: msg.guildId,
closedAt: { $exists: false },
}).exec();

if(!modmailThreadData) {
return
}

const modmailThread = await getModMailThread(msg.guild, msg.author)

if (msg.attachments.size === 0) {
await modmailThread.send({
embeds: [
new MessageEmbed()
.setColor(0xff_96_00)
.setAuthor({
name: `${msg.author.username}#${msg.author.discriminator}`,
iconURL: msg.author.avatarURL(),
})
.setDescription(msg.content)
.setTitle('Message Received')
.setTimestamp(msg.createdTimestamp),
],
});

msg.react("✅")
}
};
24 changes: 24 additions & 0 deletions src/v2/modules/modmail/util/isDmThread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Guild, Message, TextChannel } from 'discord.js';

import { DM_ALT_CHANNEL_ID } from '../../../env';
import { DMThread } from '../db/dm_thread';
import { cache } from "./cache";

export const isDmThread = async (msg: Message): Promise<boolean> => {
if(!msg.channel.isThread()) {
return false
}

if (msg.channel.parent?.id !== DM_ALT_CHANNEL_ID) {
return false
}

const dmThread = await DMThread.findOne({
guildId: msg.guild,
threadId: msg.channelId,
channelId: msg.channel.parent.id,
closedAt: { $exists: false }
}).exec()

return !!dmThread
};
Loading

0 comments on commit bfc59cc

Please sign in to comment.