简体   繁体   中英

How would you go about where a user types in {prefix}reddit {subreddit} (then replies a random image from that subreddit)

So far I got this that pulls images from set subreddits using random-puppy. I will change meme to reddit for the cmd. Plus how would you go about restricting people from pulling images from 18+ subreddit if the channel is not marked NSFW

const randomPuppy = require('random-puppy');

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

  let reddit = [
    "meme",
    "animemes",
    "MemesOfAnime",
    "animememes",
    "AnimeFunny",
    "dankmemes",
    "dankmeme",
    "wholesomememes",
    "MemeEconomy",
    "techsupportanimals",
    "meirl",
    "me_irl",
    "2meirl4meirl",
    "AdviceAnimals"
  ]

  let subreddit = reddit[Math.floor(Math.random() * reddit.length)];

  message.channel.startTyping();

  randomPuppy(subreddit).then(async url => {
    await message.channel.send({
      files: [{
        attachment: url,
        name: 'meme.png'
      }]
    }).then(() => message.channel.stopTyping());
  }).catch(err => console.error(err));

};
exports.help = {
    name: 'meme',
    aliases: [],
    description: 'What can I say ͡°-͜ʖ-͡°',
    usage: 'meme'
};

Instead of defining an array of subreddits and selecting a random one, you can simply use the user provided argument.

const randomPuppy = require('random-puppy');

exports.run = async (client, message, args) => {
  // Assuming args[1] is the name of the subreddit.
  try {
    if (!args[1]) {
      // Insert your code for choosing from a random subreddit.
    } else {
      if (args[2]) return await message.channel.send('Too many arguments.');

      const imgURL = await randomPuppy(args[1]);

      await message.channel.send({
        files: [{
          attachment: imgURL,
          name: 'image.png'
        }];
      });
    }
  } catch(err) {
    console.error(err);
  }
};

exports.help = {
  name: 'reddit',
  aliases: [],
  description: 'Retrieve a random picture from a subreddit',
  usage: 'meme'
};

As for the restriction of NSFW content, you'd probably have to scan the image with some sort of nudity/NSFW detection system. To check if the channel is marked as NSFW, you can use...

if (message.channel.nsfw === false) console.log('Not an NSFW channel'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM