gaber-elsayed

addemote

Feb 8th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. exports.description = 'Adds new emote to the current server. Suported arguments: emotes, image links and image attachments';
  2. exports.usage = '<:Kappa:630103099578646558>\nhttps://static-cdn.jtvnw.net/emoticons/v1/114836/3.0';
  3. exports.level = 0;
  4. exports.perms = ['MANAGE_EMOJIS'];
  5. exports.cooldown = 5000;
  6. exports.dmable = false;
  7.  
  8. exports.run = async message => {
  9. if (!message.guild.me.hasPermission('MANAGE_EMOJIS')) throw ['botperm', 'Manage Emojis'];
  10. let url = '';
  11. let emotename = '';
  12. if (message.attachments.size){
  13. //image attachment resolver
  14. if (!message.attachments.first().width) throw ['normal', 'Attached file is not a valid image'];
  15. if (message.attachments.first().filesize > 262143) throw ['normal', `Attached image is too big: ${Math.floor(message.attachments.first().filesize / 1024)}kb > 256kb`];
  16. url = message.attachments.first().url;
  17. emotename = message.attachments.first().name.split('.')[0];
  18. }
  19. else if (message.args.length){
  20. //resolving discord emotes (from nitro users)
  21. let res = /<(a?):([a-z0-9-_]+):(\d+)>/i.exec(message.args[0]);
  22. //res[0] - whole string; res[1] - is animated; res[2] - name; res[3] - id
  23. if (res){
  24. url = `${client.options.http.cdn}/emojis/${res[3]}.${res[1].length ? 'gif' : 'png'}`;
  25. emotename = res[2];
  26. }
  27. else {
  28. //resolving URLs
  29. const fetch = require('node-fetch');
  30. await fetch(message.args[0]).catch(err => {return;throw ['fetch', err.toString()];}).then(f => { //returning in fetch errors now, those are just dank
  31. if (f ? f.headers.get('content-type').startsWith('image') : false){
  32. if (parseInt(f.headers.get('content-length')) > 262143) throw ['normal', `Requested image is too big: ${Math.floor(f.headers.get('content-length') / 1024)}kb > 256kb`];
  33. url = message.args[0];
  34. emotename = f.headers.get('content-location') ? f.headers.get('content-location').split('.')[0] : `emoji_${message.guild.emojis.cache.size+1}`;
  35. }
  36. });
  37. }
  38. }
  39. if (!url.length || !emotename.length) throw ['normal', "You have to provide one of following: emote, image link, attached image\nFor links, make sure those lead directly to image"];
  40. //got an emote, resolving extension
  41. let m = await message.channel.send('Creating emote...');
  42. await message.guild.emojis.create(url, emotename).catch(err => {throw ['discordapi', err.toString()];})
  43. .then(async emote => {
  44. let psa = `Successfully created emote \`${emote.name}\` ${emote}\nReact with ❌ within 17 seconds to remove it or change it's name with typing \`emote <new_name>\``;
  45. !m ? message.channel.send(psa) : m.edit(psa)
  46. .then(async msg => {
  47. //initializing collectors
  48. await msg.react('❌');
  49. let Rfilter = (reaction, user) => reaction.emoji.name === '❌' && user.id === message.author.id;
  50. let Rcollector = msg.createReactionCollector(Rfilter, {time:17000});
  51. //message collector for chaning new emote name
  52. let Mfilter = mesg => mesg.author.id === message.author.id && mesg.content.startsWith('emote');
  53. let Mcollector = msg.channel.createMessageCollector(Mfilter, {time:17000});
  54. //reaction collector for deleting emote
  55. Rcollector.on('collect', () => {
  56. //deleting emote
  57. emote.delete(`Responsible user: ${message.author.tag}`).catch(err => {throw ['discordapi', err.toString()];})
  58. .then(() => {
  59. message.reply(`removed emote \`${emote.name}\``);
  60. Rcollector.stop();
  61. });
  62. });
  63. Mcollector.on('collect', mess => {
  64. let newName = mess.content.slice('emote'.length).trim().split(/\s+/g)[0];
  65. if (!/^[a-z0-9-_]{2,}$/i.test(newName)) message.reply("Given name is invalid!");
  66. else if (emote){
  67. let oldName = emote.name;
  68. emote.setName(newName, `Responsible user: ${message.author.tag}`).catch(err => {throw ['discordapi', err.toString()];})
  69. .then(() => message.channel.send(`Changed name of ${emote} from \`${oldName}\` to \`${emote.name}\`.`));
  70. }
  71. });
  72. Mcollector.on('end', () => {
  73. if (m){
  74. if (emote.deleted) m.edit(`Created emote \`${emote.name}\` but deleted it afterwards`);
  75. else m.edit(`Successfully created emote \`${emote.name}\` ${emote}`);
  76. }
  77. });
  78. Rcollector.on('end', () => {
  79. if (msg) msg.reactions.cache.get('❌').users.remove();
  80. Mcollector.stop(); //also emitting end for message handler
  81. });
  82. });
  83. });
  84. }
Advertisement
Add Comment
Please, Sign In to add comment