From 8ce7f51f8d57781d7411790dfda5c1382320f74b Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 28 Nov 2021 22:16:30 -0500 Subject: [PATCH] add promo commands --- src/commands/addpromo.ts | 33 ++++++++++ src/commands/billing_t3.ts | 124 ++++++++++++++++++------------------- src/commands/delpromo.ts | 29 +++++++++ src/commands/index.ts | 108 ++++++++++++++++---------------- 4 files changed, 179 insertions(+), 115 deletions(-) create mode 100644 src/commands/addpromo.ts create mode 100644 src/commands/delpromo.ts diff --git a/src/commands/addpromo.ts b/src/commands/addpromo.ts new file mode 100644 index 0000000..e6439bb --- /dev/null +++ b/src/commands/addpromo.ts @@ -0,0 +1,33 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class AddPromoCode extends Command { + constructor(client: Client) { + super(client); + this.name = 'addpromo'; + this.description = 'Creates a new promotional code for Stripe.'; + this.usage = `${this.client.config.prefix}addpromo `; + this.aliases = ['promo', 'ap']; + this.permissions = 6; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + try { + const pcd = await this.client.stripe.promotionCodes.retrieve(args[0]); + const promo = new this.client.db.Promo({ + code: pcd.code, + pID: args[0], + }); + await promo.save(); + } catch (err) { + return this.error(message.channel, 'Promotional API ID doesn\'t exist.'); + } + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/billing_t3.ts b/src/commands/billing_t3.ts index 63568cb..efcaffa 100644 --- a/src/commands/billing_t3.ts +++ b/src/commands/billing_t3.ts @@ -1,62 +1,62 @@ -import axios from 'axios'; -import { Message } from 'eris'; -import type { Stripe } from 'stripe'; -import { Client, Command } from '../class'; -import type { PromoInterface } from '../models'; - -export default class Billing_T3 extends Command { - constructor(client: Client) { - super(client); - this.name = 't3'; - this.description = 'Subscription to CS Tier 3.'; - this.usage = `${this.client.config.prefix}billing t3 [promoCode]`; - this.permissions = 0; - this.guildOnly = false; - this.enabled = true; - } - - public async run(message: Message, args: string[]) { - try { - await message.delete(); - const response = <{ - found: boolean, - emailAddress?: string, - tier?: number, - supportKey?: string, - }>(await axios.get(`https://api.cloud.libraryofcode.org/wh/info?id=${message.author.id}&authorization=${this.client.config.internalKey}`)).data; - if (!response.found) return this.error(message.channel, 'CS Account not found.'); - - const customer = await this.client.db.Customer.findOne({ userID: message.author.id }); - if (!customer) return this.error(message.channel, `You do not have a Customer Account. Please run \`${this.client.config.prefix}billing\`, once you visit the Billing Portal via the URL given to you, please try again.`); - - let promoCode: PromoInterface; - if (args[0]) { - promoCode = await this.client.db.Promo.findOne({ code: args[0].toUpperCase() }); - } - - let subscription: Stripe.Response; - try { - subscription = await this.client.stripe.subscriptions.create({ - customer: customer.cusID, - payment_behavior: 'allow_incomplete', - items: [{ price: 'price_1H8e6ODatwI1hQ4WFVvX6Nda' }], - days_until_due: 1, - collection_method: 'send_invoice', - default_tax_rates: ['txr_1HlAadDatwI1hQ4WRHu14S2I'], - promotion_code: promoCode ? promoCode.id : undefined, - }); - } catch (err) { - return this.error(message.channel, `Error creating subscription.\n\n${err}`); - } - - await this.client.stripe.invoices.finalizeInvoice(subscription.latest_invoice.toString()); - const invoice = await this.client.stripe.invoices.retrieve(subscription.latest_invoice.toString()); - - const chan = await this.client.getDMChannel(message.author.id); - await chan.createMessage(`__**Invoice for New Subscription**__\n${invoice.hosted_invoice_url}\n\n*Please click on the link above to pay for your subscription.*`); - return this.success(message.channel, 'Transaction processed.'); - } catch (err) { - return this.client.util.handleError(err, message, this); - } - } -} +import axios from 'axios'; +import { Message } from 'eris'; +import type { Stripe } from 'stripe'; +import { Client, Command } from '../class'; +import type { PromoInterface } from '../models'; + +export default class Billing_T3 extends Command { + constructor(client: Client) { + super(client); + this.name = 't3'; + this.description = 'Subscription to CS Tier 3.'; + this.usage = `${this.client.config.prefix}billing t3 [promoCode]`; + this.permissions = 0; + this.guildOnly = false; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + await message.delete(); + const response = <{ + found: boolean, + emailAddress?: string, + tier?: number, + supportKey?: string, + }>(await axios.get(`https://api.cloud.libraryofcode.org/wh/info?id=${message.author.id}&authorization=${this.client.config.internalKey}`)).data; + if (!response.found) return this.error(message.channel, 'CS Account not found.'); + + const customer = await this.client.db.Customer.findOne({ userID: message.author.id }); + if (!customer) return this.error(message.channel, `You do not have a Customer Account. Please run \`${this.client.config.prefix}billing\`, once you visit the Billing Portal via the URL given to you, please try again.`); + + let promoCode: PromoInterface; + if (args[0]) { + promoCode = await this.client.db.Promo.findOne({ code: args[0].toUpperCase() }); + } + + let subscription: Stripe.Response; + try { + subscription = await this.client.stripe.subscriptions.create({ + customer: customer.cusID, + payment_behavior: 'allow_incomplete', + items: [{ price: 'price_1H8e6ODatwI1hQ4WFVvX6Nda' }], + days_until_due: 1, + collection_method: 'send_invoice', + default_tax_rates: ['txr_1HlAadDatwI1hQ4WRHu14S2I'], + promotion_code: promoCode ? promoCode.id : undefined, + }); + } catch (err) { + return this.error(message.channel, `Error creating subscription.\n\n${err}`); + } + + await this.client.stripe.invoices.finalizeInvoice(subscription.latest_invoice.toString()); + const invoice = await this.client.stripe.invoices.retrieve(subscription.latest_invoice.toString()); + + const chan = await this.client.getDMChannel(message.author.id); + await chan.createMessage(`__**Invoice for New Subscription**__\n${invoice.hosted_invoice_url}\n\n*Please click on the link above to pay for your subscription.*`); + return this.success(message.channel, 'Transaction processed.'); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/delpromo.ts b/src/commands/delpromo.ts new file mode 100644 index 0000000..de52583 --- /dev/null +++ b/src/commands/delpromo.ts @@ -0,0 +1,29 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class DeletePromoCode extends Command { + constructor(client: Client) { + super(client); + this.name = 'delpromo'; + this.description = 'Deletes a promotional code for Stripe.'; + this.usage = `${this.client.config.prefix}delpromo `; + this.aliases = ['dp']; + this.permissions = 6; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + try { + await this.client.stripe.promotionCodes.retrieve(args[0]); + await this.client.db.Promo.deleteOne({ pID: args[0] }).lean().exec(); + } catch (err) { + return this.error(message.channel, 'Promotional API ID doesn\'t exist.'); + } + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 92057eb..0ad1e12 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,53 +1,55 @@ -export { default as additem } from './additem'; -export { default as addmerchant } from './addmerchant'; -export { default as addnote } from './addnote'; -export { default as addrank } from './addrank'; -export { default as addredirect } from './addredirect'; -export { default as apply } from './apply'; -export { default as ban } from './ban'; -export { default as billing } from './billing'; -export { default as callback } from './callback'; -export { default as delitem } from './delitem'; -export { default as delmerchant } from './delmerchant'; -export { default as delnote } from './delnote'; -export { default as delrank } from './delrank'; -export { default as delredirect } from './delredirect'; -export { default as djs } from './djs'; -export { default as eris } from './eris'; -export { default as eval } from './eval'; -export { default as game } from './game'; -export { default as help } from './help'; -export { default as info } from './info'; -export { default as pulldata } from './inquiry'; -export { default as intercom } from './intercom'; -export { default as judgement } from './judgement'; -export { default as kick } from './kick'; -export { default as listredirects } from './listredirects'; -export { default as market } from './market'; -export { default as members } from './members'; -export { default as mute } from './mute'; -export { default as notes } from './notes'; -export { default as npm } from './npm'; -export { default as offer } from './offer'; -export { default as page } from './page'; -export { default as ping } from './ping'; -export { default as pgp } from './pgp'; -export { default as profile } from './profile'; -export { default as rank } from './rank'; -export { default as role } from './role'; -export { default as roleinfo } from './roleinfo'; -export { default as saa } from './saa'; -export { default as score } from './score'; -export { default as sip } from './sip'; -export { default as site } from './site'; -export { default as slowmode } from './slowmode'; -export { default as sss } from './sss'; -export { default as stats } from './stats'; -export { default as storemessages } from './storemessages'; -export { default as sysinfo } from './sysinfo'; -export { default as train } from './train'; -export { default as tts } from './tts'; -export { default as unban } from './unban'; -export { default as unmute } from './unmute'; -export { default as whois } from './whois'; -export { default as x509 } from './x509'; +export { default as additem } from './additem'; +export { default as addmerchant } from './addmerchant'; +export { default as addnote } from './addnote'; +export { default as addpromo } from './addpromo'; +export { default as addrank } from './addrank'; +export { default as addredirect } from './addredirect'; +export { default as apply } from './apply'; +export { default as ban } from './ban'; +export { default as billing } from './billing'; +export { default as callback } from './callback'; +export { default as delitem } from './delitem'; +export { default as delmerchant } from './delmerchant'; +export { default as delnote } from './delnote'; +export { default as delpromo } from './delpromo'; +export { default as delrank } from './delrank'; +export { default as delredirect } from './delredirect'; +export { default as djs } from './djs'; +export { default as eris } from './eris'; +export { default as eval } from './eval'; +export { default as game } from './game'; +export { default as help } from './help'; +export { default as info } from './info'; +export { default as pulldata } from './inquiry'; +export { default as intercom } from './intercom'; +export { default as judgement } from './judgement'; +export { default as kick } from './kick'; +export { default as listredirects } from './listredirects'; +export { default as market } from './market'; +export { default as members } from './members'; +export { default as mute } from './mute'; +export { default as notes } from './notes'; +export { default as npm } from './npm'; +export { default as offer } from './offer'; +export { default as page } from './page'; +export { default as ping } from './ping'; +export { default as pgp } from './pgp'; +export { default as profile } from './profile'; +export { default as rank } from './rank'; +export { default as role } from './role'; +export { default as roleinfo } from './roleinfo'; +export { default as saa } from './saa'; +export { default as score } from './score'; +export { default as sip } from './sip'; +export { default as site } from './site'; +export { default as slowmode } from './slowmode'; +export { default as sss } from './sss'; +export { default as stats } from './stats'; +export { default as storemessages } from './storemessages'; +export { default as sysinfo } from './sysinfo'; +export { default as train } from './train'; +export { default as tts } from './tts'; +export { default as unban } from './unban'; +export { default as unmute } from './unmute'; +export { default as whois } from './whois'; +export { default as x509 } from './x509';