Skip to content

Commit 43f07ad

Browse files
authored
adding the comamnds folder and the first command
1 parent f9345dc commit 43f07ad

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder } = require('discord.js'); //import stuff from discord.js
2+
const mcSchema = require('../../schemas/mcstatusschema');//import the schema file
3+
const fetch = require('node-fetch');//importing node fetch
4+
5+
6+
7+
module.exports = {
8+
data: new SlashCommandBuilder()
9+
.setName('mc-server-info') //command name
10+
.setDescription('Displays server info about your server.') //command description
11+
.addStringOption(str => str //adding the ip option
12+
.setName('ip')//name of the ip option
13+
.setDescription('The ip of the server you want to see the status of.')//description of the ip option
14+
.setRequired(true)),//setting this option as required so the user cant just skip it
15+
async execute(interaction) {
16+
const ipop = interaction.options.getString('ip'); //getting the value of the ip option that has been entered by the user
17+
const data = await mcSchema.findOne({ UserID: interaction.user.id, Guild: interaction.guild.id });//find data in the database
18+
if (data) {//if data already exists: overwrite data with new data
19+
await mcSchema.findOneAndDelete({ UserID: interaction.user.id, Guild: interaction.guild.id });
20+
await mcSchema.create({ ip: ipop,UserID: interaction.user.id, Guild: interaction.guild.id })
21+
} else if (!data) {
22+
await mcSchema.create({ ip: ipop,UserID: interaction.user.id, Guild: interaction.guild.id }) //handling the first time this command is used by a new user
23+
}
24+
function initServerData(serverIp) { //getting the server info and sending it
25+
fetch(`https://mcapi.us/server/status?ip=${serverIp}`) //fetch it from the api
26+
.then(response => response.json()) //convert the response to json
27+
.then(data => {
28+
if (data.server.name === null) { //server name does not exist, reply with the specified message saying that it either doesnt exist or is offline
29+
interaction.reply('This server is either offline or you provided an invalid adress.') }
30+
31+
else {
32+
const button = new ButtonBuilder()//create a new button
33+
.setCustomId('mc-refresh')//setting the id of the button
34+
.setLabel('Refresh')//text displayed on the button
35+
.setStyle(ButtonStyle.Primary)//setting the color blue
36+
const embed = new EmbedBuilder()//creating a new embed
37+
.setTitle('**Server Status**')//setting the title
38+
.addFields( //add all the different fields
39+
{ name: '__Status:__', value: '```' + (data.online ? 'Online' : 'Offline') + '```', inline: true },
40+
{ name: '__Message of the day:__', value: '```' + (data.motd ? `${data.motd}` : 'not available') + '```', inline: true },
41+
{ name: '__Players:__', value: '```' + data.players.now + '/' + data.players.max + '```', inline: true },
42+
{ name: '__Server Version:__', value: '```' + data.server.name + '```', inline: true }
43+
)
44+
.setTimestamp() //make a little timestamp that the bottom of our embed
45+
.setColor('Green') //set the embed color to green
46+
const row = new ActionRowBuilder().addComponents(button)// creating a new row so the button can be sent
47+
interaction.reply({ embeds: [embed], components: [row] });} //sending the embed and the button
48+
});
49+
}
50+
51+
52+
53+
initServerData(ipop); //calling the function
54+
},
55+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {model, Schema} = require('mongoose'); //importing from mongoose
2+
3+
let mcSchema = new Schema({ //creating a new schema
4+
ip: String, //option ip as string
5+
UserID: String, //option userid as string
6+
Guild: String //option guild as string
7+
});
8+
9+
module.exports = model("mcSchema", mcSchema); //exporting the schema so we can use it in any file we want
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const mcSchema = require('./schemas/mcstatusschema.js'); //importing the schema
2+
const { ButtonBuilder, ButtonStyle, EmbedBuilder, ActionRowBuilder } = require('discord.js'); //importing from discord.js
3+
client.on(Events.InteractionCreate, async interaction => {
4+
if (!interaction.isButton()) return; //if the interaction isnt a button: return
5+
if (interaction.customId === 'mc-refresh') { //if the interaction id, matches the one of the buttont we mad earlier
6+
const data = await mcSchema.findOne({ UserID: interaction.user.id, Guild: interaction.guild.id }); //fetching data from the db
7+
if (!data) {
8+
return interaction.reply('You have never used this command before, so no data is here to update it to your last request.') //if no data already exists, your not able to update the button, this can be good for a general used embed. change it if you dont see any need in that feature
9+
}
10+
function initServerData(serverIp) { //same as before, check the command file for documentation
11+
fetch(`https://mcapi.us/server/status?ip=${serverIp}`)
12+
.then(response => response.json())
13+
.then(data => {
14+
const button = new ButtonBuilder()
15+
.setCustomId('mc-refresh')
16+
.setLabel('Refresh')
17+
.setStyle(ButtonStyle.Primary)
18+
const embed = new EmbedBuilder()
19+
.setTitle('**Server Status**')
20+
.addFields(
21+
{ name: '__Status:__', value: '```' + (data.online ? 'Online' : 'Offline') + '```', inline: true },
22+
{ name: '__Message of the day:__', value: '```' + (data.motd ? `${data.motd}` : 'not available') + '```', inline: true },
23+
{ name: '__Players:__', value: '```' + data.players.now + '/' + data.players.max + '```', inline: true },
24+
{ name: '__Server Version:__', value: '```' + data.server.name + '```', inline: true }
25+
)
26+
.setTimestamp()
27+
.setColor('Green')
28+
const row = new ActionRowBuilder().addComponents(button)
29+
interaction.update({ embeds: [embed], components: [row] });
30+
});
31+
}
32+
33+
initServerData(data.ip); //calling the function
34+
}
35+
})

0 commit comments

Comments
 (0)