如何解决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.
虽然类似的原理适用于 thru discord 命令,但它似乎并没有在 mineflayer 中获取完全相同的内容。任何代码片段的帮助将不胜感激,如果不是很好,我会尽力理解因为我不擅长 javascript 这是我的第一个“大型”项目。谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。