I need to send a DM to a user, and I have a variable that stores a user's ID. I am aware of sending the message to the user using client.users.cache.get, and I'm also aware of fetching the user using client.users.fetch, but these only work if I specifically define the user's ID within the parentheses as a STRING.
client.users.fetch('XXXXXXXXXXXX') would work, but client.users.fetch(userid) using the variable, would not work. Any help?
So it really all depends on your trigger. Here are a few examples, all of which assume you are using Discord v13. And these are just a few examples. If you provide some code that you are trying to use, I can update this answer to give a more specific example.
client.on('messageCreate', async message => {
const member = message.member
member.send({
content: 'Hi there'
})
}
client.on('guildMemberAdd', async member => {
member.send({
content: 'Hi there'
})
}
client.on('messageDelete', async message => {
const member = message.member
member.send({
content: 'Hi there'
})
}
client.on('messageReactionAdd', async (messageReaction, user) => {
const member = messageReaction.message.member
member.send({
content: 'Hi there'
})
})
To send messages based on a variable
const guildID = '098765432109876543'
const memberID = '123456789012345678'
const guild = client.guilds.cache.get(guildID)
guild.members.cache.get(memberID).send({
content: 'Hi there'
})