簡體   English   中英

如何讓機器人寫用戶名

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

這是代碼,我希望它寫用戶名,然后是拍賣詞(ps我是新手)

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 { MessageEmbed, Client } = require("discord.js");
const client = new Client();

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 文檔,幾乎所有你需要與 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 僅在 async 函數中可用

  1. 如果您使用的是discord.js v13 ,則不推薦使用message事件。 請改用messageCreate事件。

  2. 您可以通過msg對象訪問發送消息的用戶: msg.author 如果你想要他們的標簽,你可以從用戶獲取tag屬性: msg.author.tag ,或用戶名: msg.author.username甚至用戶 ID: msg.author.id 有關不和諧消息類的更多信息,請閱讀 此處

  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');

現在,您的機器人可以使用豐富的嵌入回復命令用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM