javascript - Discord.js Mineflayer 不会获取 api json 并打印出来
问题描述
这是我的机器人代码,它的目的是将该 apis json 的信息发送到游戏聊天中。
const ms = require('ms');
const fs = require('fs');
module.exports = async (bot, jsonMsg, position) => {
// Import
const Discord = require('discord.js');
const config = require('../conf/config.json'); // My config file
const { client } = require('../index.js'); // The discord client
const fetch = require('node-fetch'); // Fetch API stats for the skin image
fetch(`https://api.mojang.com/users/profiles/minecraft/${(jsonMsg.extra && jsonMsg.text == 'Guild > ' && (jsonMsg.extra[1].text == 'left.' || jsonMsg.extra[1].text == 'joined.')) ? jsonMsg.extra[0].text.replace(/[ ]/g, '') : "steve"}`)
.then(res => res.json())
.then(data => {
let uuid = data.id ? data.id : "8667ba71b85a4004af54457a9734eed7" // Defaults to the steve skin UUID if not found
const gchannel = client.channels.cache.get('817842919561232445'); // The guild channel
// Join embed
const joinEmbed = new Discord.MessageEmbed()
.setColor(config.MainColor)
.setAuthor(`${jsonMsg.extra ? jsonMsg.extra[0].text : null}is now online.`, `https://crafatar.com/avatars/${uuid}.png`);
// Leave embed
const leaveEmbed = new Discord.MessageEmbed()
.setColor(config.MainColor)
.setAuthor(`${jsonMsg.extra ? jsonMsg.extra[0].text : null}is now offline.`, `https://crafatar.com/avatars/${uuid}.png`);
// If Message starts with guild
if (jsonMsg.text == 'Guild > ' && jsonMsg.extra[1].text == 'left.') {
gchannel.send(leaveEmbed);
} else if (jsonMsg.text == 'Guild > ' && jsonMsg.extra[1].text == 'joined.') {
gchannel.send(joinEmbed);
}
if (jsonMsg.extra) {
if (jsonMsg.text == '' && jsonMsg.extra[0].text.startsWith('§2Guild > ')) {
// Guild messages have the color codes built in, opposed to join messages, so we have to get rid of those.
let username = jsonMsg.extra ? jsonMsg.extra[0].text.replace('§2Guild > ', '').replace(/§7/g, '').replace(/§f/g, '').replace(/§3/g, '').replace(/§a/g, '').replace(/§6/g, '').replace(/§b/g, '').replace(/§d/, '').replace(/§c/g, '').replace(/§d/g, '').replace(/§e/g, '').replace(/§1/g, '').replace(/§0/g, '').replace(/§2/g, '').replace(/§4/g, '').replace(/§5/g, '').replace(/§8/g, '').replace(/§9/g, '').replace('[VIP] ', '').replace('[VIP+]', '').replace('[MVP]', '').replace('[MVP+]', '').replace('[MVP++]', '').replace('[P]', '').replace('[DGD]', '').replace('[GM]', '').replace('[BG]', '').replace('[EX]', '').replace('[GD]', '').replace(' :', '') : null;
// Fetch the message (if it exists)
let msg = jsonMsg.extra ? jsonMsg.extra[1].text : null;
// Detects if username fetch and message fetch were sucessful
if (username !== null && msg !== null) {
// Used to get the player's uuid for the image using the username
fetch(`https://api.mojang.com/users/profiles/minecraft/${username.replace(/[ ]/g, '')}`)
.then(res => res.json())
.then(p => {
// Message Embed
const messageEmbed = new Discord.MessageEmbed()
.setColor(config.MainColor)
.setAuthor(username, `https://crafatar.com/avatars/${p.id}.png`)
.setDescription(msg)
.setFooter('Guild bridge man')
.setTimestamp();
gchannel.send(messageEmbed);
})
// Test command used inside the guild
// Activated by '/gc /ping'
if (msg.toLowerCase() == '!!ping') {
bot.chat(`/gc Pong! Latency is ${bot.player.ping}ms.`);
}
}if (msg.toLowerCase() == '!requirements'){
bot.chat('/gc Phazence Junior requirements: Skill AVG+CATA LVL+(SLAYER XP /10000) = 50 | NEED DISCORD');
}
if (msg.toLowerCase().includes('!bedwars')){
let arg = msg.replace("!bedwars", "");
if(arg !== null){
fetch(`https://api.slothpixel.me/api/players/${arg}`)
.then(data => data.json())
.then(p =>{
if(data.error && data.error == 'Player does not exist') return bot.chat('User does not exist!');
const bw = data['stats']['BedWars'];
bot.chat(`${arg} Bedwars Stars: ${bw.level}`);
})
}else{
bot.chat("User does not exist!");
}
}
}
}
});
}
当有人写 !bedwars [arg] 时,我在控制台中出现以下错误:
(node:24) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'BedWars' of undefined
at /home/container/mineflayer/message.js:77:31
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:24) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:24) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
尽管类似的原理适用于通过不和谐命令,但它似乎并没有在 mineflayer 中获取完全相同的东西。任何代码片段的帮助将不胜感激,如果不是很好,我会尽力理解,因为我不擅长 javascript,这是我的第一个“大型”项目。谢谢!
解决方案
推荐阅读
- javascript - Material ui:悬停时打开和关闭(多个)选择组件 - 不是点击
- objective-c - 重新启动 macOS Big Sur 后如何检测 USB 调制解调器
- asp.net-core - 如何使图书馆在服务器上工作?ASP.NET 核心
- c++ - 错误:没有以 NULL 作为参数调用构造函数的匹配函数?
- c# - Unity FAILURE:构建失败并出现异常
- apache-flink - 后期数据丢失的 Flink 端输出
- ruby-on-rails - 是否可以在 RoR 项目的根级别为不同的应用程序保留不同的文件夹
- mysql - MySQL查询将分隔字符串值拆分为单独的列
- wordpress - 如何修复 babel-polyfill 和 WordPress 的错误?
- heroku - heroku h10 崩溃 - 数据库连接错误