forked from Johnson-Su/Botanist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (87 loc) · 3.6 KB
/
index.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
105
106
107
108
109
110
const Discord = require('discord.js');
const fs = require('fs');
var Scraper = require('images-scraper');
const language = require('@google-cloud/language');
const {Storage} = require('@google-cloud/storage');
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
global.XMLHttpRequest = require("xhr2");
let request = require(`request`);
const client = new Discord.Client();
const nlp_client = new language.LanguageServiceClient();
const storage = new Storage();
var config = {
apiKey: "AIzaSyCwR7ySw5QkY6r1zpFhaF3VJhAJD0yuLR4",
authDomain: "botanist-312407.firebaseapp.com",
databaseURL: "https://botanist-312407-default-rtdb.firebaseio.com/",
storageBucket: "botanist-312407.appspot.com"
};
firebase.initializeApp(config)
const db = firebase.database();
const prefix = '-';
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./modules/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./modules/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Botanist is online!');
});
async function uploadFile(attachment) {
const bucket = storage.bucket("botanist-312407.appspot.com");
var data = fs.readFileSync(`./modules/${attachment.name}`, 'UTF-8');
data = data.substr(2, data.indexOf("\n"));
const image_options = {
destination: `${attachment.name}`,
metadata: {
metadata: {
tags: data // parse first line comment, splice whitespace
}
}
}
await bucket.upload(`./modules/${attachment.name}`, image_options, function(err, file, apiResponse) {});
const bucket_options = {
includeFiles: true,
force: true
};
bucket.makePublic(bucket_options, function(err, files) {});
console.log('Uploaded a new module file!');
}
async function download(attachment){
await request.get(attachment.url)
.on('error', console.error)
.pipe(fs.createWriteStream(`./modules/${attachment.name}`));
}
client.on('message', message =>{
if(message.attachments.size > 0 && !message.author.bot){
message.channel.send("Grafting your branch...", {files: ["./graft.gif"]});
setTimeout(() => {
if(message.attachments.first().url.slice(-3) === `.js`){
async function order(){
await download(message.attachments.first());
message.channel.send("🌳 Thank you for contributing " + message.attachments.first().name);
setTimeout(() => { uploadFile(message.attachments.first()).catch(console.error);; }, 1500);
} order().catch(console.error);
} else if (message.attachments.first().url.slice(-3) != '.js'){
message.reply('Uploading graft failed not a .js file');
return;
}
}, 4000);
}
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(client, message, args, db);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
process.env.GOOGLE_APPLICATION_CREDENTIALS="./service_account.json"
var token = require('./token.js');
BOT_TOKEN = token.BOT_TOKEN;
client.login(BOT_TOKEN);