Kingamezz
Kingamezz

Reputation: 25

How can I grab the message link of a sent message in discordjs?

I'm trying to work on a more detailed logging system for my discord bot, and this is what I currently have:

client.on('message', message => {

    if (message.author.bot) return; //don't log its own messages

    const channel = client.channels.cache.get('805457298867879936'); //sent logs to this channel
    channel.send(
        `${message.author} in ${message.channel} of ${message.guild} said ${message.content}`, //log in discord channel
    );
    
    console.log(
        `${message.author.tag} in #${message.channel.name} of ${message.guild.name} said: ${message.content}`, //log in console
    );

client.login(token); //login

This code logs any messages sent that the bot can see in a specific channel and my console. However, I'd like to also have a message link to the sent message. For example, this is what I currently have logged: Kingamezz#02XX in #testing of Testing Server said: test And what I'd like to have is this: Kingamezz#02XX in #testing of Testing Server said: test (https://discord.com/channels/763786268181397524/805457298867879936/805502804134330448) so I can jump straight to the message.

I don't know how to grab the message link of a message, the discordjs documentation doesn't seem to have anything about message links. I've tried this:

channel.send(
    "https://discord.com/channels/" + guild.id + "/" + message.id
)

but it results in "guild is not defined". Am I going at this the right way?

Upvotes: 0

Views: 1844

Answers (1)

Axiumin_
Axiumin_

Reputation: 2145

You can use the .url property of Message to get a url you can jump to, like this:

channel.send(message.url);

Documentation: https://discord.js.org/#/docs/main/master/class/Message?scrollTo=url

In addition, the reason your current code isn't working is because there is no guild object, only a message object. Therefore, you'd first have to get the guild from the message object in order to access the guild properties. So, if you wanted to stick with your current code, you could also do this:

channel.send(
    "https://discord.com/channels/" + message.guild.id + "/" + message.guild.id
)

However, one thing to note with the second solution is that if the message was a direct message, it would likely error out since the message was not sent in a guild.

Upvotes: 1

Related Questions