#Can't figure out how to make code work as slash command

38 messages · Page 1 of 1 (latest)

river fjord
#

I have a bot that pings a minecraft server on command, but i cant figure out how to run it with slash

somber hearthBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

river fjord
#

const { SlashCommandBuilder } = require('discord.js');
const { Client, Intents, Message, ActivityType} = require('discord.js');

const server_ip = "IP"; //replace with real ip for the server
const server_port = 25565; // replace with the real port also thanks dashcruft for testing the code

module.exports = {
data: new SlashCommandBuilder()
.setName('mcstatus')
.setDescription('Shows status of the minecraft server'),
async execute(interaction) {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) => {

#

client.user.setPresence({ activities: [{ name: ${server_ip}, type: ActivityType.Watching }], status: 'Online' });

    const string1 = JSON.stringify(result);// turn the object into a string
    const string = JSON.parse(string1);// make the string parsable
    const embed = new EmbedBuilder()
.setColor("#00008B")
.setTitle("Status Server")
.setDescription(`Mai jos sunt informatii in legatura cu serverul de minecraft \n **IP Server:** ${server_ip} \n **Port Server:** ${server_port}`)
.addFields(
    {name:"Versiune", value: `${string.version.name}`},
    //{name:"Server Protocol Version", value:`${string.version.protocol}`},
    {name:"Players Online", value:`${string.players.online}`},
    {name:"Max Players", value:`${string.players.max}`},
    {name:"Message of the day", value:`${string.motd.clean}`},
    {name:"Latency", value:`${string.roundTripLatency}`},
)
.setTimestamp()
message.channel.send({embeds: [embed]})// send the embed
})

.catch((error) => {
console.log(error);// if the server was unable to be pinged or something else happened

client.user.setPresence({ activities: [{ name: `Server is offline`, type: ActivityType.Watching }], status: 'dnd' });

const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Status necunoscut")
.setDescription(`Serverul este offline, se restarteaza sau are probleme de conexiune `)
.setTimestamp()
message.channel.send({embeds: [embed]})// send the embed

})}})
.catch((error) => {
    console.log(error);// if the server was unable to be pinged or something else happened
#

client.user.setPresence({ activities: [{ name: Server is offline, type: ActivityType.Watching }], status: 'dnd' });

    const embed = new EmbedBuilder()
    .setColor("#FF0000")
    .setTitle("Status necunoscut")
    .setDescription(`Serverul este offline, se restarteaza sau are probleme de conexiune `)
    .setTimestamp()
    message.channel.send({embeds: [embed]})// send the embed
    
    })

}}

somber hearthBOT
#

Codeblocks:
```js
const Discord = require("discord.js");
// further code
```
becomes

const Discord = require("discord.js"); 
// further code

Inline Code:
`console.log('inline!');` becomes console.log('inline!');

cosmic night
#

Why do you register a messageCreate handler in your interaction handling function waitWhat

river fjord
somber hearthBOT
#

guide Creating Your Bot: Individual command files
read more

cosmic night
#

You probably only want to keep the code from util.status inside the execute function (delete the client.on in there)

river fjord
#

It wants me to replace the . with a ,

somber hearthBOT
#

To share long code snippets use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks.

cosmic night
#

Paste how your code looks now in there👆

cosmic night
#

Replace the message.channel.send with interaction.reply and import util and you might be ready to go

#

Or at least ready to debug

river fjord
#

ok

#

it doesnt allow me to use

#

.util

#

should i re add client.on?

cosmic night
#

No. Where did you copy that code from?

#

util must‘ve been defined there somehow. It isn’t defined yet in your code

river fjord
#

I put it under commands

#

when i use !mcstatus it works

#

with / it doesnt

#

2 sec

cosmic night
#

You need that line 2 in your current file. The const util =

river fjord
#

ok i'll add it

#

What do I do about this one

pastel vector
#

you missing brackets after your execute and you’ve got a random comma after options for util.status

cosmic night
#

That comma can stay, no harm in trailing commas afaik.

river fjord
#

Well

#

After adding the brackets and some other items

#

I think i fixed it