Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- **| PonkBot emotes
- **@
- */
- 'use strict';
- const path = require('path')
- const request = require('request')
- const fs = require('fs')
- const fileType = require('file-type')
- const stream = require('stream')
- const URL = require('url')
- class Emotes {
- constructor(ponk) {
- Object.assign(this, {
- bot : ponk // The bot
- })
- this.emotespath = path.join(__dirname, '..', '..', 'emotes', 'public')
- this.filenames = new Set()
- const keepnames = new Set()
- fs.readdirSync(this.emotespath).forEach(filename => {
- const stat = fs.statSync(path.join(this.emotespath, filename))
- if (stat.isFile()) this.filenames.add(filename)
- else if (stat.isDirectory()) {
- if (filename === 'xmas') this.xmasfilenames = fs.readdirSync(path.join(this.emotespath, 'xmas'))
- else if (filename === '_bak') this.bakfilenames = fs.readdirSync(path.join(this.emotespath, '_bak'))
- }
- })
- if (!this.bakfilenames) {
- fs.mkdirSync(path.join(this.emotespath, '_bak'))
- this.bakfilenames = []
- }
- this.bot.emotes.forEach(({ name, image }) => {
- const filenname = name.slice(1).replace(/[:()<>]/g, '\\$&')
- if (image.startsWith(this.bot.API.keys.emotehost)) {
- const linkedname = path.basename(URL.parse(image).pathname)
- const shouldname = filenname + path.extname(linkedname)
- if (!this.filenames.has(linkedname) && !this.recoverEmote(linkedname)) return console.log(image)
- if (shouldname != linkedname) this.renameEmote(linkedname, shouldname, false)
- }
- //else this.downloadEmote(filenname, image)
- //fs.renameSync(path.join(this.emotespath, linkedname), path.join(this.emotespath, '_bak', linkedname))
- })
- fs.readdir(path.join(this.emotespath, 'xmas'), (err, filenames) => {
- if (err) return console.log(err)
- //console.log(filenames)
- })
- this.bot.client.prependListener('updateEmote', ({ name, image }) => {
- const emote = this.bot.emotes.find(emote => emote.name === name)
- if (!emote) this.bot.sendMessage(`Emote ${name} addiert.`)
- else this.bot.sendMessage(`Emote "${name}" wurde geändert von ${emote.image} zu ${image}.pic`)
- const linkedname = path.basename(URL.parse(image).pathname)
- const filename = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
- if (image.startsWith(this.bot.API.keys.emotehost)) {
- if (!this.filenames.has(filename)) this.recoverEmote(filename)
- }
- //else this.downloadEmote(filename, image)
- })
- this.bot.client.on('removeEmote', ({ name, image, source }) => {
- const linkedname = path.basename(URL.parse(image).pathname)
- const filename = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
- this.removeEmote(filename)
- })
- this.bot.client.on('renameEmote', ({ name, old, source }) => {
- const emote = this.bot.emotes.find(emote => emote.name === name)
- const linkedname = path.basename(URL.parse(emote.image).pathname)
- const shouldname = name.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
- const oldname = old.slice(1).replace(/[:()]/g, '\\$&') + path.extname(linkedname)
- this.renameEmote(oldname, shouldname)
- this.removeEmote(oldname)
- })
- //this.bot.client.on('emoteList', (list)=>{ });
- }
- removeEmote(filename) {
- if (!this.filenames.has(filename)) return
- fs.renameSync(path.join(this.emotespath, filename), path.join(this.emotespath, '_bak', filename))
- this.bakfilenames.push(filename)
- this.filenames.delete(filename)
- }
- renameEmote(oldname, shouldname, add = true) {
- this.removeEmote(shouldname)
- fs.copyFileSync(path.join(this.emotespath, oldname), path.join(this.emotespath, shouldname))
- if (add) this.filenames.add(shouldname)
- }
- recoverEmote(filename) {
- if (!this.bakfilenames.includes(filename)) return console.log(filename + ' not found')
- fs.copyFileSync(path.join(this.emotespath, '_bak', filename), path.join(this.emotespath, filename))
- this.filenames.add(filename)
- return true
- }
- downloadEmote(filename, image) {
- //if (/framapic/.test(url)) return
- const pass = new stream.PassThrough();
- const r = request.get(image).on('error', err => {
- console.error(image, err);
- })
- r.pipe(pass)
- fileType.stream(pass).then(stream => {
- filename = filename + '.' + stream.fileType.ext
- stream.pipe(fs.createWriteStream(path.join(this.emotespath, filename)).on('close', () => {
- this.filenames.add(filename)
- console.log(filename + ' written')
- fs.readFile(path.join(this.emotespath, filename), {encoding: 'base64'}, (err, data) => {
- if (err) return console.log(err)
- //this.bot.pushToGit(filename, data, 'base64')
- })
- }))
- }).catch(err => console.log(err))
- }
- }
- module.exports = {
- meta: {
- active: true,
- type: 'giggle'
- },
- giggle: function(ponk){
- return new Promise((resolve, reject)=>{
- try {
- ponk.API.emotes = new Emotes(ponk);
- }
- catch (err) {
- console.error(err)
- }
- //ponk.logger.log('Registering emotes');
- resolve()
- })
- },
- handlers: {
- //emote: function(user, params, meta) {
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement