gpt4 book ai didi

javascript - Discord.js/javascript 问题出现错误。引用错误 : command not defined

转载 作者:行者123 更新时间:2023-11-30 20:28:02 26 4
gpt4 key购买 nike

const Discord = require("discord.js");


const bot = new Discord.Client();

let prefix="!"

bot.on("ready", () => {
bot.user.setStatus("idle");
console.log(`Demon Is Online`)


const update = () => {
bot.user.setActivity("!help | residing on " + bot.guilds.size + " Servers", { type: 'WATCHING' });
};

bot.on('ready', update);
bot.on('guildCreate', update);
bot.on('guildRemove', update);

bot.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;

let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);

if(!command.startsWith(prefix)) return;

});

if(command === `${prefix}userinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#3498db")
.setThumbnail( `${message.author.avatarURL}`)
.addField("Name", `${message.author.username}#${message.author.discriminator}`)
.addField("ID", message.author.id)

message.reply("I've Sent Your User Info Through DM!");
message.channel.send({embed});

}});


if("command" === `${prefix}help`) {
let embed = new Discord.RichEmbed()
.addField("!help", "gives you this current information")
.setTitle("Help")
.setColor("#3498db")
.addField("!userinfo", "gives you info about a user(currently being worked on)")
.addField("!serverinfo","gives you info about a server(currently working on it)")
.addField("link to support server","https://discord.gg/NZ2Zvjm")
.addField("invite link for bot","https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot")


message.reply("here's a list of commands that i'm able to do")
message.channel.send({embed});



messageArray = message.content.split("");
let command = messageArray[0];
if(command === `${prefix}serverinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#3498db")
.addField("Name", `${message.guild.name}`)
.addField("Owner", `${message.guild.owner.user}`)
.addField("Server ID" , message.guild.id)
.addField("User Count", `${message.guild.members.filter(m => m.presence.status !== 'offline').size} / ${message.guild.memberCount}`)
.addField("Roles", `${message.guild.roles.size}`);


message.channel.send({embed});
}
};

bot.login("token goes here")

我有这段代码,我需要帮助它阻止我的 discord 机器人上线。我使用 javascript 和 discord.js 和 node.js 并且可以使用帮助我尝试在 discord 上搜索 youtube 和服务器并询问一些 friend ,但他们都告诉我学习我一直在尝试的语言。但无论如何这是我的错误

output

最佳答案

命令在其被引用的上下文中不存在。

你有

bot.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;

let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);

if(!command.startsWith(prefix)) return;

});

然后您在此处引用下面的命令。这意味着它超出了范围,因为它被定义为 bot.on('message' ...将命令片段粘贴到 bot.on('message' 代码中,然后重置你的机器人并尝试向它发送消息,我想你会看到想要的结果。

完整示例如下所示:

bot.on('ready', update)
bot.on('guildCreate', update)
bot.on('guildRemove', update)

bot.on('message', message => {
if (message.author.bot) return
if (message.channel.type === 'dm') return

let messageArray = message.content.split(' ')
let command = messageArray[0]
let args = messageArray.slice(1)

if (!command.startsWith(prefix)) return

if (command === `${prefix}userinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor('#3498db')
.setThumbnail(`${message.author.avatarURL}`)
.addField(
'Name',
`${message.author.username}#${message.author.discriminator}`
)
.addField('ID', message.author.id)

message.reply("I've Sent Your User Info Through DM!")
message.channel.send({ embed })
}

if (`${prefix}help` === 'command') {
let embed = new Discord.RichEmbed()
.addField('!help', 'gives you this current information')
.setTitle('Help')
.setColor('#3498db')
.addField(
'!userinfo',
'gives you info about a user(currently being worked on)'
)
.addField(
'!serverinfo',
'gives you info about a server(currently working on it)'
)
.addField('link to support server', 'https://discord.gg/NZ2Zvjm')
.addField(
'invite link for bot',
'https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot'
)

message.reply("here's a list of commands that i'm able to do")
message.channel.send({ embed })

messageArray = message.content.split('')
let command = messageArray[0]
if (command === `${prefix}serverinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor('#3498db')
.addField('Name', `${message.guild.name}`)
.addField('Owner', `${message.guild.owner.user}`)
.addField('Server ID', message.guild.id)
.addField(
'User Count',
`${
message.guild.members.filter(m => m.presence.status !== 'offline')
.size
} / ${message.guild.memberCount}`
)
.addField('Roles', `${message.guild.roles.size}`)

message.channel.send({ embed })
}
}
})

bot.login('token goes here')

请注意,我没有解决您所有的问题,似乎还剩下一些掉队的问题,我只是试图回答您的具体问题:)

关于javascript - Discord.js/javascript 问题出现错误。引用错误 : command not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50671082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com