2021-04-01 10:44:44 +00:00
|
|
|
import {client} from "../index";
|
|
|
|
import {TextChannel, APIMessage, MessageEmbed} from "discord.js";
|
2021-04-05 04:35:12 +00:00
|
|
|
import {getPrefix} from "../structures";
|
2021-03-30 08:58:21 +00:00
|
|
|
import {DiscordAPIError} from "discord.js";
|
2021-03-20 12:27:57 +00:00
|
|
|
|
2021-04-01 10:44:44 +00:00
|
|
|
client.on("message", async (message) => {
|
|
|
|
// Only execute if the message is from a user and isn't a command.
|
|
|
|
if (message.content.startsWith(getPrefix(message.guild)) || message.author.bot) return;
|
|
|
|
const messageLink = extractFirstMessageLink(message.content);
|
|
|
|
if (!messageLink) return;
|
|
|
|
const [guildID, channelID, messageID] = messageLink;
|
2021-03-20 12:27:57 +00:00
|
|
|
|
|
|
|
try {
|
2021-03-30 08:58:21 +00:00
|
|
|
const channel = client.guilds.cache.get(guildID)?.channels.cache.get(channelID) as TextChannel;
|
|
|
|
const link_message = await channel.messages.fetch(messageID);
|
2021-03-20 12:27:57 +00:00
|
|
|
|
2021-03-30 08:58:21 +00:00
|
|
|
let rtmsg: string | APIMessage = "";
|
2021-03-20 12:27:57 +00:00
|
|
|
if (link_message.cleanContent) {
|
|
|
|
rtmsg = new APIMessage(message.channel as TextChannel, {
|
|
|
|
content: link_message.cleanContent,
|
2021-03-30 08:58:21 +00:00
|
|
|
disableMentions: "all",
|
2021-03-20 12:27:57 +00:00
|
|
|
files: link_message.attachments.array()
|
2021-03-30 08:58:21 +00:00
|
|
|
});
|
2021-03-20 12:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 08:58:21 +00:00
|
|
|
const embeds = [...link_message.embeds.filter((v) => v.type == "rich"), ...link_message.attachments.values()];
|
2021-03-20 12:27:57 +00:00
|
|
|
|
|
|
|
/// @ts-ignore
|
|
|
|
if (!link_message.cleanContent && embeds.empty) {
|
2021-03-30 08:58:21 +00:00
|
|
|
const Embed = new MessageEmbed().setDescription("🚫 The message is empty.");
|
|
|
|
return message.channel.send(Embed);
|
2021-03-20 12:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const infoEmbed = new MessageEmbed()
|
|
|
|
.setAuthor(
|
|
|
|
link_message.author.username,
|
2021-03-30 08:58:21 +00:00
|
|
|
link_message.author.displayAvatarURL({format: "png", dynamic: true, size: 4096})
|
|
|
|
)
|
2021-03-20 12:27:57 +00:00
|
|
|
.setTimestamp(link_message.createdTimestamp)
|
2021-03-30 08:58:21 +00:00
|
|
|
.setDescription(
|
|
|
|
`${link_message.cleanContent}\n\nSent in **${link_message.guild?.name}** | <#${link_message.channel.id}> ([link](https://discord.com/channels/${guildID}/${channelID}/${messageID}))`
|
|
|
|
);
|
|
|
|
if (link_message.attachments.size !== 0) {
|
|
|
|
const image = link_message.attachments.first();
|
|
|
|
/// @ts-ignore
|
|
|
|
infoEmbed.setImage(image.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
await message.channel.send(infoEmbed);
|
2021-03-20 12:27:57 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof DiscordAPIError) {
|
2021-03-30 08:58:21 +00:00
|
|
|
message.channel.send("I don't have access to this channel, or something else went wrong.");
|
2021-03-20 12:27:57 +00:00
|
|
|
}
|
2021-03-30 08:58:21 +00:00
|
|
|
return console.error(error);
|
2021-03-20 12:27:57 +00:00
|
|
|
}
|
2021-04-01 10:44:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export function extractFirstMessageLink(message: string): [string, string, string] | null {
|
|
|
|
const messageLinkMatch = message.match(
|
|
|
|
/([!<])?https?:\/\/(?:ptb\.|canary\.)?discord(?:app)?\.com\/channels\/(\d{17,19})\/(\d{17,19})\/(\d{17,19})(>)?/
|
|
|
|
);
|
|
|
|
if (messageLinkMatch === null) return null;
|
|
|
|
const [, leftToken, guildID, channelID, messageID, rightToken] = messageLinkMatch;
|
|
|
|
// "!link" and "<link>" will cancel the embed request.
|
|
|
|
if (leftToken === "!" || (leftToken === "<" && rightToken === ">")) return null;
|
|
|
|
else return [guildID, channelID, messageID];
|
2021-03-30 08:58:21 +00:00
|
|
|
}
|