2022-07-08 14:21:09 +03:00
/*
2022-07-08 17:09:36 +03:00
-----WEBHOOKS ARE MONITORED AND PROCESSED HERE-----
2022-07-31 10:11:55 +03:00
https://selmer-bot-listener.ion606.repl.co
2022-07-08 14:21:09 +03:00
--------------------------------------------------
*/
2022-09-27 16:45:50 -04:00
//@ts-check
2022-07-08 14:21:09 +03:00
const { MongoClient , ServerApiVersion } = require ( 'mongodb' ) ;
2022-09-27 16:45:50 -04:00
const { MessageActionRow , MessageSelectMenu , Constants } = 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!" ) ; }
2022-07-31 10:11:55 +03:00
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
2022-07-08 14:21:09 +03:00
new Promise ( async function ( resolve , reject ) {
2022-07-31 10:11:55 +03:00
bot . mongoconnection . then ( async ( client ) => {
// if (err) { return console.log(err); }
2022-07-08 14:21:09 +03:00
const dbo = client . db ( 'main' ) . collection ( 'authorized' ) ;
await dbo . findOne ( { 'discordID' : id } ) . then ( async ( doc ) => {
var userID ;
if ( doc != undefined ) {
2022-07-31 10:11:55 +03:00
// client.close();
2022-07-08 14:21:09 +03:00
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)
2022-07-31 10:11:55 +03:00
await dbo . insertOne ( { stripeID : userID , discordID : id , paid : false , startDateUTC : null , tier : 0 } ) . then ( ( ) => { /*client.close();*/ resolve ( userID ) ; } ) ;
2022-07-08 14:21:09 +03:00
}
} ) ;
} ) ;
} ) . 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
}
2022-09-27 16:45:50 -04:00
async function changeSubscriptionManual ( bot , interaction ) {
2022-07-08 14:21:09 +03:00
const stripe = bot . stripe ;
const mongouri = bot . mongouri ;
2022-09-27 16:45:50 -04:00
const id = interaction . user . id ;
2022-07-08 14:21:09 +03:00
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?' ) ; }
2022-07-31 10:11:55 +03:00
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
2022-07-08 14:21:09 +03:00
new Promise ( async function ( resolve , reject ) {
2022-07-31 10:11:55 +03:00
bot . mongoconnection . then ( async ( client ) => {
// if (err) { return console.log(err); }
2022-07-08 14:21:09 +03:00
const dbo = client . db ( 'main' ) . collection ( 'authorized' ) ;
await dbo . findOne ( { 'discordID' : id } ) . then ( async ( doc ) => {
var userID ;
if ( doc != undefined ) {
userID = doc . stripeID ;
2022-07-31 10:11:55 +03:00
// client.close();
2022-07-08 14:21:09 +03:00
resolve ( userID ) ;
} else {
2022-07-31 10:11:55 +03:00
// client.close();
2022-09-27 16:45:50 -04:00
reject ( ` No user with the ID of <@ ${ interaction . user . id } > ` ) ;
2022-07-08 14:21:09 +03:00
}
} ) ;
} ) ;
} ) . 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 ) => {
2022-09-27 16:45:50 -04:00
interaction . reply ( session . url ) ;
2022-07-12 20:10:35 +03:00
} )
2022-07-08 14:21:09 +03:00
} ) . catch ( ( err ) => {
2022-07-12 20:10:35 +03:00
if ( String ( typeof ( err ) ) == 'string' ) {
2022-09-27 16:45:50 -04:00
interaction . reply ( err ) ;
2022-07-12 20:10:35 +03:00
} else {
console . log ( err ) ;
2022-09-27 16:45:50 -04:00
interaction . 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-09-27 16:45:50 -04:00
function createDropDown ( bot , interaction ) {
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 ;
2022-09-27 16:45:50 -04:00
n . then ( ( t ) => {
2022-07-08 14:21:09 +03:00
t . forEach ( data => {
let price = data . unit _amount / 100 ;
vl [ i ] . description = ` The $ ${ price } tier ` ;
i ++ ;
} ) ;
const row = new MessageActionRow ( )
. addComponents (
new MessageSelectMenu ( )
2022-09-27 16:45:50 -04:00
. setCustomId ( ` ${ interaction . user . id } |premium ` )
2022-07-08 14:21:09 +03:00
. setPlaceholder ( 'Nothing selected' )
. addOptions ( vl )
) ;
2022-09-27 16:45:50 -04:00
interaction . reply ( { content : ` Please choose a tier ` , components : [ row ] , ephemeral : true } ) ;
2022-07-08 14:21:09 +03:00
} ) ;
} ) ;
}
2022-09-27 16:45:50 -04:00
function handleInp ( bot , interaction ) {
const inp = interaction . options . data [ 0 ] ;
if ( ! inp || inp . value == 'help' ) {
interaction . reply ( { content : '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/ *_' , ephemeral : true } ) ;
} else if ( inp . value == 'buy' ) {
createDropDown ( bot , interaction ) ;
} else if ( inp . value == 'manage' ) {
changeSubscriptionManual ( bot , interaction ) ;
2022-07-08 14:21:09 +03:00
}
}
module . exports = {
name : 'premium' ,
description : 'everything payment' ,
2022-09-27 16:45:50 -04:00
execute ( interaction , Discord , Client , bot ) {
handleInp ( bot , interaction ) ;
} , handleInp , createSubscriptionManual ,
options : [ { name : 'input' , description : 'What do you want to do?' , type : Constants . ApplicationCommandOptionTypes . STRING , required : true , choices : [ { name : 'help' , value : 'help' } , { name : 'buy' , value : 'buy' } , { name : 'manage' , value : 'manage' } ] } ] ,
isDM : true
2022-07-08 14:21:09 +03:00
}