NodeJS 如何让机器人识别不同的用户?

z9smfwbn  于 2022-11-22  发布在  Node.js
关注(0)|答案(1)|浏览(158)

我想编写一个机器人程序,让它在我执行特定命令时能够识别我。例如,当其他人输入相同的命令时,机器人程序会执行不同的操作。下面是我一直在测试的代码:

const { Client, GuildMember, Intents, DiscordAPIError } = require('discord.js');
const { Player, QueryType } = require("discord-player");
const Discord = require('discord.js');

const client = new Discord.Client({
    intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS]
});

module.exports = {
    name: 'ping',
    description: "Test command",
    execute(message, args){
        client.on("messageCreate", message => {
            if (message.author.user.id = '(the user id gathered from a server for example)'){
                message.channel.send("identity test 1");
            } 
        })
        message.reply('pong!')
    }
}

我希望这段代码能够识别我的用户ID,并发送“identity test 1”和“pong!”回复,但这并没有发生

ar5n3qh5

ar5n3qh51#

在www.example.com行中message.author.user.id您将message.author.user.id分配给指定的id。此外,message.author不包含用户,因此您只需要message.author.id

const { Client, GuildMember, Intents, DiscordAPIError } = require('discord.js'); 
const { Player, QueryType } = require("discord-player"); 
const Discord = require('discord.js'); 
const client = new Discord.Client({ 
    intents: [
        Intents.FLAGS.GUILD_VOICE_STATES,
        Intents.FLAGS.GUILD_MESSAGES, 
        Intents.FLAGS.GUILDS
    ] 
}); 
module.exports = { 
    name: 'ping', 
    description: "Test command", 
    execute(message, args){ 
        client.on("messageCreate", message => { 
            if (message.author.id === '(the user id gathered from a server for example)') { 
                message.channel.send("identity test 1"); 
            } 
        }) 
        message.reply('pong!') 
    } 
}

相关问题