From 7316b62282dbf0cce852d1916f8570629e95603b Mon Sep 17 00:00:00 2001 From: Bsian Date: Thu, 21 Nov 2019 14:35:21 +0000 Subject: [PATCH 1/3] Added command --- src/commands/index.ts | 1 + src/commands/unban.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/commands/unban.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index 600c9de..ee72450 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -17,6 +17,7 @@ export { default as Pull } from './pull'; export { default as Restart } from './restart'; export { default as SecureSign } from './securesign'; export { default as Sysinfo } from './sysinfo'; +export { default as Unban } from './unban'; export { default as Unlock } from './unlock'; export { default as Warn } from './warn'; export { default as Whois } from './whois'; diff --git a/src/commands/unban.ts b/src/commands/unban.ts new file mode 100644 index 0000000..8366434 --- /dev/null +++ b/src/commands/unban.ts @@ -0,0 +1,35 @@ +import { Message } from 'eris'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class Unban extends Command { + constructor(client: Client) { + super(client); + + this.name = 'unban'; + this.description = 'Unban an IP from Cloud/NGINX'; + this.aliases = ['unbanip']; + this.usage = `${this.client.config.prefix}unban [service] [ip]`; + this.permissions = { roles: ['455972169449734144', '643619219988152321'] }; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[1]) return this.client.commands.get('help').run(message, [this.name]); + const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Unbanning IP...***`); + try { + await this.client.util.exec(`sudo fail2ban-client set ${args[0]} unbanip ${args[1]}`); + } catch (error) { + if (error.message.includes('is not banned')) return msg.edit(`${this.client.stores.emojis.error} ***IP address not banned***`); + if (error.message.includes(`'${args[0]}'`)) return msg.edit(`${this.client.stores.emojis.error} ***Invalid service***`); + throw error; + } + + message.delete(); + return msg.edit(`${this.client.stores.emojis.success} ***IP address ${args[1]} unbanned from ${args[0].toUpperCase()}***`); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} From 3156fe58e9b238625f8876ae2e82cee26c3bb253 Mon Sep 17 00:00:00 2001 From: Bsian Date: Thu, 21 Nov 2019 14:39:07 +0000 Subject: [PATCH 2/3] Error handling --- src/api/routes/Account.ts | 3 +++ src/api/routes/Root.ts | 1 + 2 files changed, 4 insertions(+) diff --git a/src/api/routes/Account.ts b/src/api/routes/Account.ts index 198bebd..8a10596 100644 --- a/src/api/routes/Account.ts +++ b/src/api/routes/Account.ts @@ -27,6 +27,7 @@ export default class Account extends Route { res.status(200).json({ code: this.constants.codes.SUCCESS, message: acc }); } catch (error) { this.handleError(error, res); + this.server.client.util.handleError(error); } }); @@ -42,6 +43,7 @@ export default class Account extends Route { } } catch (error) { this.handleError(error, res); + this.server.client.util.handleError(error); } }); @@ -51,6 +53,7 @@ export default class Account extends Route { res.status(200).json({ code: this.constants.codes.SUCCESS, message: data }); } catch (error) { this.handleError(error, res); + this.server.client.util.handleError(error); } }); } diff --git a/src/api/routes/Root.ts b/src/api/routes/Root.ts index 4832b99..a1e5766 100644 --- a/src/api/routes/Root.ts +++ b/src/api/routes/Root.ts @@ -35,6 +35,7 @@ export default class Root extends Route { res.status(200).json({ code: this.constants.codes.SUCCESS, message: response }); } catch (error) { this.handleError(error, res); + this.server.client.util.handleError(error); } }); } From 5a3520aa4b4a8361f7f341f7c0b542dd3c0ba219 Mon Sep 17 00:00:00 2001 From: Bsian Date: Thu, 21 Nov 2019 14:42:05 +0000 Subject: [PATCH 3/3] Error handler --- src/commands/whois_user.ts | 62 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/commands/whois_user.ts b/src/commands/whois_user.ts index 525d419..a635a8c 100644 --- a/src/commands/whois_user.ts +++ b/src/commands/whois_user.ts @@ -16,34 +16,38 @@ export default class Whois_User extends Command { } public async run(message: Message, args: string[]) { - let account: AccountInterface; - if (!args[0]) account = await this.client.db.Account.findOne({ userID: message.author.id }); - else account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0] }] }); - if (!account) return message.channel.createMessage(`***${this.client.stores.emojis.error} You don't have an account.***`); - const embed = new RichEmbed(); - embed.setTitle('Account Information'); - if (this.client.users.get(account.userID)) embed.setThumbnail(this.client.users.get(account.userID).avatarURL); - embed.setColor(0x36393f); - embed.addField('Username', `${account.username} | <@${account.userID}>`, true); - embed.addField('ID', account.userID, true); - embed.addField('Created By', `<@${this.client.users.get(account.createdBy).id}>`, true); - embed.addField('Created At', moment(account.createdAt).format('dddd, MMMM Do YYYY, h:mm:ss A'), true); - const cpuUsage = await this.client.util.exec(`top -b -n 1 -u ${account.username} | awk 'NR>7 { sum += $9; } END { print sum; }'`); - embed.addField('CPU Usage', cpuUsage.split('\n')[0] ? `${cpuUsage.split('\n')[0]}%` : '0%', true); - embed.addField('Memory', dataConversion(Number(await this.client.util.exec(`memory ${account.username}`)) * 1000), true); - const data = await this.client.redis.get(`storage-${account.username}`) ? dataConversion(Number(await this.client.redis.get(`storage-${account.username}`))) : 'N/A'; - embed.addField('Storage', data, true); - let details = ''; - if (account.locked) details += 'This account is currently locked.\n'; - if (account.permissions.engineer) details += 'This account belongs to an Engineer.\n'; - else if (account.permissions.communityManager) details += 'This account belongs to a Community Manager.\n'; - else if (account.permissions.supervisor) details += 'This account belongs to a Supervisor.\n'; - else if (account.permissions.staff) details += 'This account belongs to a Staff member.\n'; - if (account.root) details += 'This account has root/administrative privileges.\n'; - if (details) embed.addField('Additional Details', details, true); - embed.setFooter(this.client.user.username, this.client.user.avatarURL); - embed.setTimestamp(); - // @ts-ignore - message.channel.createMessage({ embed }); + try { + let account: AccountInterface; + if (!args[0]) account = await this.client.db.Account.findOne({ userID: message.author.id }); + else account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0] }] }); + if (!account) return message.channel.createMessage(`***${this.client.stores.emojis.error} You don't have an account.***`); + const embed = new RichEmbed(); + embed.setTitle('Account Information'); + if (this.client.users.get(account.userID)) embed.setThumbnail(this.client.users.get(account.userID).avatarURL); + embed.setColor(0x36393f); + embed.addField('Username', `${account.username} | <@${account.userID}>`, true); + embed.addField('ID', account.userID, true); + embed.addField('Created By', `<@${this.client.users.get(account.createdBy).id}>`, true); + embed.addField('Created At', moment(account.createdAt).format('dddd, MMMM Do YYYY, h:mm:ss A'), true); + const cpuUsage = await this.client.util.exec(`top -b -n 1 -u ${account.username} | awk 'NR>7 { sum += $9; } END { print sum; }'`); + embed.addField('CPU Usage', cpuUsage.split('\n')[0] ? `${cpuUsage.split('\n')[0]}%` : '0%', true); + embed.addField('Memory', dataConversion(Number(await this.client.util.exec(`memory ${account.username}`)) * 1000), true); + const data = await this.client.redis.get(`storage-${account.username}`) ? dataConversion(Number(await this.client.redis.get(`storage-${account.username}`))) : 'N/A'; + embed.addField('Storage', data, true); + let details = ''; + if (account.locked) details += 'This account is currently locked.\n'; + if (account.permissions.engineer) details += 'This account belongs to an Engineer.\n'; + else if (account.permissions.communityManager) details += 'This account belongs to a Community Manager.\n'; + else if (account.permissions.supervisor) details += 'This account belongs to a Supervisor.\n'; + else if (account.permissions.staff) details += 'This account belongs to a Staff member.\n'; + if (account.root) details += 'This account has root/administrative privileges.\n'; + if (details) embed.addField('Additional Details', details, true); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + // @ts-ignore + message.channel.createMessage({ embed }); + } catch (error) { + this.client.util.handleError(error, message, this); + } } }