Split Image and Avatar Types

This commit is contained in:
Dragon Fire
2021-01-30 12:02:37 -05:00
parent d84d4faa8c
commit 39e5c4d02e
89 changed files with 118 additions and 101 deletions
+24
View File
@@ -0,0 +1,24 @@
const { ArgumentType } = require('discord.js-commando');
module.exports = class ImageOrAvatarArgumentType extends ArgumentType {
constructor(client) {
super(client, 'image-or-avatar');
}
async validate(value, msg, arg) {
return this.client.registry.types.get('image').validate(value, msg, arg)
|| this.client.registry.types.get('user').validate(value, msg, arg);
}
async parse(value, msg, arg) {
const image = await this.client.registry.types.get('image').parse(value, msg, arg);
if (image) return image;
const user = await this.client.registry.types.get('user').parse(value, msg, arg);
return user.displayAvatarURL({ format: 'png', size: 512 });
}
isEmpty(value, msg, arg) {
return this.client.registry.types.get('image').isEmpty(value, msg, arg)
|| this.client.registry.types.get('user').isEmpty(value, msg, arg);
}
};
+6 -7
View File
@@ -8,7 +8,7 @@ module.exports = class ImageArgumentType extends ArgumentType {
super(client, 'image');
}
async validate(value, msg, arg) {
async validate(value, msg) {
const attachment = msg.attachments.first();
if (attachment) {
if (attachment.size > 8e+6) return 'Please provide an image under 8 MB.';
@@ -24,19 +24,18 @@ module.exports = class ImageArgumentType extends ArgumentType {
return false;
}
}
return this.client.registry.types.get('user').validate(value, msg, arg);
return false;
}
async parse(value, msg, arg) {
async parse(value, msg) {
const attachment = msg.attachments.first();
if (attachment) return attachment.url;
if (fileTypeRe.test(value.toLowerCase())) return value;
const user = await this.client.registry.types.get('user').parse(value, msg, arg);
return user.displayAvatarURL({ format: 'png', size: 512 });
return null;
}
isEmpty(value, msg, arg) {
isEmpty(value, msg) {
if (msg.attachments.size) return false;
return this.client.registry.types.get('user').isEmpty(value, msg, arg);
return true;
}
};