Skip to content

Commit

Permalink
adopt matchAll
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed Jan 7, 2022
1 parent 97390bd commit faa7bbe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import readenv from "@cm-ayf/readenv";
import dotenv from "dotenv";
import readenv from '@cm-ayf/readenv';
import dotenv from 'dotenv';

dotenv.config();

Expand Down
55 changes: 36 additions & 19 deletions src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@ import {
Message,
MessageEmbed,
Permissions,
} from "discord.js";
} from 'discord.js';

function isProperMatch(match: RegExpMatchArray): match is {
groups: {
guild: string;
channel: string;
message: string;
};
} & RegExpMatchArray {
return (
!!match.groups &&
!!match.groups.guild &&
!!match.groups.channel &&
!!match.groups.message
);
}

export default function expand(client: Client<true>, message: Message) {
async function getMessage(
url: string
match: RegExpMatchArray
): Promise<[GuildTextBasedChannel, Message]> {
let ids = url.split("/").slice(4);
if (ids[0] != message.guild?.id)
if (!isProperMatch(match)) throw new Error('not proper match');
if (match.groups.guild != message.guild?.id)
throw new Error(
`\`${url}\`\nis not from this server. I could not expand it.`
`\`${match[0]}\`\nis not from this server. I could not expand it.`
);

let channel = await message.guild.channels.fetch(ids[1]);
if (!channel) throw new Error(`channel with id ${ids[1]} not found.`);
let channel = await message.guild.channels.fetch(match.groups.channel);
if (!channel)
throw new Error(`channel <#${match.groups.channel}> not found.`);

if (!(channel.isText() || channel.isThread()))
throw new Error(`\`${channel}\` is not a text channel.`);
Expand All @@ -27,10 +43,10 @@ export default function expand(client: Client<true>, message: Message) {
?.has(Permissions.FLAGS.READ_MESSAGE_HISTORY);
if (!allowed)
throw new Error(
`I didn't have permission to see \n\`${url}\`.\nI could not expand it.`
`I didn't have permission to see \n\`${match[0]}\`.\nI could not expand it.`
);

return [channel, await channel.messages.fetch(ids[2])];
return [channel, await channel.messages.fetch(match.groups.message)];
}

function createEmbeds([
Expand All @@ -46,18 +62,19 @@ export default function expand(client: Client<true>, message: Message) {
embeds.length > 0
? `\n(${embeds.length} ${
embeds.length == 1
? "embed follows."
: "embeds follow."
? 'embed follows.'
: 'embeds follow.'
})`
: ""
: ''
}`,
timestamp: createdAt,
footer: {
text: channel.parent?.parent
? `${channel.parent.parent.name} > `
: "" + channel.parent
? `${channel.parent?.name} > `
: "" + channel.name,
text:
(channel.parent?.parent
? `${channel.parent.parent.name} > `
: '') +
(channel.parent ? `${channel.parent?.name} > ` : '') +
channel.name,
icon_url: message.guild?.iconURL() ?? undefined,
},
image: {
Expand All @@ -68,8 +85,8 @@ export default function expand(client: Client<true>, message: Message) {
return [embed, ...embeds];
}

return (url: string) =>
getMessage(url)
return (match: RegExpMatchArray) =>
getMessage(match)
.then(createEmbeds)
.then((embeds) => message.channel.send({ embeds }))
.catch((error) => message.channel.send(`${error as Error}`))
Expand Down
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { Client, Intents } from "discord.js";
import env from "./env";
import expand from "./expand";
import { Client, Intents } from 'discord.js';
import env from './env';
import expand from './expand';

const { BOT_TOKEN } = env;

const client = new Client({
intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS],
});

const regurl = /https:\/\/discord(app)?.com\/channels(\/\d{18}){3}/g;
const regurl =
/https:\/\/(ptb\.|canary\.)?discord(app)?\.com\/channels\/(?<guild>\d{18})\/(?<channel>\d{18})\/(?<message>\d{18})/g;

client.on("ready", (client) => {
client.on('ready', (client) => {
console.log(`Logged in as ${client.user.tag}!`);
});

client.on("messageCreate", (message) => {
client.on('messageCreate', (message) => {
if (message.author.bot) return;
let urls = message.content.match(regurl);
let urls = [...message.content.matchAll(regurl)];
if (!urls) return;

urls.forEach(expand(client, message));
});

client.on("threadCreate", (channel) => {
client.on('threadCreate', (channel) => {
channel.join().catch(console.error);
});

Expand Down

0 comments on commit faa7bbe

Please sign in to comment.