discord.js v13 slash commads everyone ping not notice - discord.js

I ping everyone with the slash command, but the #everyone mark is visible, but there is no notification.
Here is my code.
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: '#everyone',
embeds: [embed]
}
}
});
I'm Korean, so I wrote a translator.

The reason #everyone doesn't actually ping from a Slash Command is because they use webhooks and webhooks are not allowed to ping #everyone regardless of your permission setup, so it's not possible to ping in slash command
However there is a workaround
you can use CommandInteraction#reply method to reply an ephemeral response that will only be visible to the person who executed the command
and after that use TextChannel#send method to normally send the message with ping and to get the channel use CommandInteraction#channel
This will ping #everyone provided your bot has permissions to ping and you are not disabling it in the ClientOptions or the message payload

Related

How to (and is it possible to) make link markup work in a discord bot message?

What I'm trying to achieve is simple:
send a message like links: [link1](https://example.com/1), [link1](https://example.com/2) via a bot
get it displayed as "links: link1, link1"
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.
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?)
You cannot use hyper links in normal messages sent by a bot. You need to use a Webhook. Considering you're using discord.js, see this guide on their documentation. When using something like that it will work as expected
const { WebhookClient } = require('discord.js');
const webhookClient = new WebhookClient({ url: "WEBHOOK_URL" });
webhookClient.send({
content: "[Hello world](https://github.com)",
username: "Webhook Username",
});
Otherwise you may use embeds and send one of these with your bot and the content you wish to have.
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.

can my bot reply with a message when pinged only

when I ping my discord bot it shows what I made it say, but when someone replies to the bot it still sends the message, I want it to reply only when pinged.
You could add this into your messageCreateevent:
if (message.mentions.has(client.user.id)) return message.channel.send({ content: `My Prefix is ${prefix}` });
You should define your prefix first so it wont get called as undefined and for the bot to show your prefix.
For more information about Discord.js mention: discord.js documentation

Discordjs: Specifying channel for an interaction.reply

I am using Discordjs v13. I have created a slash command and I am able to "print" a message using
await interaction.reply(messageObj);
I need to send the reply to a different channel where the command was triggered, is this possible?
Something like:
interaction.setChannel(channelId).reply(...)
OR
interaction.reply({
channel: ....
....
})
What you want is not possible. The Discord API does not allow to specify the channel where the app interaction should be replied in: https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
However if you are concerned with the reply being shown to everyone, you can make the reply ephemeral. If you want to log interactions, you can reply to the interaction then send another message using the solution provided in the comments of your question.
this is actually possible in discordjs v14 and may be in v13 as well. Carl-bot does it with suggestions.but its not an actual reply
use
interaction.guild.channels.cache.get('channel-id').send('message')
this will send a message in a the select channel you may still want to
interaction.reply({Content:'replied in #channel' [embed], ephemeral: true })
so the user knows the reply was redirected . ephemeral: true makes the replay only visible to the user that evoked the interaction.
and if you need the message id for the new message use
const msg = await interaction.guild.channels.cache.get('channel-id').send('message');
to send the message and you can do something like const messageid = msg.id

Discord bot : How to know when a user is pinged in the message and not because it's a reply

The goal :
I am trying to get the bot to send a message when a specific user is pinged in a message.
So I tried with this :
if (message.mentions.has(bot.users.cache.get(userID)))
And it's working fine, if you ping the user, the bot sends a message. Excepted...
The problem :
The bot also react when you simply reply to the user without pinging the user in the message, and I don't want that.
The only way I can see is :
if (message.content.includes(userID))
You should check directly if they were pinged in the message.
const userRegex = new RegExp(`<#!?${userID}>`)
if (message.content.match(userRegex)) {
//user has been mentioned and it was not because of reply
}

How to make my discord bot only work in a chat?

I am using discord.js to implement a bot in the discord. When I use a command in any channel in my server, my bot responds to it, but I would like that my bot only worked if someone was sending the commands inside a private chat with the bot, how do I do that?
If you only want it to work between DMs, do
if (!message.channel.type == `dm`) return;
//other commands
You can check if the message was sent in a certain channel by checking the Message.channel.id property.
client.on("message", message => {
if (message.channel.id !== "ChannelID") return false;
// Execute your commands here
});

Resources