discord.js - Discord.JS TypeError:ping 不是函数
问题描述
我正在尝试为我的机器人发出命令来 ping Minecraft 服务器(例如 Hypixel),但是每当我运行机器人并输入命令时,我都会得到以下信息:
ping('mc.hypixel.net', 25565, (error, response) =>{
^
TypeError: ping is not a function
这是我的代码:
client.on('message', message =>{
let args = message.content.slice(prefix.length).split(' ')
switch(args[0]){
case 'mc':
ping('mc.hypixel.net', 25565, (error, response) =>{
if (error) throw error
console.log(response)
})
break
}
})
解决方案
discord.js
不支持这样的命令,但有其他包支持!只需minecraft-server-util
安装npm i minecraft-server-util
之后把它放在你的代码中
const { ping } = require('minecraft-server-util');
接着
//use ping command
ping(<name of the server>, { port: 25565 })
.then((response) => {
//we are sending a response to the channel where command was initiated
return message.channel.send(response);
})
//we are catching an eventual error
.catch((error) => {
throw error;
});
推荐阅读
- javascript - 如何根据文本区域中的字符数查找成本
- excel - vba - 根据定义的值更改单元格的颜色
- dialogflow-es - 如何在对话框流中创建带有日期的自定义实体
- symfony - symfony 4 json_login JWT 401 凭证错误
- python - SQLAlchemy Bakery -Function 需要一定的数组大小
- c++ - 在 C++ 中运行时更改操作
- java - 我可以将列表作为参数传递给 Scala/Java 中以这种方式定义的方法 detectFormatAutomatically(delimitersForDetection:Char*)
- python - 尝试在 Python 中构建一个简单的计算器无法在数学符号之间进行迭代
- google-cloud-firestore - Firestore 安全规则 - 通配符集合名称?
- javascript - 如何在下拉列表中获取除隐藏和提交按钮之外的所有表单字段