-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_memory.js
87 lines (74 loc) · 2.89 KB
/
daily_memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//The environment string loads the corresponding .env file from the config folder.
//It controls the output channel and log file.
//Current options are 'production' or 'test'
environment = 'production'
require('./consoleTimestamp')()
const {resolve} = require('path')
path = require('path')
env = require('dotenv').config({path: resolve(__dirname,`./config/${environment}.env`)}).parsed
const config = require("./config/botconfig.json")
image_folder = resolve(__dirname,'./assets/memories/output/') + path.sep
const Discord = require("discord.js")
const client = new Discord.Client({disableEveryone: true})
var image = 0
const channel_general = '291802168372101120'
const channel_cammy_server = '673516599851876384'
client.on("ready", async ready => {
console.log("ready")
//client.channels.cache.get(channel_general).send("YES UwU")
loggedImages()
setTimeout(function(){ // Logout and quit after 20s
client.destroy()
}, 20000)
})
//logs values recorded
async function loggedImages() {
try {
image = await getValue()
if (image) {
await post_image(image)
}
} catch(err) {
console.log(err)
}
}
function getValue(){
fs = require('fs')
imagelog = fs.readFileSync(env.memories_log).toString('utf-8')
defaults_log = fs.readFileSync(env.memories_defaults).toString('utf-8')
image_array = imagelog.replace(/\r\n|\n/g, ',').split(",").filter(Boolean)
defaults_array = defaults_log.replace(/\r\n|\n/g, ',').split(",").filter(Boolean)
image = image_array.shift()
if (image) {
fs.writeFile(env.memories_log, image_array.map(item => item + '\r\n').join().replace(/,/g, ''), (err) => {
if(err) throw err
})
fs.writeFile(env.memory_today, image, (err) => {
if(err) throw err
})
} else {
image = defaults_array.shift()
fs.writeFile(env.memories_log, defaults_array.map(item => item + '\r\n').join().replace(/,/g, ''), (err) => {
if(err) throw err
})
const warn_reset = "Smith memories log was reset."
if (environment === 'production') {
client.channels.cache.get(channel_cammy_server).send(warn_reset)
}
// console.log(`image = ${image}`)
console.log(warn_reset)
return image
}
return image
}
async function post_image(image) {
if (image) { // if image is not 'undefined', which happens if you run out of images
console.log(`Posting: ${image}`)
await client.channels.cache.get(env.output_channel).send("Good day, foodbeat players. Here is today's memory.", {
files: [image_folder+image]
}).catch(error => console.log(`Couldn't post because of: ${error}`))
} else {
console.log(`Not attempting to post image: ${image}`)
}
}
client.login(config.token)