discorddiscord.js

How to make link markup work in a discord bot message?


What I'm trying to achieve is simple:

In a ephemeral follow up, it works as expected:

const content = "links: [link1](https://example.com/1), [link1](https://example.com/2)"
await interaction.followUp({
    ephemeral: true,
    content,
})

But when I send this to a public channel like this:

await channel.send(content)

I'm getting it as plain text (links: [link1](https://example.com/1), [link1](https://example.com/2)) except the links are clickable.

like this

Is it possible to get the same result as in an ephemeral message?

I've checked the permissions, there's only "embed" links (which sounds like "allow links in embeds", and not "allow links in messages) and it is enabled anyway on server for everyone, for the bot and in the channel. So what am I missing here?

PS Here they say that "it's only possible if the message was sent with webhook though", but I'm not quite sure what does this mean (can this be different for a public and an ephemeral message?)


Solution

  • Right, so I've found a solution based on suggestion by Krypton. As an embed doesn't look nice and manual creation of a webhook is not an option for me, I've found that I can create a webhook on the go and use it.

    After some testing, I've figured that such webhooks are also permanent (although they can be found in another part of settings than the manually created ones); they should be deleted as there's a limit of 15 webhooks – an attempt to create more fails (with DiscordAPIError[30007]: Maximum number of webhooks reached (15)).

    Instead of doing

    await channel.send(content)
    

    I've put

    // a guard, TypeScript requires us to be sure
    if(channel instanceof TextChannel) {
        const webhook = await channel.createWebhook({
            // a message from a webhook has a different sender, here we set its name,
            // for instance the same as the name of our bot (likewise, we can set an avatar)
            name: "MyBot",
        })
        await webhook.send(content)
        await webhook.delete()
    }
    

    This seems to be a minimal code change to get the links working.