2020-02-21 00:26:49 +00:00
|
|
|
const gm = require("gm");
|
2020-05-20 18:24:57 +00:00
|
|
|
const cron = require("cron");
|
2020-02-27 15:49:32 +00:00
|
|
|
const { promisify } = require("util");
|
2019-09-13 20:02:41 +00:00
|
|
|
const client = require("../utils/client.js");
|
|
|
|
const database = require("../utils/database.js");
|
2020-04-26 21:55:33 +00:00
|
|
|
const collections = require("../utils/collections.js");
|
2019-09-13 20:02:41 +00:00
|
|
|
const logger = require("../utils/logger.js");
|
2019-11-13 00:09:06 +00:00
|
|
|
const messages = require("../messages.json");
|
2019-09-13 20:02:41 +00:00
|
|
|
const misc = require("../utils/misc.js");
|
2020-06-27 17:18:26 +00:00
|
|
|
const soundPlayer = require("../utils/soundplayer.js");
|
2020-04-10 02:40:52 +00:00
|
|
|
const helpGenerator =
|
|
|
|
process.env.OUTPUT !== "" ? require("../utils/help.js") : null;
|
|
|
|
const twitter =
|
|
|
|
process.env.TWITTER === "true" ? require("../utils/twitter.js") : null;
|
2019-09-13 20:02:41 +00:00
|
|
|
|
|
|
|
// run when ready
|
|
|
|
module.exports = async () => {
|
2020-07-02 15:33:27 +00:00
|
|
|
// add gm extensions
|
|
|
|
gm.prototype.writePromise = promisify(gm.prototype.write);
|
|
|
|
gm.prototype.streamPromise = promisify(gm.prototype.stream);
|
|
|
|
gm.prototype.sizePromise = promisify(gm.prototype.size);
|
|
|
|
gm.prototype.identifyPromise = promisify(gm.prototype.identify);
|
|
|
|
gm.prototype.bufferPromise = function(format, delay, type) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.in(
|
|
|
|
delay ? "-delay" : "",
|
|
|
|
delay ? delay.split("/").reverse().join("x") : ""
|
|
|
|
)
|
|
|
|
.out(
|
|
|
|
type !== "sonic" ? "-layers" : "",
|
|
|
|
type !== "sonic" ? "OptimizeTransparency" : ""
|
|
|
|
)
|
|
|
|
.out(type !== "sonic" ? "-fuzz" : "", type !== "sonic" ? "2%" : "")
|
|
|
|
.out("+profile", "xmp")
|
|
|
|
.out("-limit", "memory", "64MB")
|
|
|
|
.out("-limit", "map", "128MB")
|
|
|
|
.stream(format, (err, stdout, stderr) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
const chunks = [];
|
|
|
|
stdout.on("data", (chunk) => {
|
|
|
|
chunks.push(chunk);
|
|
|
|
});
|
|
|
|
// these are 'once' because they can and do fire multiple times for multiple errors,
|
|
|
|
// but this is a promise so you'll have to deal with them one at a time
|
|
|
|
stdout.once("end", () => {
|
|
|
|
resolve(Buffer.concat(chunks));
|
|
|
|
});
|
|
|
|
stderr.once("data", (data) => {
|
|
|
|
reject(data.toString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// connect to lavalink
|
|
|
|
if (!soundPlayer.status) await soundPlayer.connect();
|
|
|
|
|
2019-09-13 20:02:41 +00:00
|
|
|
// make sure settings/tags exist
|
2019-12-16 18:13:38 +00:00
|
|
|
for (const [id] of client.guilds) {
|
2020-07-10 17:07:24 +00:00
|
|
|
const guildDB = await database.query("SELECT * FROM guilds WHERE guild_id = $1", [id]);
|
|
|
|
if (guildDB.rows.length === 0) {
|
2019-12-29 16:56:32 +00:00
|
|
|
logger.log(`Registering guild database entry for guild ${id}...`);
|
2020-07-10 17:07:24 +00:00
|
|
|
await database.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled) VALUES ($1, $2, $3, $4, $5)", [id, misc.tagDefaults, "&", {}, []]);
|
2020-02-27 15:49:32 +00:00
|
|
|
}
|
2019-10-28 20:21:06 +00:00
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
|
2020-05-20 18:24:57 +00:00
|
|
|
const job = new cron.CronJob("0 0 * * 0", async () => {
|
|
|
|
logger.log("Deleting stale guild entries in database...");
|
2020-07-10 17:07:24 +00:00
|
|
|
const guildDB = await database.query("SELECT * FROM guilds");
|
|
|
|
for (const { guild_id } of guildDB.rows) {
|
|
|
|
if (!client.guilds.get(guild_id)) {
|
|
|
|
await database.query("DELETE FROM guilds WHERE guild_id = $1", [guild_id]);
|
|
|
|
logger.log(`Deleted entry for guild ID ${guild_id}.`);
|
2020-05-20 18:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.log("Finished deleting stale entries.");
|
|
|
|
});
|
|
|
|
job.start();
|
|
|
|
|
2020-07-10 17:07:24 +00:00
|
|
|
let counts;
|
|
|
|
try {
|
|
|
|
counts = await database.query("SELECT * FROM counts");
|
|
|
|
} catch {
|
|
|
|
counts = { rows: [] };
|
|
|
|
}
|
|
|
|
if (!counts.rows[0]) {
|
2020-04-26 21:55:33 +00:00
|
|
|
for (const command of collections.commands.keys()) {
|
2020-07-10 17:07:24 +00:00
|
|
|
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
2020-04-26 21:55:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const command of collections.commands.keys()) {
|
2020-07-10 17:07:24 +00:00
|
|
|
const count = await database.query("SELECT * FROM counts WHERE command = $1", [command]);
|
|
|
|
if (!count) {
|
|
|
|
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
2020-04-26 21:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:40:09 +00:00
|
|
|
// generate docs
|
2020-07-06 20:19:30 +00:00
|
|
|
if (helpGenerator) await helpGenerator(process.env.OUTPUT);
|
2019-12-05 17:40:09 +00:00
|
|
|
|
2019-09-13 20:02:41 +00:00
|
|
|
// set activity (a.k.a. the gamer code)
|
|
|
|
(async function activityChanger() {
|
2020-02-26 01:57:44 +00:00
|
|
|
client.editStatus("dnd", {
|
2020-04-10 02:40:52 +00:00
|
|
|
name: `${misc.random(messages)} | @esmBot help`,
|
2020-02-26 01:57:44 +00:00
|
|
|
});
|
2019-09-13 20:02:41 +00:00
|
|
|
setTimeout(activityChanger, 900000);
|
|
|
|
})();
|
|
|
|
|
2019-11-15 16:59:50 +00:00
|
|
|
// tweet stuff
|
2019-11-23 23:23:28 +00:00
|
|
|
if (twitter !== null && twitter.active === false) {
|
2020-03-14 23:03:45 +00:00
|
|
|
const blocks = await twitter.client.blocks.ids();
|
2019-11-18 01:57:12 +00:00
|
|
|
const tweet = async () => {
|
2020-07-10 17:07:24 +00:00
|
|
|
const tweetContent = await misc.getTweet(twitter.tweets);
|
2019-11-23 23:23:28 +00:00
|
|
|
try {
|
2020-03-14 23:03:45 +00:00
|
|
|
const info = await twitter.client.statuses.update(tweetContent);
|
|
|
|
logger.log(`Tweet with id ${info.id_str} has been posted.`);
|
|
|
|
// with status code ${info.resp.statusCode} ${info.resp.statusMessage}
|
2019-11-23 23:23:28 +00:00
|
|
|
} catch (e) {
|
|
|
|
const error = JSON.stringify(e);
|
|
|
|
if (error.includes("Status is a duplicate.")) {
|
|
|
|
logger.log("Duplicate tweet, will retry in 30 minutes");
|
|
|
|
} else {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 01:57:12 +00:00
|
|
|
};
|
|
|
|
tweet();
|
|
|
|
setInterval(tweet, 1800000);
|
2019-11-23 23:23:28 +00:00
|
|
|
twitter.active = true;
|
2020-07-07 19:28:16 +00:00
|
|
|
try {
|
|
|
|
const stream = twitter.client.statuses.filter(`@${process.env.HANDLE}`);
|
|
|
|
stream.on("data", async (tweet) => {
|
2020-04-10 02:40:52 +00:00
|
|
|
if (
|
2020-07-07 19:28:16 +00:00
|
|
|
tweet.user.screen_name !== "esmBot_" &&
|
|
|
|
!blocks.ids.includes(tweet.user.id_str)
|
|
|
|
) {
|
|
|
|
let tweetContent;
|
2020-07-10 17:07:24 +00:00
|
|
|
if (new RegExp(["@this_vid", "@DownloaderBot", "GetVideoBot", "@thisvid_"].join("|")).test(tweet.text)) {
|
|
|
|
tweetContent = await misc.getTweet(twitter.tweets, true, true);
|
2020-07-07 19:28:16 +00:00
|
|
|
} else {
|
2020-07-10 17:07:24 +00:00
|
|
|
tweetContent = await misc.getTweet(twitter.tweets, true).replace(/{{user}}/gm, `@${tweet.user.screen_name}`);
|
2020-07-07 19:28:16 +00:00
|
|
|
}
|
|
|
|
const payload = {
|
|
|
|
status: `@${tweet.user.screen_name} ${tweetContent}`,
|
|
|
|
in_reply_to_status_id: tweet.id_str,
|
|
|
|
};
|
|
|
|
const info = await twitter.client.statuses.update(payload);
|
|
|
|
logger.log(`Reply with id ${info.id_str} has been posted.`);
|
2020-03-14 23:03:45 +00:00
|
|
|
// with status code ${info.resp.statusCode} ${info.resp.statusMessage}
|
2020-07-07 19:28:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`The Twitter streaming API ran into an error: ${e}`);
|
|
|
|
}
|
2019-11-15 16:59:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 02:40:52 +00:00
|
|
|
logger.log(
|
|
|
|
"info",
|
|
|
|
`Successfully started ${client.user.username}#${client.user.discriminator} with ${client.users.size} users in ${client.guilds.size} servers.`
|
|
|
|
);
|
|
|
|
};
|