rtg604
rtg604

Reputation: 11

How to take multiple variables that read from text files and put them into an array

Creating a Discord bot using node.js and discord.js and am fairly new to JavaScript. I have made a basic bot that works how I want it to, but am looking to find better ways to make the code efficient, yet concise. I have multiple variables that read from text files and was told that an array could help, only knowing basic arrays I couldn't really find tips on how to still make the array elements read from the specified text files.

I don't know much about arrays, so I haven't tried much. I was just told that arrays could help make my code concise.

const Discord = require("discord.js");
const fs = require('fs');
const client = new Discord.Client();
const colors = require("colors");

var help = fs.readFileSync("./text/help.txt", "utf8");
var FUN = fs.readFileSync("./text/fun.txt", "utf8");
var MEME = fs.readFileSync("./text/meme.txt", "utf8");
var hello = fs.readFileSync("./text/hello.txt", "utf8");
var GAMES = fs.readFileSync("./text/games.txt", "utf8");
var music = fs.readFileSync("./text/music.txt", "utf8");

client.on("ready", () => {
    console.log(colors.green("Connected as " + client.user.tag));
    client.user.setPresence({
        'game' : {
            'name' : 'Checking...',
            'type' : 'Playing'
        }
    })

    try {
        client.on("message", function(message) {
            var input = message.content.toUpperCase();

            if(input === "!help".toUpperCase()) {
                message.channel.send(help);
            }
            if(input === "!FUN".toUpperCase()){
                message.channel.send(FUN);
            }
            if(input === "!MEME".toUpperCase()){
                message.channel.send(MEME);
            }
            if(input === "!hello".toUpperCase()){
                message.channel.send(hello);
            }
            if(input === "!GAMES".toUpperCase()){
                message.channel.send(GAMES);
            }
            if(input === "!music".toUpperCase()){
                message.channel.send(music);
            }
        })
    }
    catch(E){
        console.log(E);
    }
})

client.login('token');

Upvotes: 1

Views: 625

Answers (1)

bntzio
bntzio

Reputation: 1324

You can make the code more concise and readable by using es6 and a bit of logic refactor.

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

const client = new Discord.Client()

const files = [
  { name: 'help', path: './text/help.txt' },
  { name: 'fun', path: './text/fun.txt' },
  { name: 'meme', path: './text/meme.txt' },
  { name: 'hello', path: './text/hello.txt' },
  { name: 'games', path: './text/games.txt' },
  { name: 'music', path: './text/music.txt' }
]

client.on('ready', () => {
  console.log(colors.green(`Connected as ${client.user.tag}`))

  client.user.setPresence({
    game: { name: 'Checking...', type: 'Playing' }
  })

  try {
    client.on('message', message => {
      const input = message.content.toUpperCase()

      const { path } = files.find(({ name }) => name.toUpperCase() === input.substring(1))
      const response = fs.readFileSync(path, 'utf8')

      message.channel.send(response)
    })
  } catch (e) {
    console.log(e)
  }
})

client.login('token')

As you can see, you get fewer lines of code and better readability.

To summarize the changes, use an array of objects to define your name and path of files, then when a message event occurs you find and read the right file by using the .find() method to compare the file name against the user input and return to the user the file content value.

You can see a good overview of es6 features in this GitHub repository.

Upvotes: 1

Related Questions