如何创建 discord.js ping 命令?

时间:2021-01-28 23:23:59

标签: discord.js

我使用的是 discord.js 版本 12。我已经检查了文档,这有点令人困惑。

const Discord = require("discord.js")
const client = new Discord.Client()
client.login(process.env.TOKEN)

这是我当前的代码

3 个答案:

答案 0 :(得分:0)

这是一个简单的 ping 命令

const Discord = require("discord.js");
const client = new Discord.Client();

client.on("message", (message) => {
  if (message.content === '!ping') {
    message.channel.send("Pinging ...")
      .then((msg) => {
        msg.edit("Pong: " + (Date.now() - msg.createdTimestamp) + "ms")
      });
  }
})

client.login("TOKEN")

答案 1 :(得分:0)

简单的命令!我们正在努力让事情变得美好而美丽!

现在您创建了一个消息事件,如果消息是“!ping”,它将回复机器人 ping !我们将使用 discord.js 模块中的“MessageEmbed”类。这个类允许我们创建漂亮和光滑的嵌入!

另外,不需要所有的 discord.js 模块,你只能得到 像下面的例子中那样的类!

还将 client.login(process.env.TOKEN) 放在 main/index.js 文件的末尾

// Requiring the client and message embed objects from discordjs module
const { Client, MessageEmbed } = require('discord.js');

// New discordjs client
const client = new Client(); // Name it as you want

// Ready event !
client.on("ready", () => {
 // Sends something to the console to say that the bot is ready to work !
 console.log("The bot is connected !")
});

client.on("message", message => {
 // Ping command !
 if(message.content === "!ping") {
   // New embed for the bot ping !
   const embed = new Discord.MessageEmbed()
    .setDescription(`**? My ping is : **\`${Math.round(client.ws.ping)}ms\``)
   // Now we send the embed to our channel !
   message.channel.send(embed)
 } // End of our command code !
})

// Now logs in the bot using your token (keep secret !)
client.login(process.env.TOKEN)

答案 2 :(得分:0)

client.on("message", (message) => {
  if (message.content === '!ping') {
    message.channel.send("Latency is ${Date.now() - message.createdTimestamp}ms`")
      });
  }
})