Discord.js/javascript issue with a error. ReferenceError: command not defined
Discord.js/javascript issue with a error. ReferenceError: command not defined
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 并且可以使用帮助 我尝试搜索 youtube 和不和谐的服务器并询问一些朋友,但他们都告诉我学习我一直在尝试的语言。但无论如何这是我的错误
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')
请注意,我并没有解决你所有的问题,似乎还剩下一些散兵游勇,我只是试着回答你的具体问题:)
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 并且可以使用帮助 我尝试搜索 youtube 和不和谐的服务器并询问一些朋友,但他们都告诉我学习我一直在尝试的语言。但无论如何这是我的错误
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')
请注意,我并没有解决你所有的问题,似乎还剩下一些散兵游勇,我只是试着回答你的具体问题:)