Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnl committed May 28, 2021
1 parent 1114e7e commit 6c2ce01
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 223 deletions.
4 changes: 4 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DISCORD_BOT_TOKEN=
UPLOAD_DIR=public/
HTACCESS_PATH=public/.htaccess
MEME_CHANNEL_ID=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
public
.env
29 changes: 15 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Client } from 'discord.js'
import dotenv from 'dotenv'
import fs from 'fs/promises'
import fs from 'fs'
import fsp from 'fs/promises'
import path from 'path'
import axios from 'axios'

dotenv.config()

Expand All @@ -10,15 +12,13 @@ const client = new Client()
// kiitti hegez
const alphanum = (s) => s.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-zA-Z0-9_*+=()-]/g, '-')
.replace(/[^a-zA-Z0-9_*+=()-\.]/g, '-')
.split('-')
.filter((x) => x !== '')
.join('-')

const splitFileName = (str) => str.match(/([0-9a-z]+)\.([0-9a-z]+)$/)

client
.on('ready', console.log('nyt meni kaatisbotti päälle'))
.on('ready', () => console.log('nyt meni kaatisbotti päälle'))

.on('message', (msg) => {
if (msg.channel.id !== process.env.MEME_CHANNEL_ID) return
Expand All @@ -27,19 +27,20 @@ client
if (!attachment || !attachment.width || !attachment.height) return
if (attachment.size > 8e6) return

const splitResult = splitFileName(attachment.name)
if (!splitResult) return
const [, fileName, fileExtension] = splitResult

const prettyFileName = `${alphanum(fileName)}.${alphanum(fileExtension)}`
const prettyFileName = `${alphanum(attachment.name)}`
if (prettyFileName.length > 30) return

const filePath = path.join(process.env.UPLOAD_DIR, fileName)
const filePath = path.join(process.env.UPLOAD_DIR, prettyFileName)

fs.access(filePath)
fsp.access(filePath)
.then(() => msg.author.send('Tämän niminen tiedosto on jo lähetetty kaatikseen!'))
.catch(() => fs.writeFile(prettyFileName, attachment.attachment)
.then(() => fs.appendFile(process.env.HTACCESS_PATH,
.catch(() => axios({
method: "get",
url: attachment.attachment,
responseType: "stream"
})
.then((response) => response.data.pipe(fs.createWriteStream(filePath)))
.then(() => fsp.appendFile(process.env.HTACCESS_PATH,
`AddDescription ${prettyFileName} ${alphanum(msg.author.username)}-${msg.author.discriminator}`)))
})

Expand Down
Loading

0 comments on commit 6c2ce01

Please sign in to comment.