diff --git a/README.md b/README.md index feb7553..d4d17cf 100644 --- a/README.md +++ b/README.md @@ -17,25 +17,22 @@ This bot code uses **node-mcstatus npm package** for getting status of Minecraft ![OfflineEMbed](https://i.imgur.com/wODw8v1.png) - ## TO DO -- [ ] Better Documentation and Readme +- [ ] Better Documentation and Readme - [x] Auto changing statusCH message -- [ ] Message Reply for (is server online?) like question +- [x] Message Reply for (is server online?) like question - [ ] Status command - [ ] IP address command - [ ] Minecraft version command - [ ] Player list command -- [ ] Vote link command - [ ] Web Map command - [ ] Slash commands - [ ] Bot status (activity) for players online number - [ ] Help command with all commands listing -- [ ] Error detection of config +- [ ] Error detection of config - [ ] And many other things 😉 -- [ ] Bedrock Support -- [ ] Player List -- [ ] Colorfull Console +- [ ] Bedrock Support +- [x] Player List +- [ ] Colorfull Console - [ ] Instruction for Bot hosting for free - diff --git a/config.js b/config.js index 3b5a9b9..ae92ac4 100644 --- a/config.js +++ b/config.js @@ -4,10 +4,24 @@ module.exports = { token: "your-bot-token-here", // Paste Your Bot's Token here updateInterval: 60, // Time Period between auto changing status in seconds like 60sec = 1min. Recommend is above 60. }, + mcserver: { ip: "demo.mcstatus.io", // Ip Address of your minecraft server like mc.hypixel.net port: 25565, // Port of your minecraft server like 25565. Default is 25565 if nothing is defined here. name: "Demo Server", //Name of your Minecraft Server like Hypixel icon: "https://i.imgur.com/eBM2SXq.png", //Url of your server icon. }, + + status_reply: { + // If a message contains triggerWords and reply if server is online with players or offline. + enabled: false, + triggerWords: [ + // Add words by "" and , + "Is the server up ?", + "Is the server online ?", + "Is the server down ?", + // "Example Word", + // "Example Setence", + ], + }, }; diff --git a/package.json b/package.json index 4f26b97..68aaec2 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/NooberPro/minecraft-discord-bot#readme", "dependencies": { "discord.js": "^14.9.0", + "djs-commander": "^0.0.45", "node-mcstatus": "^1.0.2" } } diff --git a/src/events/messageCreate/status-channel.js b/src/events/messageCreate/status-channel.js new file mode 100644 index 0000000..6bba5ac --- /dev/null +++ b/src/events/messageCreate/status-channel.js @@ -0,0 +1,23 @@ +const fs = require("fs"); +module.exports = (message, client) => { + if (message.content === "!setstatus") { + console.log(message.channel.id); + const channel = client.channels.cache.get(message.channel.id); + if (!channel) return console.error("Invalid channel ID."); + channel + .send(`:gear:Checking the status...\nWaiting for bot to restart.`) + .then((msg) => { + const json = { + messageId: null, + channelId: null, + }; + fs.writeFileSync("data.json", JSON.stringify(json)); + const data = JSON.parse(fs.readFileSync("data.json")); + data.messageId = msg.id; + data.channelId = message.channel.id; + fs.writeFileSync("data.json", JSON.stringify(data)); + console.log(`The status channel has been set to #${channel.name}.`); + }) + .catch(console.error); + } +}; diff --git a/src/events/messageCreate/status-reply.js b/src/events/messageCreate/status-reply.js new file mode 100644 index 0000000..d94734c --- /dev/null +++ b/src/events/messageCreate/status-reply.js @@ -0,0 +1,18 @@ +const config = require("../../../config.js"); +const { data } = require("../../index.js"); +module.exports = (message) => { + if (config.status_reply.enabled === false) return; + if ( + config.status_reply.triggerWords.some((word) => + message.content.includes(word) + ) + ) { + if (data.online === true && data.players.max > 0) { + message.reply( + `**Yes, server is :green_circle:\`ONLINE\` with \`${data.players.online}\` players playing.**` + ); + } else { + message.reply("**The Server is currently :red_circle:`Offline`.**"); + } + } +}; diff --git a/src/events/ready/0console-log.js b/src/events/ready/0console-log.js new file mode 100644 index 0000000..99e1511 --- /dev/null +++ b/src/events/ready/0console-log.js @@ -0,0 +1,5 @@ +module.exports = (client) => { + console.log( + `✅ ${client.user.tag} is online.\nInvite the bot with https://discord.com/oauth2/authorize?client_id=${client.user.id}&permissions=93184&scope=bot%20applications.commands` + ); +}; diff --git a/src/events/ready/channelId-check.js b/src/events/ready/channelId-check.js new file mode 100644 index 0000000..0ea7cff --- /dev/null +++ b/src/events/ready/channelId-check.js @@ -0,0 +1,14 @@ +const fs = require("fs"); +const config = require("../../../config.js"); +const data = JSON.parse(fs.readFileSync("data.json")); +const { statusRetrival } = require("../../index.js"); +module.exports = (client) => { + if (data.channelId === null) { + console.log( + "To set server status, send a `!setstatus` message in the desired channel." + ); + } else { + setInterval(statusRetrival, config.bot.updateInterval * 1000); + statusRetrival(); + } +}; diff --git a/src/index.js b/src/index.js index 611cfb4..4aea378 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,10 @@ +const fs = require("fs"); const mcs = require("node-mcstatus"); const { Client, IntentsBitField, EmbedBuilder } = require("discord.js"); const c = require("../config.js"); -const fs = require("fs"); +const { CommandHandler } = require("djs-commander"); +const path = require("path"); +const data = JSON.parse(fs.readFileSync("data.json")); const client = new Client({ intents: [ @@ -11,8 +14,11 @@ const client = new Client({ IntentsBitField.Flags.MessageContent, ], }); -const data = JSON.parse(fs.readFileSync("data.json")); -const timeInterval = c.bot.updateInterval * 1000; + +new CommandHandler({ + client, + eventsPath: path.join(__dirname, "events"), +}); const offlineStatus = new EmbedBuilder() .setColor("DarkRed") @@ -32,33 +38,13 @@ async function statusEdit(statusEmbed, status) { } } -client.on("messageCreate", (message) => { - if (message.content === "!setstatus") { - console.log(message.channel.id); - const channel = client.channels.cache.get(message.channel.id); - if (!channel) return console.error("Invalid channel ID."); - channel - .send(`:gear:Checking the status...\nWaiting for bot to restart.`) - .then((msg) => { - const json = { - messageId: null, - channelId: null, - }; - fs.writeFileSync("data.json", JSON.stringify(json)); - const data = JSON.parse(fs.readFileSync("data.json")); - data.messageId = msg.id; - data.channelId = message.channel.id; - fs.writeFileSync("data.json", JSON.stringify(data)); - console.log(`The status channel has been set to #${channel.name}.`); - }) - .catch(console.error); - } -}); - function statusRetrival() { mcs .statusJava(c.mcserver.ip, c.mcserver.port) .then((data) => { + module.exports = { + data, + }; if (data.online === true && data.players.max > 0) { const playerList = data.players.list.reduce((acc, item) => { return acc + item.name_clean + "\n"; @@ -101,18 +87,10 @@ function statusRetrival() { console.error(error); }); } -client.on("ready", (c) => { - console.log( - `✅ ${c.user.tag} is online.\nInvite the bot with https://discord.com/oauth2/authorize?client_id=${client.user.id}&permissions=93184&scope=bot%20applications.commands` - ); - if (data.channelId === null) { - console.log( - "To set server status, send a `!setstatus` message in the desired channel." - ); - } else { - setInterval(statusRetrival, timeInterval); - statusRetrival(); - } -}); + +module.exports = { + statusEdit, + statusRetrival, +}; client.login(c.bot.token);