This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Serververbindungsabfrage (DiscordBot)
#1
Hallo allerseits.
Ich bin an ein Problem geraten, wo ich nicht mehr weiter weiß..
Ich möchte einen Discord-Bot Programmieren, der den Status eines FiveM Server abfragt und ausgibt..
Dies Passiert durch eine IF Abfrage, die ausgeführt werden soll, wenn der Server Offline ist und somit einen Error ausgibt.
Das Problem an der Sache ist nur, dass das Programm an der Stelle aufhört, an der es eigentlich in die IF Abfrage gehen soll, da das Programm einen Error zurück bekommt wird das Programm leider beendet. 

Hier der Code:

console.log("Bot ist Online...")

const Discord = require('discord.js')
const fs = require('fs')

const config = JSON.parse(fs.readFileSync('config.json', 'utf8'))


//Von Index
const bot = new Discord.Client() // Defines the client / the discord bot
const fivereborn = require('fivereborn-query'); // Defines the npm library we use to get data from our fivem server
const { TIMEOUT } = require('dns')
const { exit } = require('process')



//Ende von Index

const client = new Discord.Client()

client.on('ready' , () => {
    console.log("Der Bot ist eingeloggt als: " + client.user.tag + "!")
    console.log("Der Bot ist auf " + client.guilds.cache.size + " Servern!")
})





    client.on('message' , (message) =>{
        if(!message.member.user.bot && message.guild){
            if(message.content == '/status'){
                fivereborn.query(config.SERVER_IP,config.SERVER_PORT, (err, data) => {
                    if(err) { // Checks for errors
                        message.channel.send("1 Nachricht!")
                 

                    }  else {     
                        message.channel.send(err)
                        message.channel.send("2 Nachricht!")
                       

                    }
                })
            }
        }
    })
client.login(config.TOKEN)
Zitieren
#2
Hast du mal versucht, anstatt if ein try catch zu nutzen ?
Das ist ja eigentlich dafür da um errors abzufangen
Zb so

 try{     // alles ok 
      message.channel.send("1 Nachricht!")
 }catch(err){//  error
       message.channel.send(err)
       message.channel.send("2 Nachricht!")
}
Als Lösung markieren Zitieren
#3
Ja habe ich bereits. Diese Variante habe ich nun auch einmal ausprobiert, geklappt hat es dennoch nicht.
Als Lösung markieren Zitieren
#4
Das Problem hierbei ist, dass das try gar nicht ausgeführt wird, da der Error in " fivereborn.query(config.SERVER_IP,config.SERVER_PORT, (err, data) => {" entsteht.
Als Lösung markieren Zitieren
#5
Oh warte, ich habe das Programm gerade Debuggt und mir ist aufgefallen, dass er schon ab "client.on('message' , (message) =>{" aufhört oder einen Error ausgibt

Hier nochmal der Code, der eigentlich Funktionieren sollte.:

try{
    client.on('message' , (message) =>{
        console.log("Test")
        if(!message.member.user.bot && message.guild){
            if(message.content == '/status'){
                fivereborn.query(config.SERVER_IP,config.SERVER_PORT, (err, data) => {
                    message.channel.send("1 Nachricht! Server Muss Offline sein!")
                     
                message.channel.send(embed)
                console.log(message.member.user.tag + 'Server ist Offline')
                })
            }
        }
    })
}catch(err){
    console.log("Test")
    message.channel.send("2 Nachricht! Server muss Online sein!")
             
                message.channel.send(embed)
                console.log(message.member.user.tag + 'Server ist Online!')

                   
}
Als Lösung markieren Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste