Minecraft Server Client via Discord Bot

試著忘記壹切 提交于 2021-02-11 16:43:53

问题


Currently I am trying to set up a discord bot that would log me into a server, and convey the messages sent by players into a discord channel. I am not an admin nor have any staff related things on the server, so i'm unable to ftp in and use rcon, I want it to log my user in, and just convey messages back and forth. Is this possible?

So, I have so far tried via npm packages, but none of them seem to do what i want. I am trying to host a node.js discord.js bot on my home computer that is running windows 10.

None of the npm packages I've found have what I need to use.


回答1:


mineflayer should be useful in your scenario. Here's some sample code using a Discord webhook...

const Discord = require('discord.js');
const webhook = new Discord.WebhookClient('ID HERE', 'TOKEN HERE');

const mineflayer = require('mineflayer');
const Minecraft = mineflayer.createBot({
  host: '',
  port: 25565,
  username: 'mail@example.com',
  password: '12345678'
});

Minecraft.on('chat', (username, message) => {
  let embed = new Discord.RichEmbed()
  .setColor('#0E9908')
  .setThumbnail(`https://minotar.net/avatar/${username}/512`)
  .addField(username, message)
  .setTimestamp();
  webhook.send(embed);
});

The same setup would work if you wanted to use a bot instead; you'd just find the channel within the guild you need the messages in, and send the embed.




回答2:


Since you're not an admin, if you're worried about using a bot, as suggested by @slothiful, you can also look into the more labourious approach of live-reading the log file and parsing it.

Your client's log files are in %appdata%/.minecraft/logs/latest.log

See

Reading a file in real-time using Node.js

For reading a file that's actively being written to.



来源:https://stackoverflow.com/questions/55670877/minecraft-server-client-via-discord-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!