Skip to content

Instantly share code, notes, and snippets.

@lze3
Last active January 18, 2021 01:43
Show Gist options
  • Save lze3/dcad6dd90475e4fce162262f3e701b32 to your computer and use it in GitHub Desktop.
Save lze3/dcad6dd90475e4fce162262f3e701b32 to your computer and use it in GitHub Desktop.
Discord Message Referencing [Discord.js]
const urlRegex = /https:\/\/((canary|ptb).)?discordapp.com\/channels\/(\d{18})\/(\d{18})\/(\d{18})/g;
/**
* All parameters for return of a request for a message.
* Based on [Discord API Documentation](https://discord.com/developers/docs/resources/channel#message-object-message-structure).
*/
interface IMessageStruct {
id: Snowflake;
channel_id: Snowflake;
guild_id?: Snowflake;
author: {
id: Snowflake;
username: string;
avatar: string;
discriminator: string;
public_flags: number;
};
type: number;
content: string;
attachments: {
id: Snowflake;
filename: string;
size: number;
url: string;
proxy_url: string;
width: number;
height: number;
}[];
embeds: {
title?: string;
type?: string;
description?: string;
url?: string;
timestamp?: Date;
color?: number;
footer?: {
text: string;
icon_url?: string;
proxy_icon_url?: string;
};
image?: {
url?: string;
proxy_url?: string;
height?: number;
width?: number;
};
thumbnail?: {
url?: string;
proxy_url?: string;
height?: number;
width?: number;
};
video?: {
id: Snowflake;
filename: string;
size: number;
url: string;
proxy_url: string;
height?: number;
widgh?: number;
};
provider?: {
name?: string;
url?: string;
};
author?: {
name?: string;
url?: string;
icon_url?: string;
proxy_icon_url?: string;
};
fields?: {
name: string;
value: string;
inline?: boolean;
}[];
}[];
mentions: string[];
mention_roles: string[];
pinned: boolean;
mention_everyone: boolean;
tts: boolean;
timestamp: Date;
edited_timestamp: Date | null;
flags: number;
reactions: {
emoji: {
id: Snowflake;
name: string;
};
count: number;
me: boolean;
}[];
}
client.on('message', async (message) => {
const isDm = message.channel.type === 'dm';
if (!isDm) {
const strSplit = message.content.split(urlRegex);
// message referencing
if (strSplit.length < 8 && strSplit.length > 5) {
let count = 3;
const guildId = strSplit[count++];
const channelId = strSplit[count++];
const messageId = strSplit[count++];
const msg = await fetch(`https://discordapp.com/api/channels/${channelId}/messages/${messageId}`, {
headers: {
Authorization: `Bot ${client.token}`
}
});
// no message found :(
if (msg.status === 404) {
return;
}
const result: IMessageStruct = await msg.json();
// if channel is not found in this guild, return!
if (!message.guild.channels.cache.find(ch => ch.id === result.channel_id)) {
return;
}
const embed = new MessageEmbed()
.setAuthor(`${result.author.username}#${result.author.discriminator} | User ${message.author.username} referenced a ${topic}`, `https://cdn.discordapp.com/avatars/${result.author.id}/${result.author.avatar}.webp`)
.setTimestamp(result.timestamp)
.setColor(color);
const messageUrl = `https://discordapp.com/channels/${guildId}/${channelId}/${messageId}`;
if (result.content.length) {
embed.setDescription(`${result.content}\n\n[Jump to Message](${messageUrl})`);
} else if (result.embeds.length && result.embeds[0].description) {
embed.setDescription(`${result.embeds[0].description} \n\n\`[..summary..]\`\n\n[Full Message](${messageUrl})`);
}
embed.setFooter('Discord', 'https://i.imgur.com/7hUUou6.png');
if (result.attachments.length) {
const fileExtension = result.attachments[0].url.substring(result.attachments[0].url.length - 3);
const allowedExtensions = [
'png',
'jpg',
'jpeg',
'webp'
];
if (allowedExtensions.find(i => i === fileExtension)) {
embed.setImage(result.attachments[0].url);
} else {
embed.addField('Attachment', result.attachments[0].url);
}
}
message.channel.send(embed)
.catch(e => {
console.log(e);
});
}
}
});
@lze3
Copy link
Author

lze3 commented Oct 3, 2020

I see with recent client changes, this has became redundant.

@lze3
Copy link
Author

lze3 commented Jan 18, 2021

I see with recent client changes, this has became redundant.

Well, not really. It's still useful for seeing message content of a referenced message w/o having to redirect to the original message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment