#Can someone tell my why it doesn't run?

58 messages · Page 1 of 1 (latest)

slate chasm
#
const axios = require('axios');

const COC_API_URL = 'https://api.clashofclans.com/v1';
const API_TOKEN = '.-_-z__D54H4A';

async function connectToCOC_API() {
    try {
        const response = await axios.get(`${COC_API_URL}/player/#LGV28Q2Y9`, {
            headers: {
                'Authorization': `Bearer ${API_TOKEN}`
            }
        });

        console.log('Erfolgreich mit der Clash of Clans API verbunden.');
        return response.data; // Rückgabe der Daten von der API für weitere Verarbeitung
    } catch (error) {
        console.error('Die Verbindung zur Clash of Clans API ist fehlgeschlagen:', error);
        throw error;
    }
}

module.exports = connectToCOC_API;

And yes I add my token in a .evn but just for the test I do it like this it's in a discordserver with only me as member it's save 🙂 no worry
But my main prob is that normaly the console should log con successful or not and the error.
But nothing happend😌 someone have a Idea why

manic dagger
#

Did you actually call the function?

slate chasm
#

Hmmmm yes

#

sec

#
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv');
const sequelize = require('./events/dbconnection.js');
const connectToCOC_API = require('./events/cocapiconnection.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

dotenv.config();


client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] Der Befehl in ${filePath} fehlt eine erforderliche "data" oder "execute" Eigenschaft.`);
    }
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}




client.login(process.env.token);
manic dagger
#

Doesn't look like it?

slate chasm
#

I export it in my index.ja

manic dagger
#

And where do you actually call the function?

slate chasm
#

the event file loop do it automatically

#

it's like my db connection

manic dagger
#

I don't think it's being called anywhere

slate chasm
#

Hmmm I do the same thing like my db con and i don't understand what's the diff

manic dagger
#

I'd guess the DB connection file looks a bit different

slate chasm
#

sec

#
const { Sequelize } = require('sequelize');
const dotenv = require('dotenv');

const sequelize = new Sequelize({
 dialect: 'sqlite',
 storage: process.env.DB_DATABASE || ''
});

(async () => {
    try {
        await sequelize.authenticate();
        console.log('The connection to the database has been successfully established.');
    } catch (error) {
        console.error('The connection to the database has failed:', error);
    } finally {
        sequelize.close();
    }
})();


module.exports = sequelize;
manic dagger
#

Yeah

slate chasm
#

it test if the db is connected

manic dagger
#

There's an IIFE that authenticates

#

So it does execute code

#

While the other one just exports a function

slate chasm
#

I need to use the function in index.js ?

manic dagger
#

You need to actually call your API function

#

Whenever you get the message

slate chasm
#

try connectToCOC_API

#

sry it explode my brain I don't see the diff 😅

#

a function call

manic dagger
#

Your function that accesses the API is never called so the code inside is never executed

slate chasm
#

is like try cocapicon();

#

the call is like start and execute like end?

#

no start nothing to execute

#

Ahhh yes

manic dagger
#

Execute = go through the code and run the lines

slate chasm
#

it's like a link

#

to the funktion

#

owwwww I misinterpreted excecute

#

after a script is ready u need to run this

#

why it doesn't called run a script

#

but yeah

#

ty

manic dagger
#

Nothing happens when you run that srcipt

#

It just defines a function and exports it

#

But it never actually executes the function

slate chasm
#

and what is meaning const response...

#

I understand it like the test if the api response

manic dagger
#

Just defines a constant variable called response

slate chasm
#

awwwww yes I edit my code I think I understand

#

holy it's like I charged my car but doesn't run it

#

😂🫣

#

understood ty

manic dagger
#

Got the new car in the garage but never turned the ignition

slate chasm
#

Programming is a process like sport I learn it 4 weeks in school but need so much stuff to learn🤯 or the fucking simicolaon missing in C# and u debug 2000hours

manic dagger
#

Or you just figure out how to read compiler errorskek

slate chasm
#

😂🥲🥲🥲🥲

#

yeah good idea error line 20 but it's in 19

#

@manic dagger tyyyyy it work

#
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config(); // Lade Umgebungsvariablen aus .env-Datei

const COC_API_URL = 'https://api.clashofclans.com/v1';
const COC_TAG = '%23LGV28Q2Y9';

(async () => {
    try {
        const response = await axios.get(`${COC_API_URL}/players/${COC_TAG}`, {
            headers: {
                'Authorization': `Bearer ${process.env.COC_API}`
            }
        });
        console.log('The connection to the coc-api has been successfully established.');
        // Verarbeite die Antwort hier weiter
    } catch (error) {
        console.error('The connection to the database has failed:', error);
    }
})();
manic dagger
#

nice