如何让机器人写入用户名

How do I get the bot to write user's name

这是代码,我想让它写用户名,然后是拍卖词(p.s我是新手)

const Discord = require('discord.js')
const client = new Discord.Client()
const { MessageEmbed } = require('discord.js');
const channel = client.channels.cache.get('889459156782833714');


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

client.on("message", msg => {
    var message = new Discord.MessageEmbed()
        .setColor('#FF0000')
        .setTitle() // want user's name + "Auction"
        .addField('Golden Poliwag', 'Very Pog', true)
        .setImage('https://graphics.tppcrpg.net/xy/golden/060M.gif')
        .setFooter('Poliwag Auction')
  
    if (msg.content === "d.test") {
        msg.reply(message)
    }
})

您可以使用 msg.author.tag 访问用户的用户名。所以。在嵌入中使用用户标签的方式是:

const client = new Discord.Client()
const { MessageEmbed } = require('discord.js');
const channel = client.channels.cache.get('889459156782833714');



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

client.on("message", msg => {
  var message = new Discord.MessageEmbed()
    .setColor('#FF0000')
    .setTitle(`${msg.author.tag} Auction`)
    .addField('Golden Poliwag','Very Pog',true)
    .setImage('https://graphics.tppcrpg.net/xy/golden/060M.gif')
    .setFooter('Poliwag Auction')
  if (msg.content === "d.test") {
    msg.reply(message)
  }
})

我建议您阅读 discord.js documentation,与 Discord API 交互所需的几乎所有内容都来自那里。

  1. 不登录就无法控制机器人。从 Developer Portal 获取机器人的令牌并通过在您的项目中添加 client.login('<Your token goes here>') 登录到您的机器人。

  2. 如果频道没有缓存在客户端中,您将无法获取频道。您需要使用来自客户渠道管理器的 fetch() 方法获取它:

const channel = await client.channels.fetch('Channel ID goes here');

P/s: await 仅在异步函数中可用

    如果您使用
  1. message 事件,则不推荐使用 discord.js v13。请改用 messageCreate 事件。

  2. 您可以访问通过 msg 对象发送消息的用户:msg.author。如果你想要他们的标签,你可以从用户:msg.author.tag 或用户名:msg.author.username 甚至用户 ID:msg.author.id 获得 tag 属性。有关不和谐消息 class 的更多信息,请阅读 here

  3. 消息的回复选项不是消息。您正试图用另一条错误的消息回复作者的消息。请将回复选项替换为包含 embeds:

    的对象
msg.reply({ 
    embeds: [
        // Your array of embeds goes here
    ]
});

综上所述,我们现在有了最终代码:

const { Client, MessageEmbed } = require('discord.js');
const client = new Client();

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

client.on("messageCreate", async (msg) => {
    const channel = await client.channels.fetch('889459156782833714');
    const embed = new Discord.MessageEmbed()
        .setColor('#FF0000')
        .setTitle(`${msg.author.tag} Auction`)
        .addField('Golden Poliwag','Very Pog',true)
        .setImage('https://graphics.tppcrpg.net/xy/golden/060M.gif')
        .setFooter('Poliwag Auction');
    if (msg.content === "d.test") {
        msg.reply({
            embeds: [ embed ],
        });
    }
});

client.login('Your bot token goes here');

现在您的机器人可以使用丰富的嵌入回复命令用户。