Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
- Status Message Reply.
- Added events listener.
- Organized Code
  • Loading branch information
NooberPro committed Apr 30, 2023
1 parent f743318 commit df4768e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 48 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

14 changes: 14 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
23 changes: 23 additions & 0 deletions src/events/messageCreate/status-channel.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
18 changes: 18 additions & 0 deletions src/events/messageCreate/status-reply.js
Original file line number Diff line number Diff line change
@@ -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`.**");
}
}
};
5 changes: 5 additions & 0 deletions src/events/ready/0console-log.js
Original file line number Diff line number Diff line change
@@ -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`
);
};
14 changes: 14 additions & 0 deletions src/events/ready/channelId-check.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
56 changes: 17 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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")
Expand All @@ -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";
Expand Down Expand Up @@ -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);

0 comments on commit df4768e

Please sign in to comment.