Erik Dieterich
Erik Dieterich

Reputation: 17

How to store a user's message as a variable?

I have a pretty simple bot I'm working on for me and my friend's server. I set up a simple web-scrape from a guide I was following to pull gifs off dogpile image search in response to a prefixed command (!).

I've been trying to edit the code to allow the prefix ! followed by any term to return a gif of that term they used instead of writing the code for each command specifically.

I figured if I could replace the string 'test' with a variable of !<variable> I could enter that into the code I already have but I can't seem to figure it out. Any guidance would be very much appreciated!

bot.on('message', message => {
  let args = message.content.substring(PREFIX.length).split(" ");

  switch (args[0]) {
    case 'test':
      test(message);
      break;
  }
});

function test(message) {
  var options = {
    url: "https://results.dogpile.com/serp?qc=images&q=" + "test gif",
    method: "GET",
    headers: {
      "Accept": "test/html",
      "User-Agent": "Chrome"
    }
  };

  request(options, function(error, response, responseBody) {
    if (error) {
      return;
    }
    $ = cheerio.load(responseBody);
    var links = $(".image a.link");
    var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));

    if (!urls.length) {
      return;
    }
    // Send result
    message.channel.send(urls[Math.floor(Math.random() * urls.length)]);
  });

}

Upvotes: 1

Views: 187

Answers (2)

Federico Grandi
Federico Grandi

Reputation: 6816

You can just use the content of the message (excluding the prefix) as the string for your query.
Notice that if you're not using commands you can simply run everything inside the message handler.

bot.on('message', message => {
  // Always ignore unwanted messages
  if (message.author.bot || !message.startsWith(PREFIX)) return;

  let query = message.content.slice(PREFIX.length).trim()

  options = {
    // Add the query string like this:
    url : "https://results.dogpile.com/serp?qc=images&q=" + query + "gif",
    ...
  }

  // You can then use your existing code
  request(...)
})

Upvotes: 1

Vinicius
Vinicius

Reputation: 324

Instead of replacing the 'test' string you can use it as a command name and then use the args variable for the search term like this: !test <search term>. To do that you just need to define that args[0] is the command name and then pass the rest of the arguments to the test function along with the message

let args = message.content.slice(PREFIX.length).trim().split(" ");
let commandName = args.shift().toLowerCase(); // Use args[0] as the command name

switch(commandName) {
   case 'test':
     test(message, args);
    break;
}

function test(message, args) {
    let [...term] = args;

    var options = {
        url : "https://results.dogpile.com/serp?qc=images&q=" + term.join(' '),
        method: "GET",
        headers: {
            "Accept": "test/html",
            "User-Agent": "Chrome"
        }
    };

    request(options, function(error, response, responseBody) {
        if (error) {
           return;
       }
       $ = cheerio.load(responseBody);
       var links = $(".image a.link");
       var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));
       
       if (!urls.length) {
           return;
       }
       // Send result
       message.channel.send(urls[Math.floor(Math.random() * urls.length)]);
   });
}  

Upvotes: 0

Related Questions