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
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
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.