jeudi 31 janvier 2019

How do I change this if statement and stop repeating forever?

My current bot works by using app.js to call commands from the /commands directory, and uses data.js to hold "global data" (eg users typed !p are added to and stored in export.players[] in data.js)

I have the below snippet of code (pl.js) that adds a list of users with the @Participating role by their discord nickname inside a RichEmbed.

It works okay, but because of the if requires message.content, and I already have a global prefix set; it requires command input more than once to start, and then repeats the entire embed output until the bot is rebooted.

Is there a way to change my if statement to start the code without checking the message.content? or to work if the command is called through app.js?

and how can I make it post once per command/stop it repeating?

pl.js

const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
const client = new Discord.Client()

// Calling Global Data
var data = require('./../data.js');

    exports.run = (client, message, args) => {

      client.on("message", message => {

          if(message.content == `!pl`) {
              const ListEmbed = new Discord.RichEmbed()
                  .setTitle('Participants:')
                  .setDescription(message.guild.roles.get('###############').members.map(m=>m.user.username));
              message.channel.send(ListEmbed);

            }
        }
    )}

app.js

const Discord = require("discord.js");
const Enmap = require("enmap");
const fs = require("fs");  
const client = new Discord.Client();
const config = require("./config.json");


client.config = config;

fs.readdir("./events/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    const event = require(`./events/${file}`);
    let eventName = file.split(".")[0];
    client.on(eventName, event.bind(null, client));
  });
});

client.commands = new Enmap();

fs.readdir("./commands/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    if (!file.endsWith(".js")) return;
    let props = require(`./commands/${file}`);
    let commandName = file.split(".")[0];
    console.log(`Attempting to load command ${commandName}`);
    client.commands.set(commandName, props);
  });
});



client.login(config.token);

Aucun commentaire:

Enregistrer un commentaire