如何在discordjs中获取已发送消息的消息链接?

时间:2021-01-31 18:49:18

标签: javascript discord

我正在尝试为我的不和谐机器人开发更详细的日志系统,这就是我目前拥有的:

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

此代码记录机器人可以在特定频道和我的控制台中看到的所有已发送消息。但是,我还希望有一个指向已发送消息的消息链接。 例如,这是我目前记录的内容: Kingamezz#02XX in #testing of Testing Server said: test 我想要的是: Kingamezz#02XX in #testing of Testing Server said: test (https://discord.com/channels/763786268181397524/805457298867879936/805502804134330448) 这样我就可以直接跳转到消息。

我不知道如何获取消息的消息链接,discordjs 文档似乎没有关于消息链接的任何内容。我试过这个:

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

但它导致“公会未定义”。我这样做对吗?

1 个答案:

答案 0 :(得分:1)

您可以使用 .urlMessage 属性来获取可以跳转到的网址,如下所示:

channel.send(message.url);

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

此外,您当前的代码不起作用的原因是没有公会对象,只有消息对象。因此,您首先必须从消息对象中获取公会才能访问公会属性。因此,如果您想坚持使用当前的代码,也可以这样做:

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

然而,第二个解决方案需要注意的一点是,如果消息是直接消息,它可能会出错,因为消息不是在公会中发送的。

相关问题