From 8ca73f2b62fdc26d4c295084443618e47916f845 Mon Sep 17 00:00:00 2001 From: Bsian Date: Mon, 30 Mar 2020 12:22:32 +0100 Subject: [PATCH] Cleanup and stuff --- src/class/Command.ts | 84 ++++++++++----------- src/commands/bearer.ts | 63 ++++++++-------- src/commands/createaccount.ts | 1 - src/commands/disk.ts | 1 - src/commands/emailcode.ts | 5 +- src/commands/eval.ts | 138 +++++++++++++++++----------------- src/commands/modlogs.ts | 1 - src/commands/notify.ts | 5 +- src/commands/tier.ts | 5 +- src/commands/warn.ts | 85 +++++++++++---------- src/commands/whois.ts | 5 +- src/commands/whois_user.ts | 5 +- src/events/messageCreate.ts | 88 +++++++++++----------- 13 files changed, 238 insertions(+), 248 deletions(-) diff --git a/src/class/Command.ts b/src/class/Command.ts index e1caa2c..0fb911e 100644 --- a/src/class/Command.ts +++ b/src/class/Command.ts @@ -1,42 +1,42 @@ -import { Message } from 'eris'; -import { Client } from '..'; -import { Collection } from '.'; - -export default class Command { - name: string - - parentName: string - - description?: string - - usage?: string - - enabled: boolean - - aliases?: string[] - - client: Client - - permissions?: { roles?: string[], users?: string[] } - - guildOnly?: boolean - - subcmds?: any[] - - subcommands?: Collection - - public run(message: Message, args: string[]) {} // eslint-disable-line - - constructor(client: Client) { - this.name = 'None'; - this.description = 'No description given'; - this.usage = 'No usage given'; - this.enabled = true; - this.aliases = []; - this.guildOnly = true; - this.client = client; - this.subcmds = []; - this.subcommands = new Collection(); - this.permissions = {}; - } -} +import { Message } from 'eris'; +import { Client } from '..'; +import { Collection } from '.'; + +export default class Command { + name: string + + parentName: string + + description?: string + + usage?: string + + enabled: boolean + + aliases?: string[] + + client: Client + + permissions?: { roles?: string[], users?: string[] } + + guildOnly?: boolean + + subcmds?: any[] + + subcommands?: Collection + + public run(message: Message, args: string[]): Promise { return Promise.resolve(); } + + constructor(client: Client) { + this.name = 'None'; + this.description = 'No description given'; + this.usage = 'No usage given'; + this.enabled = true; + this.aliases = []; + this.guildOnly = true; + this.client = client; + this.subcmds = []; + this.subcommands = new Collection(); + this.permissions = {}; + } +} diff --git a/src/commands/bearer.ts b/src/commands/bearer.ts index fd0d70a..74d9cf8 100644 --- a/src/commands/bearer.ts +++ b/src/commands/bearer.ts @@ -1,32 +1,31 @@ -/* eslint-disable consistent-return */ -import { Message } from 'eris'; -import { Command } from '../class'; -import { Client } from '..'; - -export default class Bearer extends Command { - constructor(client: Client) { - super(client); - this.name = 'bearer'; - this.description = 'Creates a bearer token.'; - this.usage = `${this.client.config.prefix}bearer`; - this.guildOnly = false; - this.enabled = true; - } - - public async run(message: Message) { - try { - const account = await this.client.db.Account.findOne({ userID: message.author.id }); - if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); - // eslint-disable-next-line no-underscore-dangle - const bearer = await this.client.server.security.createBearer(account._id); - const dm = await this.client.getDMChannel(message.author.id); - const msg = await dm.createMessage(`__**Library of Code sp-us | Cloud Services [API]**__\n*This message will automatically be deleted in 60 seconds, copy the token and save it. You cannot recover it.*\n\n${bearer}`); - message.channel.createMessage(`***${this.client.stores.emojis.success} Bearer token sent to direct messages.***`); - setTimeout(() => { - msg.delete(); - }, 60000); - } catch (error) { - return this.client.util.handleError(error, message, this); - } - } -} +import { Message } from 'eris'; +import { Command } from '../class'; +import { Client } from '..'; + +export default class Bearer extends Command { + constructor(client: Client) { + super(client); + this.name = 'bearer'; + this.description = 'Creates a bearer token.'; + this.usage = `${this.client.config.prefix}bearer`; + this.guildOnly = false; + this.enabled = true; + } + + public async run(message: Message) { + try { + const account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); + // eslint-disable-next-line no-underscore-dangle + const bearer = await this.client.server.security.createBearer(account._id); + const dm = await this.client.getDMChannel(message.author.id); + const msg = await dm.createMessage(`__**Library of Code sp-us | Cloud Services [API]**__\n*This message will automatically be deleted in 60 seconds, copy the token and save it. You cannot recover it.*\n\n${bearer}`); + message.channel.createMessage(`***${this.client.stores.emojis.success} Bearer token sent to direct messages.***`); + return setTimeout(() => { + msg.delete(); + }, 60000); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/createaccount.ts b/src/commands/createaccount.ts index 15c04b5..d2eb954 100644 --- a/src/commands/createaccount.ts +++ b/src/commands/createaccount.ts @@ -1,5 +1,4 @@ import { Message, PrivateChannel, GroupChannel } from 'eris'; -import uuid from 'uuid/v4'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; diff --git a/src/commands/disk.ts b/src/commands/disk.ts index b53b45c..5721961 100644 --- a/src/commands/disk.ts +++ b/src/commands/disk.ts @@ -3,7 +3,6 @@ import moment from 'moment'; import { Client } from '..'; import { RichEmbed, Command } from '../class'; import { dataConversion } from '../functions'; -// eslint-disable-next-line import/no-unresolved import 'moment-precise-range-plugin'; export default class Disk extends Command { diff --git a/src/commands/emailcode.ts b/src/commands/emailcode.ts index ad203c1..c4949d3 100644 --- a/src/commands/emailcode.ts +++ b/src/commands/emailcode.ts @@ -1,4 +1,3 @@ -/* eslint-disable consistent-return */ import { randomBytes } from 'crypto'; import { Message } from 'eris'; import { Client } from '..'; @@ -35,9 +34,9 @@ export default class EmailCode extends Command { `, }); - message.channel.createMessage(`***${this.client.stores.emojis.success} Code: \`${code}\` | Email Address: ${args[0]}***`); + return message.channel.createMessage(`***${this.client.stores.emojis.success} Code: \`${code}\` | Email Address: ${args[0]}***`); } catch (error) { - await this.client.util.handleError(error, message, this); + return this.client.util.handleError(error, message, this); } } } diff --git a/src/commands/eval.ts b/src/commands/eval.ts index 06fb02c..9cab205 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -1,69 +1,69 @@ -/* eslint-disable no-eval */ -import { Message } from 'eris'; -import { inspect } from 'util'; -import axios from 'axios'; -import { Client } from '..'; -import { Command } from '../class'; - -export default class Eval extends Command { - constructor(client: Client) { - super(client); - this.name = 'eval'; - this.aliases = ['e']; - this.description = 'Evaluate JavaScript code'; - this.enabled = true; - this.permissions = { users: ['253600545972027394', '278620217221971968', '155698776512790528'] }; - this.guildOnly = false; - } - - public async run(message: Message, args: string[]) { - try { - // const evalMessage = message.content.slice(this.client.config.prefix.length).split(' ').slice(1).join(' '); - let evalString = args.join(' ').trim(); - let evaled: any; - let depth = 0; - - if (args[0] && args[0].startsWith('-d')) { - depth = Number(args[0].replace('-d', '')); - if (!depth || depth < 0) depth = 0; - args.shift(); - evalString = args.join(' ').trim(); - } - if (args[0] === '-a' || args[0] === '-async') { - args.shift(); - evalString = `const top = this; (async () => { ${args.join(' ').trim()} })()`; - } - - try { - evaled = await eval(evalString); - if (typeof evaled !== 'string') { - evaled = inspect(evaled, { depth }); - } - if (evaled === undefined) { - evaled = 'undefined'; - } - } catch (error) { - evaled = error.stack; - } - - evaled = evaled.replace(new RegExp(this.client.config.token, 'gi'), 'juul'); - evaled = evaled.replace(new RegExp(this.client.config.emailPass, 'gi'), 'juul'); - evaled = evaled.replace(new RegExp(this.client.config.cloudflare, 'gi'), 'juul'); - - - const display = this.client.util.splitString(evaled, 1975); - if (display[5]) { - try { - const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', display.join('')); - return message.channel.createMessage(`${this.client.stores.emojis.success} Your evaluation evaled can be found on https://snippets.cloud.libraryofcode.org/${data.key}`); - } catch (error) { - return message.channel.createMessage(`${this.client.stores.emojis.error} ${error}`); - } - } - - return display.forEach((m) => message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``)); - } catch (error) { - return this.client.util.handleError(error, message, this); - } - } -} +/* eslint-disable no-eval */ +import { Message } from 'eris'; +import { inspect } from 'util'; +import axios from 'axios'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class Eval extends Command { + constructor(client: Client) { + super(client); + this.name = 'eval'; + this.aliases = ['e']; + this.description = 'Evaluate JavaScript code'; + this.enabled = true; + this.permissions = { users: ['253600545972027394', '278620217221971968', '155698776512790528'] }; + this.guildOnly = false; + } + + public async run(message: Message, args: string[]) { + try { + const evalMessage = message.content.slice(this.client.config.prefix.length).split(' ').slice(1); + let evalString = evalMessage.join(' ').trim(); + let evaled: any; + let depth = 0; + + if (args[0] && args[0].startsWith('-d')) { + depth = Number(args[0].replace('-d', '')); + if (!depth || depth < 0) depth = 0; + const index = evalMessage.findIndex((v) => v.startsWith('-d')); + evalString = evalMessage.slice(index).join(' ').trim(); + } + if (args[0] === '-a') { + const index = evalMessage.findIndex((v) => v === '-a'); + evalString = `(async () => { ${evalMessage.slice(index).join(' ').trim()} })()`; + } + + try { + evaled = await eval(evalString); + if (typeof evaled !== 'string') { + evaled = inspect(evaled, { depth }); + } + if (evaled === undefined) { + evaled = 'undefined'; + } + } catch (error) { + evaled = error.stack; + } + + evaled = evaled.replace(new RegExp(this.client.config.token, 'gi'), 'juul'); + evaled = evaled.replace(new RegExp(this.client.config.emailPass, 'gi'), 'juul'); + evaled = evaled.replace(new RegExp(this.client.config.cloudflare, 'gi'), 'juul'); + + + const display = this.client.util.splitString(evaled, 1975); + if (display[5]) { + try { + const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', display.join('')); + return message.channel.createMessage(`${this.client.stores.emojis.success} Your evaluation evaled can be found on https://snippets.cloud.libraryofcode.org/${data.key}`); + } catch (error) { + return message.channel.createMessage(`${this.client.stores.emojis.error} ${error}`); + } + } + + return display.forEach((m) => message.channel.createMessage(`\`\`\`js\n${m}\n\`\`\``)); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/modlogs.ts b/src/commands/modlogs.ts index da620c2..4b9f60a 100644 --- a/src/commands/modlogs.ts +++ b/src/commands/modlogs.ts @@ -1,5 +1,4 @@ import { Message } from 'eris'; -// eslint-disable-next-line import/no-unresolved import { createPaginationEmbed } from 'eris-pagination'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; diff --git a/src/commands/notify.ts b/src/commands/notify.ts index c7b4cfd..71fcaff 100644 --- a/src/commands/notify.ts +++ b/src/commands/notify.ts @@ -1,4 +1,3 @@ -/* eslint-disable consistent-return */ import { Message } from 'eris'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; @@ -43,9 +42,9 @@ export default class Notify extends Command { `, }); message.delete(); - edit.edit(`***${this.client.stores.emojis.success} Send notification to ${account.username}.***`); + return edit.edit(`***${this.client.stores.emojis.success} Send notification to ${account.username}.***`); } catch (error) { - await this.client.util.handleError(error, message, this); + return this.client.util.handleError(error, message, this); } } } diff --git a/src/commands/tier.ts b/src/commands/tier.ts index 9c2c76f..5a13d98 100644 --- a/src/commands/tier.ts +++ b/src/commands/tier.ts @@ -1,4 +1,3 @@ -/* eslint-disable consistent-return */ import { Message } from 'eris'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; @@ -33,9 +32,9 @@ export default class Tier extends Command { embed.addField('Old Tier -> New Tier', `${account.tier} -> ${args[1]}`, true); embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setTimestamp(); - this.client.createMessage('580950455581147146', { embed }); this.client.getDMChannel(account.userID).then((channel) => channel.createMessage({ embed })).catch(); + this.client.createMessage('580950455581147146', { embed }); return this.client.getDMChannel(account.userID).then((channel) => channel.createMessage({ embed })).catch(); } catch (error) { - await this.client.util.handleError(error, message, this); + return this.client.util.handleError(error, message, this); } } } diff --git a/src/commands/warn.ts b/src/commands/warn.ts index 49d66af..00e0bff 100644 --- a/src/commands/warn.ts +++ b/src/commands/warn.ts @@ -1,43 +1,42 @@ -/* eslint-disable consistent-return */ -import { Message } from 'eris'; -import { Client } from '..'; -import { Command } from '../class'; - -export default class Warn extends Command { - constructor(client: Client) { - super(client); - this.name = 'warn'; - this.description = 'Sends an official warning to user.'; - this.usage = `${this.client.config.prefix}warn [username | user ID]`; - this.permissions = { roles: ['446104438969466890'] }; - this.enabled = true; - } - - public async run(message: Message, args: string[]) { - try { - if (!args.length) return this.client.commands.get('help').run(message, [this.name]); - const edit = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Sending warning...***`); - const account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0].replace(/[<@!>]/gi, '') }] }); - if (!account) return edit.edit(`***${this.client.stores.emojis.error} Cannot find user.***`); - if (account.root) return edit.edit(`***${this.client.stores.emojis.error} Permission denied.***`); - await this.client.util.createModerationLog(account.userID, message.member, 1, args.slice(1).join(' ')); - message.delete(); - edit.edit(`***${this.client.stores.emojis.success} Account ${account.username} has been warned by Moderator ${message.author.username}#${message.author.discriminator}.***`); - this.client.util.transport.sendMail({ - to: account.emailAddress, - from: 'Library of Code sp-us | Cloud Services ', - subject: 'Your account has been warned', - html: ` -

Library of Code sp-us | Cloud Services

-

Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid possible moderative action.

-

Reason: ${args.slice(1).join(' ') ? args.slice(1).join(' ') : 'Not Specified'}

-

Moderator: ${message.author.username}

- - Library of Code sp-us | Support Team - `, - }); - } catch (error) { - await this.client.util.handleError(error, message, this); - } - } -} +import { Message } from 'eris'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class Warn extends Command { + constructor(client: Client) { + super(client); + this.name = 'warn'; + this.description = 'Sends an official warning to user.'; + this.usage = `${this.client.config.prefix}warn [username | user ID]`; + this.permissions = { roles: ['446104438969466890'] }; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args.length) return this.client.commands.get('help').run(message, [this.name]); + const edit = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Sending warning...***`); + const account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0].replace(/[<@!>]/gi, '') }] }); + if (!account) return edit.edit(`***${this.client.stores.emojis.error} Cannot find user.***`); + if (account.root) return edit.edit(`***${this.client.stores.emojis.error} Permission denied.***`); + await this.client.util.createModerationLog(account.userID, message.member, 1, args.slice(1).join(' ')); + message.delete(); + edit.edit(`***${this.client.stores.emojis.success} Account ${account.username} has been warned by Moderator ${message.author.username}#${message.author.discriminator}.***`); + return this.client.util.transport.sendMail({ + to: account.emailAddress, + from: 'Library of Code sp-us | Cloud Services ', + subject: 'Your account has been warned', + html: ` +

Library of Code sp-us | Cloud Services

+

Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid possible moderative action.

+

Reason: ${args.slice(1).join(' ') ? args.slice(1).join(' ') : 'Not Specified'}

+

Moderator: ${message.author.username}

+ + Library of Code sp-us | Support Team + `, + }); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/whois.ts b/src/commands/whois.ts index 38a74c0..fd05802 100644 --- a/src/commands/whois.ts +++ b/src/commands/whois.ts @@ -1,4 +1,3 @@ -/* eslint-disable consistent-return */ import moment from 'moment'; import { Message } from 'eris'; import { Client } from '..'; @@ -53,9 +52,9 @@ export default class Whois extends Command { if (details) embed.addField('Additional Details', details, true); embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setTimestamp(); - message.channel.createMessage({ embed }); + return message.channel.createMessage({ embed }); } catch (error) { - await this.client.util.handleError(error, message, this); + return this.client.util.handleError(error, message, this); } } } diff --git a/src/commands/whois_user.ts b/src/commands/whois_user.ts index e4076f8..9254e07 100644 --- a/src/commands/whois_user.ts +++ b/src/commands/whois_user.ts @@ -1,4 +1,3 @@ -/* eslint-disable consistent-return */ import moment from 'moment'; import { Message } from 'eris'; import { Client } from '..'; @@ -44,9 +43,9 @@ export default class Whois_User extends Command { if (details) embed.addField('Additional Details', details, true); embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setTimestamp(); - message.channel.createMessage({ embed }); + return message.channel.createMessage({ embed }); } catch (error) { - this.client.util.handleError(error, message, this); + return this.client.util.handleError(error, message, this); } } } diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index b9048cb..39684d7 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,44 +1,44 @@ -import { Message, TextChannel } from 'eris'; -import { Client } from '..'; -import Command from '../class/Command'; - -export default class { - public client: Client - - constructor(client: Client) { - this.client = client; - } - - public async run(message: Message) { - try { - if (message.author.bot) return; - if (message.content.indexOf(this.client.config.prefix) !== 0) return; - const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g); - const resolved = await this.client.util.resolveCommand(noPrefix, message); - if (!resolved) return; - if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel)) return; - let hasUserPerms: boolean; - if (resolved.cmd.permissions.users) { - hasUserPerms = resolved.cmd.permissions.users.includes(message.author.id); - } - let hasRolePerms: boolean = false; - if (resolved.cmd.permissions.roles) { - for (const role of resolved.cmd.permissions.roles) { - if (message.member && message.member.roles.includes(role)) { - // this.client.signale.debug(message.member.roles.includes(role)); - hasRolePerms = true; break; - } - } - } - if (!resolved.cmd.permissions.users && !resolved.cmd.permissions.roles) { - hasUserPerms = true; - hasRolePerms = true; - } - if (!hasRolePerms && !hasUserPerms) return; - if (!resolved.cmd.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; } - resolved.cmd.run(message, resolved.args); - } catch (error) { - this.client.util.handleError(error, message); - } - } -} +import { Message, TextChannel } from 'eris'; +import { Client } from '..'; +import Command from '../class/Command'; + +export default class { + public client: Client + + constructor(client: Client) { + this.client = client; + } + + public async run(message: Message) { + try { + if (message.author.bot) return; + if (message.content.indexOf(this.client.config.prefix) !== 0) return; + const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g); + const resolved = await this.client.util.resolveCommand(noPrefix, message); + if (!resolved) return; + if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel)) return; + let hasUserPerms: boolean; + if (resolved.cmd.permissions.users) { + hasUserPerms = resolved.cmd.permissions.users.includes(message.author.id); + } + let hasRolePerms: boolean = false; + if (resolved.cmd.permissions.roles) { + for (const role of resolved.cmd.permissions.roles) { + if (message.member && message.member.roles.includes(role)) { + // this.client.signale.debug(message.member.roles.includes(role)); + hasRolePerms = true; break; + } + } + } + if (!resolved.cmd.permissions.users && !resolved.cmd.permissions.roles) { + hasUserPerms = true; + hasRolePerms = true; + } + if (!hasRolePerms && !hasUserPerms) return; + if (!resolved.cmd.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; } + await resolved.cmd.run(message, resolved.args); + } catch (error) { + this.client.util.handleError(error, message); + } + } +}