aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/commands/owner/eval.ts16
-rw-r--r--src/lib/extensions/BotClient.ts17
-rw-r--r--src/lib/extensions/BotTask.ts6
-rw-r--r--src/tasks.ts41
-rw-r--r--src/tasks/Unban.ts49
5 files changed, 74 insertions, 55 deletions
diff --git a/src/commands/owner/eval.ts b/src/commands/owner/eval.ts
index 2d5eb2d..0b793c9 100644
--- a/src/commands/owner/eval.ts
+++ b/src/commands/owner/eval.ts
@@ -66,7 +66,7 @@ export default class EvalCommand extends BotCommand {
config = this.client.config,
sh = promisify(exec),
models = this.client.db.models,
- got = await import('got');
+ got = require('got'); // eslint-disable-line @typescript-eslint/no-var-requires
output = eval(code);
output = await output;
if (typeof output !== 'string') output = inspect(output, { depth });
@@ -81,14 +81,14 @@ export default class EvalCommand extends BotCommand {
'📥 Input',
code.length > 1012
? 'Too large to display. Hastebin: ' +
- (await this.client.util.haste(code))
+ (await this.client.util.haste(code))
: '```js\n' + code + '```'
)
.addField(
'📤 Output',
output.length > 1012
? 'Too large to display. Hastebin: ' +
- (await this.client.util.haste(output))
+ (await this.client.util.haste(output))
: '```js\n' + output + '```'
)
.setColor('#66FF00')
@@ -104,18 +104,18 @@ export default class EvalCommand extends BotCommand {
'📥 Input',
code.length > 1012
? 'Too large to display. Hastebin: ' +
- (await this.client.util.haste(code))
+ (await this.client.util.haste(code))
: '```js\n' + code + '```'
)
.addField(
'📤 Output',
e.length > 1012
? 'Too large to display. Hastebin: ' +
- (await this.client.util.haste(e))
+ (await this.client.util.haste(e))
: '```js\n' +
- e +
- '```Full stack:' +
- (await this.client.util.haste(e.stack))
+ e +
+ '```Full stack:' +
+ (await this.client.util.haste(e.stack))
)
.setColor('#FF0000')
.setFooter(
diff --git a/src/lib/extensions/BotClient.ts b/src/lib/extensions/BotClient.ts
index bd14fd2..0b59675 100644
--- a/src/lib/extensions/BotClient.ts
+++ b/src/lib/extensions/BotClient.ts
@@ -2,14 +2,14 @@ import {
AkairoClient,
CommandHandler,
InhibitorHandler,
- ListenerHandler
+ ListenerHandler,
+ TaskHandler
} from 'discord-akairo';
import { Guild } from 'discord.js';
import * as path from 'path';
import { Sequelize } from 'sequelize';
import * as Models from '../models';
import { Util } from './Util';
-import * as Tasks from '../../tasks';
import { exit } from 'process';
import { Intents } from 'discord.js';
import * as config from '../../config/options';
@@ -23,6 +23,7 @@ export class BotClient extends AkairoClient {
public listenerHandler: ListenerHandler;
public inhibitorHandler: InhibitorHandler;
public commandHandler: CommandHandler;
+ public taskHandler: TaskHandler;
public util: Util;
public ownerID: string[];
public db: Sequelize;
@@ -57,6 +58,11 @@ export class BotClient extends AkairoClient {
automateCategories: true
});
+ // Create task handler
+ this.taskHandler = new TaskHandler(this, {
+ directory: path.join(__dirname, "..", "..", "tasks"),
+ });
+
// Create command handler
this.commandHandler = new CommandHandler(this, {
directory: path.join(__dirname, '..', '..', 'commands'),
@@ -110,7 +116,8 @@ export class BotClient extends AkairoClient {
const loaders = {
commands: this.commandHandler,
listeners: this.listenerHandler,
- inhibitors: this.inhibitorHandler
+ inhibitors: this.inhibitorHandler,
+ tasks: this.taskHandler
};
for (const loader of Object.keys(loaders)) {
try {
@@ -126,10 +133,8 @@ export class BotClient extends AkairoClient {
);
}
}
+ this.taskHandler.startAll();
await this.dbPreInit();
- Object.keys(Tasks).forEach((t) => {
- setInterval(() => Tasks[t](this), 30000);
- });
}
public async dbPreInit(): Promise<void> {
diff --git a/src/lib/extensions/BotTask.ts b/src/lib/extensions/BotTask.ts
new file mode 100644
index 0000000..c0da5d3
--- /dev/null
+++ b/src/lib/extensions/BotTask.ts
@@ -0,0 +1,6 @@
+import { Task } from 'discord-akairo';
+import { BotClient } from './BotClient';
+
+export class BotTask extends Task {
+ public client: BotClient;
+} \ No newline at end of file
diff --git a/src/tasks.ts b/src/tasks.ts
deleted file mode 100644
index d728636..0000000
--- a/src/tasks.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import chalk from 'chalk';
-import { DiscordAPIError } from 'discord.js';
-import { Op } from 'sequelize';
-import { BotClient } from './lib/extensions/BotClient';
-import { Ban } from './lib/models';
-
-export const BanTask = async (client: BotClient): Promise<void> => {
- const rows = await Ban.findAll({
- where: {
- [Op.and]: [
- {
- expires: {
- [Op.lt]: new Date() // Find all rows with an expiry date before now
- }
- }
- ]
- }
- });
- client.logger.verbose(
- chalk.cyan(`Queried bans, found ${rows.length} expired bans.`)
- );
- for (const row of rows) {
- const guild = client.guilds.cache.get(row.guild);
- if (!guild) {
- await row.destroy();
- continue;
- }
- try {
- await guild.members.unban(
- row.user,
- `Unbanning user because tempban expired`
- );
- } catch (e) {
- if (e instanceof DiscordAPIError) {
- // Member not banned, ignore
- } else throw e;
- }
- await row.destroy();
- client.logger.verbose(chalk.cyan('Unbanned user'));
- }
-};
diff --git a/src/tasks/Unban.ts b/src/tasks/Unban.ts
new file mode 100644
index 0000000..a59933a
--- /dev/null
+++ b/src/tasks/Unban.ts
@@ -0,0 +1,49 @@
+import chalk from "chalk";
+import { DiscordAPIError } from "discord.js";
+import { Op } from "sequelize";
+import { BotTask } from "../lib/extensions/BotTask"
+import { Ban } from "../lib/models";
+
+export default class UnbanTask extends BotTask {
+ constructor() {
+ super('unban', {
+ delay: 30_000, // 1/2 min
+ runOnStart: true
+ })
+ }
+ async exec(): Promise<void> {
+ const rows = await Ban.findAll({
+ where: {
+ [Op.and]: [
+ {
+ expires: {
+ [Op.lt]: new Date() // Find all rows with an expiry date before now
+ }
+ }
+ ]
+ }
+ });
+ this.client.logger.verbose(
+ chalk.cyan(`Queried bans, found ${rows.length} expired bans.`)
+ );
+ for (const row of rows) {
+ const guild = this.client.guilds.cache.get(row.guild);
+ if (!guild) {
+ await row.destroy();
+ continue;
+ }
+ try {
+ await guild.members.unban(
+ row.user,
+ `Unbanning user because tempban expired`
+ );
+ } catch (e) {
+ if (e instanceof DiscordAPIError) {
+ // Member not banned, ignore
+ } else throw e;
+ }
+ await row.destroy();
+ this.client.logger.verbose(chalk.cyan('Unbanned user'));
+ }
+ }
+} \ No newline at end of file