This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (112 loc) · 4.87 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Discord = require('discord.js');
const client = new Discord.Client();
require('dotenv').config();
const fs = require('fs');
const request = require('request');
const { Client } = require('pg');
const database = new Client({
connectionString: process.env.DATABASE_URL,
ssl: false
});
var schedule = require('node-schedule');
database.connect();
database.query(`
CREATE TABLE IF NOT EXISTS app_command (id serial PRIMARY KEY, aliases VARCHAR(100), help VARCHAR(1000), function VARCHAR(100000));
CREATE TABLE IF NOT EXISTS app_listener (id serial PRIMARY KEY, channel VARCHAR(100), function VARCHAR(100000));
CREATE TABLE IF NOT EXISTS app_task (id serial PRIMARY KEY, description VARCHAR(500), trigger VARCHAR(100), function VARCHAR(100000));
`);
database.query(`
INSERT INTO app_command (aliases, help, function) SELECT 'channel', $1::text, $2::text WHERE NOT EXISTS (SELECT aliases FROM app_command WHERE aliases = 'channel');
`, ['```\nchannel export {destination} - Exports the history of the current channel to the specified destination.\nchannel scrub [messages] - Deletes the amount of specified messages. If no number is specified, then the last 100 messages will be deleted.\nchannel private - Prevents @everyone from viewing the channel.\nchannel reset - Completely resets a channel\'s permissions.\nchannel invite {user1} [user2] [user3] - Enables read access on the current channel for the specified user(s).\nchannel remove {user1} [user2] [user3] - Deletes the amount of specified messages.\nchannel list [displaynames|ids] - Lists the specified values that are present in the channel.\n```', fs.readFileSync(__dirname + '/commands/channel.txt')]);
database.query(`
INSERT INTO app_command (aliases, help, function) SELECT 'help', $1::text, $2::text WHERE NOT EXISTS (SELECT aliases FROM app_command WHERE aliases = 'help');
`, ['Runs the help command.', fs.readFileSync(__dirname + '/commands/help.txt')]);
database.query(`
INSERT INTO app_command (aliases, help, function) SELECT 'message', $1::text, $2::text WHERE NOT EXISTS (SELECT aliases FROM app_command WHERE aliases = 'message');
`, ['Sends a message to everyone with the first argument, role name.', fs.readFileSync(__dirname + '/commands/message.txt')]);
client.on('ready', () => {
client.user.setPresence({
game: {
name: (process.env.PREFIX || 's!') + ' help | github.com/DDynamic/Stack'
}
});
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content.toLowerCase().startsWith(process.env.PREFIX.toLowerCase() || 's!')) {
if (msg.guild) {
var content = msg.content.substring(process.env.PREFIX.length || 2).trim().split(' ');
var invoke = content[0].toLowerCase();
var args = content.slice(1);
database.query('SELECT * FROM app_command', (err, res) => {
var found = false;
var commands = res.rows;
for (command in commands) {
var command = commands[command];
var aliases = command['aliases'].split(', ');
for (alias in aliases) {
if (aliases[alias].toLowerCase() == invoke.toLowerCase()) {
found = true;
try {
eval(command['function']);
} catch (err) {
console.log(err);
msg.reply('an error occured when executing that command. Check the console for more information.');
}
}
}
}
if (!found) {
msg.reply('that command does not exist. Refer to command help by running: `'+ (process.env.PREFIX || 's!') + ' help`');
}
});
} else {
msg.author.send('Commands can only be run in a channel on the discord server.');
}
}
if (msg.guild) {
database.query('SELECT * FROM app_listener', (err, res) => {
var found = false;
var listeners = res.rows;
for (listener in listeners) {
var listener = listeners[listener];
if (listener['channel'] == msg.channel.name) {
try {
eval(listener['function']);
} catch (err) {
console.log(err);
msg.reply('an error occured when executing a listener. Check the console for more information.');
}
}
}
});
}
});
jobs = []
setInterval(function() {
database.query('SELECT * FROM app_task', (err, res) => {
for (let job in jobs) {
job = jobs[job];
if (job) {
job.cancel();
}
}
jobs = [];
tasks = res.rows;
for (task in tasks) {
task = tasks[task];
var job = schedule.scheduleJob(task['trigger'], function() {
try {
eval(task['function']);
} catch (err) {
console.log(err);
}
});
jobs.push(job)
}
});
}, (process.env.TASK_TIME || 60000));
process.on('unhandledRejection', (reason) => {
console.error(reason.toString());
});
client.login(process.env.TOKEN);