Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { ArgumentType } = require('discord.js-commando');
- const fileType = require('file-type');
- const fetch = require('node-fetch');
- let buffer;
- /**
- * ImageArgumentType for discord.js-commando
- * returns a buffer usable for MessageAttachment
- */
- class ImageArgumentType extends ArgumentType {
- constructor(client) {
- super(client, 'image');
- }
- async validate(val, msg, arg) {
- const matches = val ? val.match(/^(?:http(s)?:\/\/)\s?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gi) : null;
- const mime = ['image/jpeg', 'image/jpg', 'image/png'];
- if (val && msg.attachments.size > 0) {
- // const message = await msg.channel.messages.get(msg.author.lastMessageID);
- const attachment = msg.attachments.first();
- buffer = await fetch(attachment.url).then(res => res.buffer())
- const type = fileType(buffer)
- if (type === null) {
- return "You provided an invalid image. Please try again."
- }
- const check = mime.find(n => n === type.mime)
- if (check) return true
- }
- const message = await msg.channel.messages.get(msg.author.lastMessageID);
- if (message.attachments.size > 0) {
- const attachment = message.attachments.first();
- buffer = await fetch(attachment.url).then(res => res.buffer());
- const type = fileType(buffer)
- if (type === null) {
- return "You provided an invalid image. Please try again."
- }
- const check = mime.find(n => n === type.mime)
- if (check) return true
- }
- if (matches) {
- buffer = await fetch(val).then(res => res.buffer())
- const type = fileType(buffer)
- if (type === null) {
- return "You provided an invalid image url. Please try again."
- }
- const check = mime.find(n => n === type.mime)
- if (check) return true
- }
- return false;
- }
- async parse(val, msg) {
- return buffer;
- }
- isEmpty(val, msg, arg) {
- if (msg.attachments.size) return false;
- // if (!match) return !val
- if (Array.isArray(val)) return val.length === 0;
- return !val;
- }
- };
- module.exports = ImageArgumentType;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement