简体   繁体   English

如何在变量内使用ID

[英]How to use an ID inside a variable

I have been making a user info commmand, and I have just figured out how to get an ID from a mention. 我一直在做用户信息命令,而且我刚刚想出了如何从提及中获取ID。 This means people can mention others and see their information. 这意味着人们可以提及其他人并查看他们的信息。

I have placed the ID inside a variable called userid1 , and swapped message.author in favour of userid1 , it runs numerous errors, and when it does work just displays everything as undefined. 我已经将ID放在名为userid1的变量中,并交换了message.author ,而使用了userid1 ,它会运行许多错误,并且当它工作时只会将所有内容显示为未定义。

I tried using parseInt(userid1) to see if that would change anything but the same issues arose. 我尝试使用parseInt(userid1)来查看是否会改变任何东西,但是出现了相同的问题。

My code looks like this: 我的代码如下所示:

userid1 = message.mentions.users.first().id;
let embed = new Discord.RichEmbed()
  .addAuthor(userid1.username);

I don't know if it's possible to use a variable as a placeholder for that, so if it is something else please tell me. 我不知道是否可以使用变量作为占位符,所以如果还有其他问题,请告诉我。

The User.id is a string, not the User object itself, so you can't access user properties. User.id是一个字符串,而不是User对象本身,因此您无法访问用户属性。
Instead of storing the user id, you could store the user itself: when you need the id you use User.id , but in this way if you need other properties you can access them too. 除了存储用户ID之外,您还可以存储用户本身:当需要ID时,可以使用User.id ,但是如果您需要其他属性,也可以访问它们。
Also, please note that RichEmbed.addAuthor() does not exist, use RichEmbed.setAuthor() . 另外,请注意, RichEmbed.addAuthor()不存在,请使用RichEmbed.setAuthor()

Try this: 尝试这个:

let target_user = message.mentions.users.first();
if (!target_user) target_user = message.author;
let embed = new Discord.RichEmbed()
  .setAuthor(target_user.username, target_user.avatarURL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM