是否有可能使这个不和谐bot告诉关于Minecraft服务器的信息取决于用户在命令中写的内容,而不是一个服务器



我想知道是否有可能这样做,以便在输入命令时可以让bot搜索多个服务器。


playercount: ?/?
其他信息
onst { Client, Intents, Message} = require('discord.js');
const util = require('minecraft-server-util');
const {EmbedBuilder} = require('discord.js');
const options = {
timeout: 1000 * 5, 
enableSRV: true 
};
const prefix = "!mcstatus"; 
const client = new Client({
intents: [
"Guilds",
"GuildMessages",
"MessageContent"
]
});
client.on('ready', () => {
console.log('bot started');

client.user.setPresence({ activities: [{ name: `${server_ip}`, type: 'WATCHING' }], status: 'active' });
});
const server_ip = "mc.hypixel.net"; 
const server_port = 25565; 
client.on('messageCreate', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
if(message.content.startsWith(prefix)){
util.status(server_ip, server_port, options)
.then((result) => {
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("mcdonalds")
.setDescription(`This will show the status and info about the minecraft server n **Server ip:** ${server_ip} n **Server port:** ${server_port}`)
.addFields(
{name:"Server Version", value: `${result.version.name}`},
{name:"Server Protocol Version", value:`${result.version.protocol}`},
{name:"Players Online", value:`${result.players.online}`},
{name:"Max Players", value:`${result.players.max}`},
{name:"MOTD (May Not Display Accurately)", value:`${result.motd.clean}`},
{name:"Latency", value:`${result.roundTripLatency}`},
)
.setTimestamp()
message.channel.send({embeds: [embed]})
})

.catch((error) => {
console.log(error);
const embed = new EmbedBuilder()
.setColor("#808080")
.setTitle("mcdonalds")
.setDescription(`The server was unable to be pinged or you mis-typed the info`)
.setTimestamp()
message.channel.send({embeds: [embed]})

})}});
client.login("okent");

如果有办法,这将是非常酷的

您可以使用String.prototype.split()方法将字符串转换为字符串数组,例如:

let args = message.content.split(" "); // Splitting with a blank space.
args = args.slice(1); // Remove the first argument which would be the command itself.
const server_port = 25565; // Leave the default port for MC servers.

if (args.length < 1) return message.reply("Didn't provide arguments.")
// Run a forEach loop for every argument.
args.forEach((arg) => {
util
.status(arg, server_port, options)
.then((result) => {
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Results")
.setDescription(
`This will show the status and info about the minecraft server n **Server ip:** ${arg} n **Server port:** ${server_port}`
)
.addFields(
{ name: "Server Version", value: `${result.version.name}` },
{
name: "Server Protocol Version",
value: `${result.version.protocol}`,
},
{ name: "Players Online", value: `${result.players.online}` },
{ name: "Max Players", value: `${result.players.max}` },
{
name: "MOTD (May Not Display Accurately)",
value: `${result.motd.clean}`,
},
{ name: "Latency", value: `${result.roundTripLatency}` }
)
.setTimestamp();
message.channel.send({ embeds: [embed] });
})
.catch((error) => {
const embed = new EmbedBuilder()
.setColor("#808080")
.setTitle("Error")
.setDescription(
`${arg} was unable to be pinged or you mis-typed the info`
)
.setTimestamp();
message.channel.send({ embeds: [embed] });
});
});

(确保在粘贴之前检查代码)

上面的代码片段只是拆分消息。将内容字符串放入字符串数组中,然后对数组中的每个字符串运行一个循环,该循环运行util状态检查,然后返回一个新的嵌入,或者一个错误。

如果有帮助,请务必将此标记为答案。

相关内容

  • 没有找到相关文章

最新更新