2022-07-08 14:21:09 +03:00
/*
2022-07-08 17:09:36 +03:00
-----WEBHOOKS ARE MONITORED AND PROCESSED HERE-----
2022-07-08 14:21:09 +03:00
https://glitch.com/edit/#!/selmer-bot-listener
--------------------------------------------------
*/
const { MongoClient , ServerApiVersion } = require ( 'mongodb' ) ;
const { MessageActionRow , MessageSelectMenu } = require ( 'discord.js' ) ;
2022-07-19 15:41:49 +03:00
const { addComplaintButton } = require ( '../dev only/submitcomplaint' ) ;
2022-07-08 14:21:09 +03:00
//Called from the dropdown menu
async function createSubscriptionManual ( bot , interaction , id , priceID ) {
const stripe = bot . stripe ;
const mongouri = bot . mongouri ;
2022-07-08 17:09:36 +03:00
//Error Checking (unlikely, but just in case)
2022-07-08 14:21:09 +03:00
if ( ! id ) { console . log ( '....What? How?' ) ; return interaction . editReply ( "Uh oh, something happened with the Stripe Discord ID check, please contact support!" ) ; }
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
new Promise ( async function ( resolve , reject ) {
client . connect ( async ( err ) => {
if ( err ) { return console . log ( err ) ; }
const dbo = client . db ( 'main' ) . collection ( 'authorized' ) ;
await dbo . findOne ( { 'discordID' : id } ) . then ( async ( doc ) => {
var userID ;
if ( doc != undefined ) {
client . close ( ) ;
reject ( ` An account with the tag <@ ${ id } > already exists! ` ) ;
} else {
const stripeUser = await stripe . customers . create ( {
metadata : { 'discordID' : id }
} ) ;
userID = stripeUser . id ;
//Add to the database (I have to wait for the insertion)
await dbo . insertOne ( { stripeID : userID , discordID : id , paid : false , startDateUTC : null , tier : 0 } ) . then ( ( ) => { client . close ( ) ; resolve ( userID ) ; } ) ;
}
} ) ;
} ) ;
} ) . then ( async ( userID ) => {
//Deal with the session
const billingPortalSession = await stripe . billingPortal . sessions . create ( {
customer : userID ,
return _url : "https://linktr.ee/selmerbot" ,
} ) ;
const session = await stripe . checkout . sessions . create (
{
payment _method _types : [ "card" ] ,
line _items : [
{
price : priceID ,
quantity : 1 ,
} ,
] ,
customer : userID ,
mode : "subscription" ,
success _url : billingPortalSession . url ,
cancel _url : "https://linktr.ee/selmerbot"
} ) ;
2022-07-12 20:10:35 +03:00
2022-07-08 14:21:09 +03:00
interaction . editReply ( session . url ) ;
2022-07-12 20:10:35 +03:00
} ) . catch ( ( err ) => {
if ( String ( typeof ( err ) ) == 'string' ) {
interaction . editReply ( err ) ;
} else {
console . log ( err ) ;
2022-07-19 15:41:49 +03:00
interaction . editReply ( "A Stripe error occured! Please click the ✅ to report this ASAP!" ) ;
addComplaintButton ( bot , interaction . message ) ;
2022-07-12 20:10:35 +03:00
}
} ) ;
2022-07-08 14:21:09 +03:00
}
async function changeSubscriptionManual ( bot , message ) {
const stripe = bot . stripe ;
const mongouri = bot . mongouri ;
const id = message . author . id ;
2022-07-08 17:09:36 +03:00
//Just in case
2022-07-08 14:21:09 +03:00
if ( ! id ) { return console . log ( '....What? How?' ) ; }
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
new Promise ( async function ( resolve , reject ) {
client . connect ( async ( err ) => {
if ( err ) { return console . log ( err ) ; }
const dbo = client . db ( 'main' ) . collection ( 'authorized' ) ;
await dbo . findOne ( { 'discordID' : id } ) . then ( async ( doc ) => {
var userID ;
if ( doc != undefined ) {
userID = doc . stripeID ;
client . close ( ) ;
resolve ( userID ) ;
} else {
client . close ( ) ;
reject ( ` No user with the ID of <@ ${ message . author . id } > ` ) ;
}
} ) ;
} ) ;
} ) . then ( async ( userID ) => {
2022-07-12 20:10:35 +03:00
await stripe . billingPortal . sessions . create ( {
2022-07-08 14:21:09 +03:00
customer : userID ,
return _url : "https://linktr.ee/selmerbot" ,
2022-07-12 20:10:35 +03:00
} ) . then ( ( session ) => {
message . reply ( session . url ) ;
} )
2022-07-08 14:21:09 +03:00
} ) . catch ( ( err ) => {
2022-07-12 20:10:35 +03:00
if ( String ( typeof ( err ) ) == 'string' ) {
message . reply ( err ) ;
} else {
console . log ( err ) ;
2022-07-19 15:41:49 +03:00
message . reply ( "A Stripe error occured! Please click the ✅ to report this ASAP!" ) ;
addComplaintButton ( bot , interaction . message ) ;
2022-07-12 20:10:35 +03:00
}
2022-07-08 14:21:09 +03:00
} ) ;
}
2022-07-08 17:09:36 +03:00
function createDropDown ( bot , message ) {
2022-07-08 14:21:09 +03:00
const stripe = bot . stripe ;
const pl = [ ] ;
const vl = [ ] ;
stripe . products . list ( {
limit : 3 ,
} ) . then ( ( prod ) => {
prod . data . forEach ( ( obj ) => {
const pricePromise = stripe . prices . retrieve ( obj . default _price ) ;
var newObj = { label : obj . name , description : null , value : ` ${ obj . default _price } ` }
pl . push ( pricePromise ) ;
vl . push ( newObj ) ;
} ) ;
let n = Promise . all ( pl ) ;
let i = 0 ;
n . then ( ( t ) => {
t . forEach ( data => {
let price = data . unit _amount / 100 ;
vl [ i ] . description = ` The $ ${ price } tier ` ;
i ++ ;
} ) ;
const row = new MessageActionRow ( )
. addComponents (
new MessageSelectMenu ( )
. setCustomId ( ` ${ message . author . id } |premium ` )
. setPlaceholder ( 'Nothing selected' )
. addOptions ( vl )
) ;
message . channel . send ( { content : ` Please choose a tier ` , components : [ row ] } ) ;
} ) ;
} ) ;
}
function handleInp ( bot , message ) {
2022-07-08 17:09:36 +03:00
if ( message . content == '!premium' || message . content == '!premium help' ) {
message . reply ( 'Use _!premium buy_ to get premium or use _!premium manage_ to change or cancel your subscription\n\n_Disclaimer: Selmer Bot uses Stripe to manage payments. Read more at *https://stripe.com/ *_' ) ;
2022-07-08 14:21:09 +03:00
} else if ( message . content == '!premium buy' ) {
createDropDown ( bot , message ) ;
} else if ( message . content == '!premium manage' ) {
2022-07-08 17:09:36 +03:00
changeSubscriptionManual ( bot , message ) ;
2022-07-08 14:21:09 +03:00
}
}
module . exports = {
name : 'premium' ,
description : 'everything payment' ,
execute ( message , args , Discord , Client , bot ) {
message . reply ( "Please DM Selmer bot to use this command!" ) ;
} , handleInp , createSubscriptionManual
}