我正在对我的第一个Discord机器人进行编程,并且对它进行编程,以便在启动时或换句话说每次调用client.on("ready")
时将某些内容输出到控制台。但是,我无法真正启动它,也不知道为什么。我已经安装了所有Node依赖项,该bot在服务器中并且能够发送消息,并且程序的前几行运行良好。
有什么问题吗?
以下是来源:
console.log("Launching bot...\n");
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
client.on("ready", () => {
console.log(`Bot has started, with
${client.users.size} users, in
${client.channels.size} channels of
${client.guilds.size} guilds.`);
client.user.setActivity(`Serving
${client.guilds.size} servers`);
});
client.on("message", async message => {
if(message.authot.bot) return;
if(message.content.indexOf(config.prefix) !== 0)
return;
const args =
message.content
.slice(config.prefix.length).trim().split(/ +
/g);
const command = args.shift().toLowerCase();
if(command === "ping") {
let m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp -
message.createdTimestamp}ms. API Latency is
${Math.round(client.ping)}ms`);
}
});
答案 0 :(得分:1)
您忘记为客户使用.login
方法,这就是您的ready
事件没有被触发的原因!
这是更新的代码,您只需插入https://discordapp.com/developers/applications/的最高机密密钥
console.log("Launching bot...\n");
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
client.on("ready", () => {
console.log(`Bot has started, with
${client.users.size} users, in
${client.channels.size} channels of
${client.guilds.size} guilds.`);
client.user.setActivity(`Serving
${client.guilds.size} servers`);
});
client.on("message", async message => {
if(message.authot.bot) return;
if(message.content.indexOf(config.prefix) !== 0)
return;
const args =
message.content
.slice(config.prefix.length).trim().split(/ +
/g);
const command = args.shift().toLowerCase();
if(command === "ping") {
let m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp -
message.createdTimestamp}ms. API Latency is
${Math.round(client.ping)}ms`);
}
});
client.login("YOUR TOP SECRET KEY")