Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2. **|   PonkBot emotes
  3. **@
  4. */
  5.  
  6. 'use strict';
  7. const path = require('path')
  8. const request = require('request')
  9. const fs = require('fs')
  10. const fileType = require('file-type')
  11. const stream = require('stream')
  12. const URL = require('url')
  13.  
  14. class Emotes {
  15.   constructor(ponk) {
  16.     Object.assign(this, {
  17.       bot         : ponk   // The bot
  18.     })
  19.     this.emotespath = path.join(__dirname, '..', '..', 'emotes', 'public')
  20.     this.filenames = new Set()
  21.     const keepnames = new Set()
  22.     fs.readdirSync(this.emotespath).forEach(filename => {
  23.       const stat = fs.statSync(path.join(this.emotespath, filename))
  24.       if (stat.isFile()) this.filenames.add(filename)
  25.       else if (stat.isDirectory()) {
  26.         if (filename === 'xmas') this.xmasfilenames = fs.readdirSync(path.join(this.emotespath, 'xmas'))
  27.         else if (filename === '_bak') this.bakfilenames = fs.readdirSync(path.join(this.emotespath, '_bak'))
  28.       }
  29.     })
  30.     if (!this.bakfilenames) {
  31.       fs.mkdirSync(path.join(this.emotespath, '_bak'))
  32.       this.bakfilenames = []
  33.     }
  34.     this.bot.emotes.forEach(({ name, image }) => {
  35.       const filenname = name.slice(1).replace(/[:()<>]/g, '\\$&')
  36.       if (image.startsWith(this.bot.API.keys.emotehost)) {
  37.         const linkedname = path.basename(URL.parse(image).pathname)
  38.         const shouldname = filenname + path.extname(linkedname)
  39.         if (!this.filenames.has(linkedname) && !this.recoverEmote(linkedname)) return console.log(image)
  40.         if (shouldname != linkedname) this.renameEmote(linkedname, shouldname, false)
  41.       }
  42.       //else this.downloadEmote(filenname, image)
  43.       //fs.renameSync(path.join(this.emotespath, linkedname), path.join(this.emotespath, '_bak', linkedname))
  44.     })
  45.     fs.readdir(path.join(this.emotespath, 'xmas'), (err, filenames) => {
  46.       if (err) return console.log(err)
  47.       //console.log(filenames)
  48.     })
  49.     this.bot.client.prependListener('updateEmote', ({ name, image }) => {
  50.       const emote = this.bot.emotes.find(emote => emote.name === name)
  51.       if (!emote) this.bot.sendMessage(`Emote ${name} addiert.`)
  52.       else this.bot.sendMessage(`Emote "${name}" wurde geändert von ${emote.image} zu ${image}.pic`)
  53.       const linkedname = path.basename(URL.parse(image).pathname)
  54.       const filename = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
  55.       if (image.startsWith(this.bot.API.keys.emotehost)) {
  56.         if (!this.filenames.has(filename)) this.recoverEmote(filename)
  57.       }
  58.       //else this.downloadEmote(filename, image)
  59.     })
  60.     this.bot.client.on('removeEmote', ({ name, image, source }) => {
  61.       const linkedname = path.basename(URL.parse(image).pathname)
  62.       const filename = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
  63.       this.removeEmote(filename)
  64.     })
  65.     this.bot.client.on('renameEmote', ({ name, old, source }) => {
  66.       const emote = this.bot.emotes.find(emote => emote.name === name)
  67.       const linkedname = path.basename(URL.parse(emote.image).pathname)
  68.       const shouldname = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
  69.       const oldname = old.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
  70.       this.renameEmote(oldname, shouldname)
  71.       this.removeEmote(oldname)
  72.     })
  73.     //this.bot.client.on('emoteList',   (list)=>{ });
  74.   }
  75.   removeEmote(filename) {
  76.     if (!this.filenames.has(filename)) return
  77.     fs.renameSync(path.join(this.emotespath, filename), path.join(this.emotespath, '_bak', filename))
  78.     this.bakfilenames.push(filename)
  79.     this.filenames.delete(filename)
  80.   }
  81.   renameEmote(oldname, shouldname, add = true) {
  82.     this.removeEmote(shouldname)
  83.     fs.copyFileSync(path.join(this.emotespath, oldname), path.join(this.emotespath, shouldname))
  84.     if (add) this.filenames.add(shouldname)
  85.   }
  86.   recoverEmote(filename) {
  87.     if (!this.bakfilenames.includes(filename)) return console.log(filename + ' not found')
  88.     fs.copyFileSync(path.join(this.emotespath, '_bak', filename), path.join(this.emotespath, filename))
  89.     this.filenames.add(filename)
  90.     return true
  91.   }
  92.   downloadEmote(filename, image) {
  93.     //if (/framapic/.test(url)) return
  94.     const pass = new stream.PassThrough();
  95.     const r = request.get(image).on('error', err => {
  96.       console.error(image, err);
  97.     })
  98.     r.pipe(pass)
  99.     fileType.stream(pass).then(stream => {
  100.       filename = filename + '.' + stream.fileType.ext
  101.       stream.pipe(fs.createWriteStream(path.join(this.emotespath, filename)).on('close', () => {
  102.         this.filenames.add(filename)
  103.         console.log(filename + ' written')
  104.         fs.readFile(path.join(this.emotespath, filename), {encoding: 'base64'}, (err, data) => {
  105.           if (err) return console.log(err)
  106.           //this.bot.pushToGit(filename, data, 'base64')
  107.         })
  108.       }))
  109.     }).catch(err => console.log(err))
  110.   }
  111. }
  112.  
  113. module.exports = {
  114.   meta: {
  115.     active: true,
  116.     type: 'giggle'
  117.   },
  118.   giggle: function(ponk){
  119.     return new Promise((resolve, reject)=>{
  120.       try {
  121.         ponk.API.emotes = new Emotes(ponk);
  122.       }
  123.       catch (err) {
  124.         console.error(err)
  125.       }
  126.       //ponk.logger.log('Registering emotes');
  127.       resolve()
  128.     })
  129.   },
  130.   handlers: {
  131.     //emote: function(user, params, meta) {
  132.  
  133.     //}
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement