-
Notifications
You must be signed in to change notification settings - Fork 0
/
emblem.js
104 lines (91 loc) · 3.54 KB
/
emblem.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//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
console.log(env.logfile)
const config = require("./config/botconfig.json")
const emblem_info = require("./assets/map.json")
image_folder = resolve(__dirname,'./assets/emblem-compiled/') + path.sep
const Discord = require("discord.js")
const client = new Discord.Client({disableEveryone: true})
var image = 0
process.env.GOOGLE_APPLICATION_CREDENTIALS = env.transate_creds_path
const {Translate} = require('@google-cloud/translate').v2
const GT = new Translate();
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()
}, 15000)
})
async function translate(text){
options = {
from: 'ja',
to: 'en'
};
translateResult = await GT.translate(text, options).catch(error => console.log(error))
translatedtext = translateResult[0]
console.log(`translated emblem: ${translatedtext}`)
return translatedtext
}
//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.emblem_log).toString('utf-8')
defaults_log = fs.readFileSync(env.emblem_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.emblem_log, image_array.map(item => item + '\r\n').join().replace(/,/g, ''), (err) => {
if(err) throw err
})
fs.writeFile(env.emblem_today, image, (err) => {
if(err) throw err
})
} else {
image = defaults_array.shift()
fs.writeFile(env.emblem_log, defaults_array.map(item => item + '\r\n').join().replace(/,/g, ''), (err) => {
if(err) throw err
})
const warn_reset = "Smith emblem 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}`)
original_text = emblem_info[image].name
translatedtext = await translate(original_text)
await client.channels.cache.get(env.output_channel).send(`Here's today's SP emblem.\nTitle: ${original_text}\n(${translatedtext})`, {
files: [image_folder+image+".png"]
}).catch(error => console.log(`Couldn't post because of: ${error}`))
} else {
console.log(`Not attempting to post image: ${image}`)
}
}
client.login(config.token)