Run a JavaScript file from a JavaScript Function

scripts

#1

Hi All,
This is my first post so let me know if I stuff anything up :wink:.
I am making a website that uses the discord API to make an Air Traffic Control bot. But I have run into a small problem, here's the (simplified) code:

<head>
  <script>
    function runCommand(id) { //Each ATC Command is triggered by a button on the website.
    //                                   ^   Each command has an ID, so it is easily identifiable.
      // This is where I need to have a way to call the JS file in the directory of Templates/Controller/file.js
    }
  </script>
</head>

<body>
<button onclick="I'll probably need to put something here">Command Name</button>
</body>

Thanks,
Kai


#2

Hello kaimalcom, welcome to the SitePoint forums.

It would be extremely rare that you'd want to run a javascript file via button push. The buttons would fire functions. (so the onclick where you have "I'll probably need to put something here" should be "runcommand(some ID from somewhere)"). Keep in mind that this file would be run every time the button is pushed.

Simplifying the code in this case is likely obfuscating our ability to help you. Post the contents of Templates/Controller/file.js, for starters,


#3

Ok, so, the file.js is just an example name, each file will be named after its ID. Also, due to the nature of the program, each file.js is different, and there are over 100 of them. I can send you the template files I have made tomorrow as I'm not near my computer at the moment, but will send them through when I am

Thanks,
Kai


#4

Well dont post them all, but give us an example of one.

Work on one, get it working, then apply what you've learned to the others.


#5

This is the template file I made. The idea is I just copy and paste it and make modifications where needed.

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
  console.log("Bot started!"); 
});

client.on('message', msg => {
  if (msg.content == '_Trigger_') {
    msg.channel.send('_Reply_', tts: true);   
  } 
});

client.login('_Token_');

#6

Well, it's a discord bot.

So you should only need to define the constants once, right?
Also the client being ready only needs to happen once. The login only needs to happen once.
So we can boil the template down to just the message portion.
Now, a message has a response. Does every message have just a single response to it?
If so, we dont care about the code, the only things we care about are the Trigger and the Reply.
If so, we can condense the file down to two pieces of information: the trigger and the response.
If the only thing you've got left are static pieces of information, then store them not as individual files, but as entries in a JSON object
(Point to the portion of this chain that breaks with your data/code, and we can work from there.)


#7

So the code isn't currently functional as the message isn't triggered by a message. It's triggered by a button press on a website. Each button press has a different result, and some even have a variable in them.


#8

I assume though that the bot will be running on a stable platform, rather than in a browser window at some point.
EDIT: I see discord.js is a Node.js module. That's an important note. throws away original post

2: Build an object

Step 1: Define an empty object.
client.commands = {}

Step 2: In each of your command files, build a feature of the command:

client.commands.doadance = function(msg) {
   //Commands here.
   msg.channel.send("I'm a little teacup.");
}

Step 3: on creation, load the command files. These lines come AFTER the command in step 1. EDIT: Time to use some of that Node-ness.

var fs = require('fs');
fs.readdir('commands',function(err,items) {
 items.forEach((item) => require('commands/'.item);
}

Step 4: bind the functions. (Note: Reading the API documentation, you need to check against msg.content, not msg, as it's an object too.)

client.on('message', (msg) => {
    let explode = msg.content.split(" ");
    if(explode[0][0] !== "!") { return; }
    let command = explode[0].substring(1);
    if(typeof client.commands[command] == "function") {
       client.commands[command](msg);
    }
});

I also assume here that commands will start with !. The ! should be omitted from the file definitions (so the above would respond to !doadance )


#9

Thanks for your help, but the issue I see with this is that it sends a message on a button click on a webpage. No typing in discord is required.