Skip to content

Commit

Permalink
Discord supporter support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Dec 11, 2023
1 parent 370bb85 commit a94c001
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 130 deletions.
23 changes: 8 additions & 15 deletions src/commands/commandList/economy/daily.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const CommandInterface = require('../../CommandInterface.js');
const alterDaily = require('../patreon/alterDaily.js');
const patreonUtil = require('../patreon/utils/patreonUtil.js');

/*
* Daily command.
Expand Down Expand Up @@ -57,7 +58,7 @@ module.exports = new CommandInterface({

// Past midnight
} else {
const generalRewards = getRewards(cowoncy, afterMid);
const generalRewards = await getRewards(p, cowoncy, afterMid);
const boxRewards = getRandomBox(p, uid);
const marriageRewards = await checkMarriage(p, marriage);

Expand Down Expand Up @@ -228,17 +229,9 @@ async function executeQuery(
async function getUserInfo(p, uid) {
let sql = `SELECT
daily,
daily_streak,
IF (
patreonDaily = 1
OR ((TIMESTAMPDIFF(MONTH, patreonTimer, NOW()) < patreonMonths) AND patreons.patreonType = 3)
OR (endDate > NOW() AND patreon_wh.patreonType = 3)
, 1, 0) as patreon
daily_streak
FROM cowoncy
LEFT JOIN user ON cowoncy.id = user.id
LEFT JOIN patreons ON user.uid = patreons.uid
LEFT JOIN patreon_wh ON user.uid = patreon_wh.uid
WHERE cowoncy.id = ${p.msg.author.id};`;
WHERE id = ${p.msg.author.id};`;
sql += `SELECT *
FROM user_announcement
WHERE
Expand Down Expand Up @@ -269,7 +262,7 @@ async function getUserInfo(p, uid) {
AND is_done = 1
)
);`;
sql += 'SELECT sid FROM survey ORDER BY sid DESC limit 1';
sql += 'SELECT sid FROM survey WHERE endDate > NOW() ORDER BY sid DESC limit 1';
const rows = await p.query(sql);

return {
Expand Down Expand Up @@ -363,13 +356,13 @@ function calculateMarriageBonus(p, marriage) {
return totalGain;
}

function getRewards(cowoncy, afterMid) {
async function getRewards(p, cowoncy, afterMid) {
// Grab streak/patreon status
let streak = 0;
let patreon = false;
const supporter = await patreonUtil.getSupporterRank(p, p.msg.author);
let patreon = supporter.benefitRank >= 3;
if (cowoncy) {
streak = cowoncy.daily_streak;
if (cowoncy.patreon == 1) patreon = true;
}

//Calculate daily amount
Expand Down
134 changes: 134 additions & 0 deletions src/commands/commandList/patreon/utils/patreonUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For more information, see README.md and LICENSE
*/

const sku = '1164099862984400926';

exports.giveCustomBattle = async function (p, id) {
const uid = await p.global.getUid(id);
const sql = `INSERT into alterbattle (uid, type, color, footer, author) VALUES
Expand Down Expand Up @@ -89,3 +91,135 @@ exports.giveCustomCookie = async function (p, id) {
(${uid}, 'cookie', 'receive');`;
await p.query(sql);
};

exports.getSupporterRank = async function (p, user) {
if (user.supporterRank) {
const now = new Date();
const updateDiff = new Date() - user.supporterRank.updatedOn;
// Refresh if its past a day
if (updateDiff >= 1000 * 60 * 60 * 24) {
delete user.supporterRank;
// User supporter rank is already cached
} else if (user.supporterRank.endTime > now) {
return user.supporterRank;
// If user does not have supporter rank
} else {
user.supporterRank.endTime = null;
user.supporterRank.benefitRank = 0;
if (updateDiff <= 20 * 1000 /** 60 * 60*/) {
return user.supporterRank;
}
}
}

const uid = await p.global.getUid(user.id);
const sql = `
SELECT * FROM patreons WHERE uid = ${uid};
SELECT * FROM patreon_wh WHERE uid = ${uid};
SELECT * FROM patreon_discord WHERE uid = ${uid};
`;
const result = await p.query(sql);

const supporter = {
endTime: null,
benefitRank: 0,
updatedOn: new Date()
};
if (result[0][0]?.patreonTimer) {
const benefitRank = result[0][0].patreonType;
const startTime = new Date(result[0][0].patreonTimer)
const endTime = new Date(startTime.setMonth(startTime.getMonth() + result[0][0].patreonMonths));
getBetterSupporterRank(supporter, benefitRank, endTime);
}
if (result[1].length) {
result[1].forEach(row => {
const benefitRank = row.patreonType;
const endTime = new Date(row.endDate);
getBetterSupporterRank(supporter, benefitRank, endTime);
});
}
if (result[2][0]) {
const benefitRank = result[2][0].patreonType;
const endTime = new Date(result[2][0].endDate);
getBetterSupporterRank(supporter, benefitRank, endTime);
}

user.supporterRank = supporter;
return supporter;
}

function getBetterSupporterRank(supporter, benefitRank, endTime) {
const now = new Date();
if (endTime < now) {
return;
}
if (benefitRank > supporter.benefitRank) {
supporter.endTime = endTime;
supporter.benefitRank = benefitRank;
} else if (benefitRank == supporter.benefitRank) {
if (endTime > supporter.endTime) {
supporter.endTime = endTime;
}
}
}

exports.handleDiscordUpdate = async function(entitlement) {
let { userId, endDate, error } = parseEntitlment(entitlement);
if (error) {
console.error(error + ': ' + JSON.stringify(entitlement, null, 2));
return;
}

const uid = await this.global.getUid(userId);
let sql = `SELECT * FROM patreon_discord WHERE uid = ${uid};`;
let result = await this.query(sql);
let renewal = false;
if (result[0]) {
const resultEnd = new Date(result[0].endDate);
if (resultEnd >= endDate) {
return;
}
renewal = true;
}
let mysqlDate = this.global.toMySQL(endDate);
sql = `INSERT INTO patreon_discord (uid, patreonType, endDate) VALUES (${uid}, 3, ${mysqlDate}) ON DUPLICATE KEY UPDATE endDate = ${mysqlDate};`;
await this.query(sql);

if (!renewal) {
let txt = `${this.config.emoji.owo.woah} **|** Thank you for supporting OwO Bot! Your account should have access to supporter benefits.`;
txt += `\n${this.config.emoji.blank} **|** Your benefit will renew on: ${this.global.toDiscordTimestamp(endDate)}`;
txt += `\n${this.config.emoji.blank} **|** If you have any questions, please stop by our support server: ${this.config.guildlink}`;
this.sender.msgUser(userId, txt);
}
}

exports.handleDiscordDelete = async function(entitlement) {
const userId = entitlement.user_id;
const uid = await this.global.getUid(entitlement.user_id);
const sql = `DELETE FROM patreon_discord WHERE uid = ${uid};`;
const result = await this.query(sql);
if (result.affectedRows > 0) {
let txt = `${this.config.emoji.owo.cry} **|** It looks like your Discord payment failed.`;
txt += `\n${this.config.emoji.blank} **|** You will no longer receive OwO Bot supporter benefits.`;
txt += `\n${this.config.emoji.blank} **|** If you have any questions, please stop by our support server: ${this.config.guildlink}`;
this.sender.msgUser(userId, txt);
}
}

function parseEntitlment(entitlment) {
if (entitlment.sku_id !== sku) {
return { error: 'Invalid SKU' };
}
const userId = entitlment.user_id;
if (!userId) {
return { error: 'Invalid User' };
}
let endDate = entitlment.ends_at;
// TODO delete
endDate = Date.now();
if (!endDate) {
return { error: 'Invalid End Time' };
}
endDate = new Date(endDate);
return { userId, endDate };
}
2 changes: 1 addition & 1 deletion src/commands/commandList/social/util/wallpaperUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ exports.getItems = async function (p) {
let items = {
'2--': {
emoji: wallpaperEmoji,
id: '2--',
id: 200,
count: result[0].count,
},
};
Expand Down
75 changes: 33 additions & 42 deletions src/commands/commandList/utils/patreon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

const CommandInterface = require('../../CommandInterface.js');

const patreonUtil = require('../../../utils/patreon.js');
const patreonUtil = require('../patreon/utils/patreonUtil.js');

module.exports = new CommandInterface({
alias: ['patreon', 'donate'],
alias: ['patreon', 'donate', 'support', 'supporter'],

args: '',

Expand All @@ -29,49 +29,40 @@ module.exports = new CommandInterface({
six: 500,

execute: async function (p) {
patreonUtil.checkPatreon(p, p.msg.author.id);

let animalPerk, dailyPerk;
let sql = `SELECT patreonDaily, patreonAnimal FROM user WHERE id = ${p.msg.author.id};`;
sql += `SELECT
patreonMonths,
patreonTimer,
TIMESTAMPDIFF(MONTH, patreonTimer, NOW()) AS monthsPassed,
patreonType
FROM user INNER JOIN patreons ON user.uid = patreons.uid
WHERE id = ${p.msg.author.id};`;
sql += `SELECT patreonType, endDate FROM patreon_wh INNER JOIN user ON user.uid = patreon_wh.uid WHERE id = ${p.msg.author.id} ORDER BY endDate desc;`;
let result = await p.query(sql);

if (result[0][0]) {
animalPerk = result[0][0].patreonAnimal;
dailyPerk = result[0][0].patreonDaily;
}
delete p.msg.author.supporterRank;
const supporter = await patreonUtil.getSupporterRank(p, p.msg.author);

let stat = 'Join today for special animals and benefits!';
if (animalPerk && !dailyPerk) stat = 'You are currently a **Patreon**!';
else if (dailyPerk) stat = 'You are currently a **Patreon+**!';
else if (result[1][0]) {
let parsed = patreonUtil.parsePatreon(result[1][0]);
if (parsed) {
if (parsed.animal && !parsed.cowoncy) stat = 'You are currently a **Patreon**';
else stat = 'You are currently a **Patreon+**';
const timestamp = this.global.toDiscordTimestamp(parsed.expireDate, 'f');
stat += '\n**<:blank:427371936482328596> |** until: **' + timestamp + '**';
}
} else if (result[2][0]) {
let parsed = patreonUtil.parseSecondPatreon(result[2][0]);
if (parsed) {
if (parsed.animal && !parsed.cowoncy) stat = 'You are currently a **Patreon**';
else stat = 'You are currently a **Patreon+**';
const timestamp = this.global.toDiscordTimestamp(parsed.expireDate, 'f');
stat += '\n**<:blank:427371936482328596> |** until: **' + timestamp + '**';
}
const timestamp = this.global.toDiscordTimestamp(new Date(supporter.endTime), 'f');
if (supporter.benefitRank >= 3) {
stat = 'You are currently a **Supporter+**!';
stat += '\n**<:blank:427371936482328596> |** until: **' + timestamp + '**';
} else if (supporter.benefitRank >= 1) {
stat = 'You are currently a **Supporter**!';
stat += '\n**<:blank:427371936482328596> |** until: **' + timestamp + '**';
}

let text = '**<:patreon:449705754522419222> |** Donate to OwO Bot for special benefits!\n';
text += '**<:blank:427371936482328596> |** ' + stat + '\n';
text += '**<:blank:427371936482328596> |** https://www.patreon.com/OwOBot';
p.send(text);
let content = `**${this.config.emoji.owo.woah} |** Donate to OwO Bot for special benefits!\n**${this.config.emoji.blank} |** ${stat}`;
content += `\n**${this.config.emoji.blank} |** Join by clicking my profile or one of the following choices below!`;
const components = [
{
type: 1,
components: [
{
type: 2,
label: 'Patreon',
style: 5,
url: 'https://www.patreon.com/OwOBot'
},
{
type: 2,
label: 'OwO Store',
style: 5,
url: 'https://owobot.com/store'
}
],
},
]
p.send({ content, components });
},
});
Loading

0 comments on commit a94c001

Please sign in to comment.