I'm using discord.js for my bot, and I want to use the User object to get the ID from that. However, the docs aren't very specific on how to use that. How do you get an object for a specific user?
My code:
const Discord = require("discord.js"); const client = new Discord.Client(); var kingdoms = []; var ids = []; const Kingdom = function(name, id){ this.name = name; this.aa = 1; this.da = 1; this.attackDate = 0; this.exhausaa = 0; ids.push(id); }; client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); client.user.setGame("k^help"); }); client.on('message', msg => { console.log(msg); console.log(msg.content.trim().indexOf('k^addkingdom')); if (msg.content.trim() == 'k^help') { msg.channel.send({embed: { description: 'Note: All things in brackets must be replaced and are parameters.', fields: [{ name: "k^help", value: "Displays this message." }, { name: "k^addkingdom [name]", value: "Adds a kingdom to the database named [name]. One per person only." }] } }); } if (msg.content.trim().indexOf('k^addkingdom ') == 0) { //if(ids.contains(msg.GuildMember.id)){ // msg.reply("You already have a kingdom!"); //} var content = msg.content; content = content.replace('k^addkingdom ', ''); content.trim(); msg.reply("Kingdom " + content + " added!"); kingdoms.push(new Kingdom(content/*, msg.GuildMember.id*/)); } }); client.login('Hidden for privacy');
Well if you are responding to a message, then you can get the user object by msg.author
. And the user id by msg.author.id
. However if what you search is to get the user object from it's id, like when you have that saved in a database. Then you do it by that client.users.get(id);
, where client is the discord client object. Example if you want to send a message to a user in PM (private mode), then you do it this way
let user = client.users.get(id);
user.send("You need to wake up my friend!");
Another example responding to a message:
client.on('message', (msg) => {
if(msg.content === '!test') {
let userId = msg.author.id;
msg.author.send('hello i\'m here ! Your user Id is ' + userId);
// let user = msg.author; user.send(...
}
});
Another example when sending a message to a new server member at it's first arriving:
client.on("serverNewMember", (server, user) => {
user.send("Welcome to: " + server.name);
});
Same example but using sendMessage (a bonus ^ ^):
client.on("serverNewMember", (server, user) => {
client.sendMessage(user, "Welcome to: " + server.name);
});
To get a specific user on a bot account, use client.fetchUser(id)
. This will return a Discord user object. Otherwise (as on selfbots), and on a channel, use channel object.member( userResolvable ).
我对不和谐不熟悉,但是如果您有用户对象,请阅读链接的api以获取用户的ID即可
user.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.