How to make a bot send DMs to a specific person? - javascript

I'm trying to make a VA training section for my bot, it's supposed to send me the training type they've requested. But I've hit a wall and have no idea where to go from here.
I've looked online for a solution but haven't found any that work for me
if (cmd === `${prefix}request`) {
let args = messageArray.slice(1);
let sUser = user.get('My_ID_Goes_Here');
message.sUser.send(args)
}
It is supposed to send the message argument to me, but it give me
Reference Error: user is not defined

It looks like you're trying to send a DM to yourself using the bot. In this case, the Client can do exactly that.
The Client has a property named users. This property contains all the User objects that your bot has cached (a.k.a., has interacted with.) The users property is a collection containing User objects, mapped by their IDs.
If you want to get your User, simply get the User from the collection that has your ID. Once you have your User object, you can send a DM to yourself using the send method.
// assuming "client" is your bot client
var me = client.users.get(MY_ID_HERE);
me.send("args");
Therefore, your code should be:
if(cmd === `${prefix}request`) {
let args = messageArray.slice(1);
let sUser = client.users.get('Your_ID_goes_here');
sUser.send(args)
}
You can also modify this to send direct messages to anyone, as long as you use their ID instead of yours.

if (message.content.startsWith(prefix + "training-P1")) {
let trainingstudent = message.mentions.users.first();
let mentuor = message.mentions.users.second();
mentour.send(trainingstudent "has requested P1 training");
}
That should work.

Related

How to send a dm to another person discord.js? (javascript)

I'm making a discord bot and I wrote this code, but it gives an error, user.dm is not a function.
My code:
if(message.content.startsWith('s!modöner')) {
let mesajöner = args.slice(1).join(' ');
let userdm = 767387717792563241
userdm.send(mesajöner);
message.reply('Quixe Öneriniz Gönderildi.');
}
I assume that the userdm variable is a user id, and you want to DM that user. In that case, you would first have to get the user from client.users.cache, then you can send it. An example:
const user = client.users.cache.get('767387717792563241')
user.send(mesajöner)
(Note: In discord, all ids are Strings, so any id you use should be used as a string instead of a number like you have)

How do I create server specific variables in Discord.js?

I made a slash command that uses a channel selection. I'm using this to set the logging channel. I need help to have the same variable but with different values per-server.
For example:
Server 1 = #logs
Server 2 = #mod-only
I don't know how to get variables to have different values in different servers.
Right now I have this. but if you ban someone in "Server 2", it will log to "Server 1."
logchannel.js
var logchannel = interaction.options.getChannel('channel');
exports.logchannel = logchannel;
ban.js
var logchannel = require('./logchannel');
var logchannel = logchannel.logchannel;
logchannel.send('message');
I need to figure out how to get the log channel variable to have different values throughout different servers. If anyone could answer how to do this please let me know.
I also already tried using MongoDB and it did not work for me.
Here's one solution without using mongo but you can achieve the same with mongo. There are many approaches you can take to achieve the same it's a basic one and not the best.
const logChannels = {
'serverID': 'channelID',
'anotherserverID': 'anotherchannelID',
}
var logchannel = logChannels[guild.id] // Here you will have your log channel ID
guild.channels.cache.get(logchannel).send(`message`)

Discord JS - Reverse lookup to obtain userID from their display name

We're currently building a bot that will review specific messages and perform follow-up administrative actions on it. One of the actions will read a message and extract a named user (string - not link), search for it and return the ID of the user - which is then used elsewhere.
if (message.channel.id === register) {
// The post is in the register channel
if (message.content.includes('doing business with you')) {
//it's a successful claim
Rclaimedby = message.content.match(/\*\*(.*?)\*\*/)[1];
const Ruser = client.users.cache.find(user => user.username == Rclaimedby).id;
The text snippet will just have the individuals name as a string, which the regex will extract. If the individual doesn't have a nickname set, it'll work and the rest of the code works. However, as soon as that name isn't their "normal" name, it doesn't work and crashes with a TypeError.
I need to be able to use that stripped name and find the ID regardless of whether they have a nickname or not, but I've been searching for days (and a LOT of trial and error) and can't find a way around it.
To help, the message content would say something like:
(...) doing business with you normalname (...) or
(...) doing business with you randomnickname (...)
Help?
Like you mentioned in your comment, the key difference between extracting a user's ID from their displayName would mean you would have to get the GuildMember object first (it's server-specific) and then get the user property from that.
All in all, it just requires a little bit of re-maneuvering around the variables in order to get what you want since the displayName property is specific to each server.
Process:
Get the server ID
From the server ID, get the server
Get the members property from the server
Find the member by matching it with the given randomnickname, which I've set as displayNickname in this case.
Get the user property from the GuildMember object and grab the ID from there.
Code:
This is merely something you can work off of.
if (command === "memberinfo") {
let serverID = "800743801526157383"; //server id; manually input your own
let displayNickname = args[0];
message.channel.send(`Sent Nickname: ${displayNickname}`);
let serverMembers = client.guilds.cache.get(serverID).members //defines server and returns members
let matchedMember = serverMembers.cache.find(m => m.displayName === displayNickname);
message.channel.send(`Matched Member ${matchedMember.user.username}.`)
console.log(`The user's ID: ${matchedMember.user.id}`);
}
Resources:
SO Question - Fetching user from displayName
Discord.js Documentation

check if an user is in a specific guild discord.js

I don't know if it's possible, but I'm trying to make my bot returning all the servers it shares with an user.
So I got all the ids from the guilds' where the bot is, and the id from the user.
And I don't know how to check for each guild if the user's id is on it or not.
I tried to do this:
let user_id = message.author.id;
let guild_list = [];
client.guilds.cache.forEach(guild => {
guild_list.push(guild.id);
})
for (let i = 0; i < guild_list.length; i++){
let thisGuild = client.guilds.cache.get(guild_list[i]);
if(thisGuild.member(user_id)){
message.channel.send("Shared server : " + guild_list[i]);
}
}
But the Guildmember class only checks the user's id on the server where the command is run.
I don't know if it's possible, and I don't find anything that could help me in the documentation.
It is very much possible...
We can use the Collector#filter method for this since we would want to filter the guild(s) the bot and the author is.
const mutualGuilds = client.guilds.cache.filter((guild) => {
return guild.members.cache.has(message.author.id);
});
// This will log the mutual guild(s) the bot and the author is in, of course you can change it however you'd like.
console.log(mutualGuilds);
It is as easy as this.

Add role to user from DMs?

So I'm making a verification bot for one of my Discord Servers, and I've come across a problem.
Purpose:
When the user first joins, they will be only allowed to talk in one specific channel, the verification channel. When they type $verify {username on-site}, the bot will reply in-server telling the user to check their DMs to continue. A unique string linked to a verification row in the database will be given, and the discord user will be told to change their status on-site to the given unique ID. Once they do that, they will be given the "Verified" role and will be able to speak on the discord.
The problem:
When everything goes right, and the user is ready to be given the role on the discord server, the bot will not give the role. I'm assuming this is because the user that the bot is trying give the role to is not linked to the actual discord server for it, because it's in DMs.
What I'm looking for:
If possible, give me an example on how to give a role to a user on a discord server from DMs, or documentation. That would be really helpful!
Code: I'll only include the part that is important, there is no other errors.
snekfetch.get("https://vertineer.com/api/getUniq.php?discord_id="+message.author.id).then(function(r){
console.log("here 2");
let uniq = JSON.parse(r.text);
if(uniq.id == uniq.status){
message.member.addRole("Verified");
message.reply("Success!");
} else {
message.reply("Looks like you didn't change your status to `"+uniq.id+"`, try again with `$finish`.");
}
});
If this is only to work for one server you can do:
client.guilds.get('myguildID').members.get(message.author.id).addRole('193654001089118208')
And also, to give a role to a user you can't use the name. You need to use the ID or the Role Object.
If you would prefer to use the name you would need to do something like this:
var role = client.guilds.get('myguildID').roles.find(role => role.name === "Verified");
client.guilds.get('myguildID').members.get(message.author.id).addRole(role);
This is what worked for me:
client.guilds.fetch(<guild.id>).then(async(guild) => {
let role = guild.roles.cache.find(r => r.name === <role.name>);
let member = await guild.members.fetch(<member.id>);
member.roles.add(role);
});
Substitute <guild.id>, <member.id> and <role.name> for the desired values respectively

Categories

Resources