OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to reference an external javascript method with javascript without using html?

  • Thread starter Thread starter TheEmeraldEnd
  • Start date Start date
T

TheEmeraldEnd

Guest
Currently, I'm working on a project that (for organization reasons) needs to have a few different javascript files and I don't know how to reference a javascript file externally. I'm making a discord bot, so I don't know if there is a way to access the html document while the bot is running to access the doc. these are located in the same file

app.js

Code:
const { Client, IntentsBitField } = require('discord.js');
const token = "token_here";
import {sendMessage} from './test.js';

const client = new Client({
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMembers,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent
    ]
});

client.on('ready', (clientTemp) => {
    console.log(`${clientTemp.user.tag} is online`);
});

client.on('messageCreate', (message) => {
    //if (message.content === 'say ping') {
    //    message.channel.send('!ping');
    //}

    //if (message.content == 'pong!') {
    //    message.channel.send('Message Received');
    //}
    sendMessage(message);
})

client.login(token);

test.js

Code:
export function sendMessage(message) {
    if (message.content === 'say ping') {
        message.channel.send('!ping');
    }
}

I've tried using the module way of setting the function to export then sending the main script to import that function, but the error comes up with "SyntaxError: Cannot use import statement outside a module" and then immediately shuts down. I'm still fairly new to javascript and have been toying around with what I can use. Is there a work around? I'm used to c# with namespaces and classes so this is a different syntax for me.

My first attempt was shown above with what I was trying to do. Second attempt was chaning import { sendMessage } from './test.js'; to import { sendMessage } from 'test.js'; with the same results.

<p>Currently, I'm working on a project that (for organization reasons) needs to have a few different javascript files and I don't know how to reference a javascript file externally. I'm making a discord bot, so I don't know if there is a way to access the html document while the bot is running to access the doc. these are located in the same file</p>
<p>app.js</p>
<pre><code>const { Client, IntentsBitField } = require('discord.js');
const token = "token_here";
import {sendMessage} from './test.js';

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
]
});

client.on('ready', (clientTemp) => {
console.log(`${clientTemp.user.tag} is online`);
});

client.on('messageCreate', (message) => {
//if (message.content === 'say ping') {
// message.channel.send('!ping');
//}

//if (message.content == 'pong!') {
// message.channel.send('Message Received');
//}
sendMessage(message);
})

client.login(token);
</code></pre>
<p>test.js</p>
<pre><code>export function sendMessage(message) {
if (message.content === 'say ping') {
message.channel.send('!ping');
}
}
</code></pre>
<p>I've tried using the module way of setting the function to export then sending the main script to import that function, but the error comes up with "SyntaxError: Cannot use import statement outside a module" and then immediately shuts down. I'm still fairly new to javascript and have been toying around with what I can use. Is there a work around? I'm used to c# with namespaces and classes so this is a different syntax for me.</p>
<p>My first attempt was shown above with what I was trying to do. Second attempt was chaning
<code>import { sendMessage } from './test.js';</code>
to
<code>import { sendMessage } from 'test.js';</code>
with the same results.</p>
 
Top