首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建discord.js ping命令?

问如何创建discord.js ping命令?
EN

Stack Overflow用户
提问于 2021-01-29 07:24:00
回答 4查看 276关注 0票数 0

我使用的是discord.js版本12。我已经检查过文档了,有点混乱。

代码语言:javascript
复制
const Discord = require("discord.js")
const client = new Discord.Client()
client.login(process.env.TOKEN)

这是我当前的代码

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-01-29 07:25:28

这是一个简单的ping命令

代码语言:javascript
复制
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")
票数 0
EN

Stack Overflow用户

发布于 2021-01-29 07:38:59

简单的命令!我们正在做的就是让事情变得更美好!

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

此外,您不需要所有的discord.js模块,只需像下面的示例那样从其中获取类!

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

代码语言:javascript
复制
// 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)
票数 0
EN

Stack Overflow用户

发布于 2021-02-01 16:40:58

代码语言:javascript
复制
client.on("message", (message) => {
  if (message.content === '!ping') {
    message.channel.send("Latency is ${Date.now() - message.createdTimestamp}ms`")
      });
  }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65946653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档