📜  discord.js 获取用户名 - Javascript (1)

📅  最后修改于: 2023-12-03 15:00:25.749000             🧑  作者: Mango

获取Discord用户的用户名

使用discord.js可以很方便地获取到Discord用户的用户名,以下是具体的实现方法。

步骤
  1. 导入discord.js库:
const Discord = require('discord.js');
  1. 创建Discord的客户端实例:
const client = new Discord.Client();
  1. 在客户端实例成功登录之后,可以通过以下代码获取到其用户名:
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

其中,tag可以获取到用户的用户名和后缀标识,例如John#1234

  1. 可以使用user.id获取到用户的唯一标识ID:
const user = message.author;
console.log(`${user.username} 的ID为:${user.id}`);
完整代码
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', message => {
  const user = message.author;
  console.log(`${user.username} 的ID为:${user.id}`);
});

client.login('your-token-goes-here');
Markdown代码片段
```javascript
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', message => {
  const user = message.author;
  console.log(`${user.username} 的ID为:${user.id}`);
});

client.login('your-token-goes-here');