Getting a bot to mention the person who initiated the command [duplicate] - discord.js

This question already has answers here:
How to ping a user with their user id in discord.js?
(2 answers)
Closed 8 months ago.
I’ve tried following a few other tutorials but I keep getting something wrong. I’m trying to have the bot ping the person who initiated the command like this.
User: Ping
Bot: #user Pong
I wrote this but when i execute the command it comes out as ${msg.author} Pong.
client.on("message", msg => {
if (msg.content === ".p1") {
msg.channel.send('${msg.author} pong');
I also tried this
client.on("message", msg => {
if (msg.content === ".p1") {
msg.channel.send(msg.author + “Pong”)
When i did this second one, the discord ID of the person initiating the command showed up but was not pinged
Ex: [string of numbers] pong.
Any help is appreciated.

msg.author is enough, you just need to change "" to backqoute
client.on("message", msg => {
if (msg.content === ".p1") {
msg.channel.send('${msg.author} pong'); // must be `${msg.author} pong`
}
If you only use quote or double quote you only passing a string that's why your mention is not working.

Related

discord.js send message on specific channel

I want to send a message on a specific channel. but the thing is my command should be like this +post CHANNELID message then it will post the message on this channel that I've put the id.
so can anyone help me to do that??
example command I want
This is pretty simple actually. There are a few things you need to do.
Separate the arguments
Find channel with first argument (id)
Send message to the channel with content of other arguments joined with spaces
This is a working example of your command
client.on("message", msg => {
const args = msg.content.split(' ');
const [, chanId, ...message] = args;
const channel = msg.guild.channels.resolve(chanId);
channel.send(message.join(" "))
})

Redirect to a channel as well as ping a role then post a message using args

I'm making an announcement command for my bot and want it to go to the announcements channel automatically, then ping the first role mentioned and finally display the message.
For now I have it going to staff commands as a placeholder to get it working, this does not work or throw an error. It properly pings the role, but nothing after that shows up, even the "Debug". What am I missing. I've seen the channel part work but it doesn't for me, and I can't find a similar situation online for the message itself.
module.exports.run = async (client, message, args) => {
if (message.author.bot) return;
let prefix = config.prefix;
if(!message.content.startsWith(prefix)) return;
const channel = message.guild.channels.cache.find(c => c.name === '🚔|staff-cmds');
let pingRole = message.mentions.roles.first();
let messageContent = args.slice(1).join(' ');
channel.send(pingRole, messageContent, "Debug");
This is what happens when the command is run
Try this channel.send(`${pingRole} ${messageContent}`)
The channel.send() function takes only two parameter "content" and "options" you can read more about it here

How can I make a discord bot send a certain message in any channel

so I'm trying to make a discord bot (javascript) to ping my friend every time he talks. What I've tried so far are things like this from different websites:
client.on('message', msg => {
if (msg.author.id === '<#ID>') {
msg.channel.send('<#ID>')
}})
I've searched around and I can't find anything that works exept the msg.reply which I don't want to use because it pings the author. If anyone could help me that would be great, thanks

How do I check if an invite link is valid or not on Discord.js? [duplicate]

This question already has an answer here:
Testing if Discord invite link is invalid?
(1 answer)
Closed 2 years ago.
How do I make the bot check if the invite link posted on a channel (a lot of channels) is valid or not? I've searched around here, google, github and discord servers but I wasn't able to get an answer.. I'm coding the bot with Discord.js, and any help would be appreciated!
As suggested in the comments, you should use the invite endpoint. You can use your preferred method to create a GET request, but here's an example with node-fetch:
const fetch = require('node-fetch');
const code = 'this-is-invalid';
fetch(`https://discordapp.com/api/invite/${code}`)
.then((res) => res.json())
.then((json) => {
if (json.message === 'Unknown Invite') {
// the invite is invalid
} else {
// the invite is valid
}
});
Here's what the result object looks like

how do i store a user in a variable in discord.js

so, I am running a boy scout server and for it, I am making a bot. we have been struggling to get everyone's real name for better organization. the bot will ask for their name and they will type !name Travis_c. then I want it to store their discord username in a variable and send me a message saying trullycool => Travis_C. with that information, it would be super simple to change their nickname on the server. I am running into issues with getting the user who sent the previous message, into a variable.
my code is
`
case 'name':
name = args[1];
const channel = message.guild.channels.cache.find(channel => channel.name === "names");
user = message.author()
if(!channel) return;
message.channel.send(`${user} => ${name}`)
`
I am having issues on user = message.author()
I know if I want to reply to something I do message.reply but that wouldn't work in my situation. thanks in advance!
message.author is not a function so there is no need for () at the end of it.

Resources