I'm trying to write a command that messages a specified user. The user can type something like /warn #user insert warning here. Here is the code:
const user = await client.users
.fetch(interaction.options.getString("user"))
.catch(console.error);
const embed = new MessageEmbed()
.setColor("#FCE100")
.setTitle(`⚠️ Warning!`)
.setDescription(`${interaction.options.getString("warning")}`);
await user.send({ embeds: embed }).catch(() => {
interaction.channel.send("Error: user not found");
});
Here's the error I'm getting:
user_id: Value "<#!9872345978#####>" is not snowflake.
How do I get the correct "snowflake" value to actually be able to DM the user?
You have to use getUser instead of getString and also change your user option type to "USER" to make it work!
Related
I want to send a message to a specific channel but it doesn't work, I've tried
client.guilds.cache.get("<id>").send("<msg>")
but I get the error ".send is not a function".
What am I missing here?
There are three ways of sending message to a specific channel.
1. Using a fetch method
const channel = await <client>.channels.fetch('channelID')
channel.send({content: "Example Message"})
2. Using a get method
const channel = await <client>.channels.cache.get('channelID')
channel.send({content: "Example Message"})
3. Using a find method
a.
const channel = await <client>.channels.cache.find(channel => channel.id === "channelID")
channel.send({content: "Example Message"})
b.
const channel = await <client>.channels.cache.find(channel => channel.name === "channelName")
channel.send({content: "Example Message})
Your current code line is getting the GUILD_ID not the CHANNEL_ID.
You are trying to get from the cache a Guild not a Channel, or more specifically a TextChannel, that's why what you try is not working, you should try to change the code to something like this and it should work.
const channel = client.channels.cache.get('Channel Id');
channel.send({ content: 'This is a message' });
Also, as something extra, I would recommend using the fetch method for these things, since it checks the bot's cache and in the case of not finding the requested object it sends a request to the api, but it depends on the final use if you need it or not.
const channel = await client.channels.fetch('Channel Id');
channel.send({ content: 'This is a message' });
i like to save my sended DM to a user in a local variable. How can I do this?
I would like to react on that message, but if I use .then() after or before the required .catch()-block, the log says "Cannot send messages to this user", without it, I can send the message..
Thank you!!
Code:
client.users.cache
.get(memb.id)
.send(emb5)
.catch((msb) => {
write("Error");
})
.then((msg) => {
msg.react(one);
});
one is the emoji...
If you are in an async function you can put the message in a variable like this:
const user = await client.users.fetch(mem.id); // Fetching the user
const awaitedMessage = await user.send(emb5); // Send the message to the user
awaitedMessage.react(one); // React the sent message
How to make a bot be able to send a private message to a specific person? (using user ID)
no, message.author is not what I need!
I'm using discord.js and node.js
client.users.fetch('123456789').then((user) => {
user.send("My Message");
});
Dont forget replace "123456789" with the user id :D
Make sure this code is inside of an async function
// Getting the user object
const user = await <Client>.users.fetch('userID');
if (user) user.send('message').catch(error => {
// code if error occurs
});
This can be done with the User.send() method.
Here's an example of how it can be done by fetching the user
Make sure this is an async function, or the await will return an error.
const user = await <client>.users.fetch("USER ID"); // Replace <client> with your client variable (usually client or bot)
user.send("Message to send");
so what i am trying to do is detect when a bot sends an embed in a channel, and when it does detect this, to take that embed and resend it the same as it was sent as.
For example, if the bot detects an embed sent in one channel, it will send that exact same embed in another channel. But the reason for this is because I want to take the embeds from multiple bots.
in discordjs.guide it says to use this code:
const receivedEmbed = message.embeds[0];
const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');
channel.send(exampleEmbed);
but this has not worked for me
You need to replace channel in the line channel.send(exampleEmbed); with an actual reference to a channel. Since you will be using the message event handler, you can get the channel the message was sent in using message.channel.
I have also added in a check to ensure that the message was sent by a bot and contains an embed.
client.on('message', message => {
// check to ensure message was sent by bot and contains embed
if (!message.author.bot || !message.embeds[0]) return;
const receivedEmbed = message.embeds[0];
const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');
// send in same channel
message.channel.send(exampleEmbed);
// send in different channel
client.channels.fetch(/* Channel ID */).then(channel => {
channel.send(exampleEmbed);
});
// alternatively, you can use this (but the function must be asynchronous)
const channel = await client.channels.fetch(/* Channel ID */);
channel.send(exampleEmbed);
});
For more information on valid properties and methods, read the Discord.js docs.
The following code will check if the message has any embeds and will resend the first one.
if (message.embeds) {
message.channel.send({ embed: message.embeds.first() || message.embeds[0] })
};
const embed = message.embeds[0];
const editedEmbed = embed
.setTitle('Edited!')
.addField('Test Field!', 'This is a test', true);
message.channel.send(editedEmbed);
This worked perfectly fine for me.
The problem will be that you don't have a TextChannel selected. (message.channel)
So, this works ALMOST as intended. I have the bot private messaging the user who uses the correct command in the correct channel of the server. Instead of clogging up the server with profile creation messages, I have the bot do profile creation privately. I know/think the problem is with the message.channel.awaitMessages, because it only sees the replies in the original channel on the server where the command was put into place.
I am not sure what I should replace the message.channel.awaitMessages with in order for it to pick up on the reply in the private message conversation rather than the original profile creation channel on the server.
I have not tested the sql message yet. I haven't tested what I have but I am pretty sure it won't work. I want the reply that is given by the user in the private message to be inserted (updated maybe?) into a mysql database I have already set up and properly got working. The users ID and username has been put into this table with the rest of the profile related questions being null at this moment. As they reply to these questions, those fields can be filled out/updated. Any ideas/suggestions are welcome!
module.exports.run = async (bot, message, args) => {
if(message.channel.id === '460633878244229120') return message.author.send(`Greetings, ${message.author.username}! FIRST QUESTION, **What is the name of your Brawler or Character?**`)
.then(function(){
message.channel.awaitMessages(response => message.content, {
max: 1,
time: 5000,
errors: ['time'],
})
.then((collected) => {
message.author.send(`Your Name is: ${collected.first().content}`);
var sql = (`UPDATE profile SET name = '${message.content}' WHERE id = ${message.author.id}`);
})
.catch(function(){
message.author.send('Please submit a name for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
});
});
}
Thanks in advance for your time!
I don't use SQL so, since the question is not specifically on that, don't ask me.
When you're using message.channel.awaitMessages, message is still the first one that you passed in the module.exports.run function, not the one that comes from send(). When send is resolved, it passes a new message, that you have to include in the arguments of the function you put inside then: so, instead of .then(function() {}) you'll need something like .then(function(new_message_sent_in_private) {})
This should fix the problem:
module.exports.run = async (bot, message, args) => {
if(message.channel.id === '460633878244229120') return message.author.send(`Greetings, ${message.author.username}! FIRST QUESTION, **What is the name of your Brawler or Character?**`)
.then((newmsg) => { //Now newmsg is the message you sent
newmsg.channel.awaitMessages(response => response.content, {
max: 1,
time: 5000,
errors: ['time'],
}).then((collected) => {
newmsg.channel.send(`Your Name is: ${collected.first().content}`);
var sql = (`UPDATE profile SET name = '${message.content}' WHERE id = ${message.author.id}`);
}).catch(() => {
newmsg.channel.send('Please submit a name for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
});
});
}
Let me now if that worked :)