Skip to content

Commit

Permalink
Fixed patreon api calls
Browse files Browse the repository at this point in the history
fixed circular dependencies
  • Loading branch information
ChristopherBThai committed Apr 20, 2024
1 parent c7fcd52 commit 24078f2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 22 deletions.
58 changes: 39 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"npm": "^7.8.0",
"parse-duration": "^0.4.4",
"patreon": "^0.4.1",
"patreon-discord": "^0.0.5",
"pluralize": "^8.0.0",
"prettier": "^2.8.0",
"random-number-csprng": "^1.0.2",
Expand Down
32 changes: 32 additions & 0 deletions src/botHandlers/patreonHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* For more information, see README.md and LICENSE
*/
const axios = require('axios');
const { Campaign } = require('patreon-discord');
const members = {};
const cache = {};
let campaign;

exports.request = async function (cookie) {
console.log('getting cowoncy...');
Expand Down Expand Up @@ -67,10 +71,19 @@ async function getUsers(cookie, url, list) {
list.push({
name: item.attributes?.full_name,
discord: discord?.user_id,
user_id: item.id,
});
} else if (item.type === 'member') {
members[item.relationships.user.data.id] = item.id;
}
});
for (let i in list) {
if (!list[i].discord) {
list[i].discord = await getDiscordId(list[i].user_id);
}
}

console.log('list length: ' + list.length);
if (data.links.next) {
console.log('getting next page...');
return getUsers(cookie, data.links.next, list);
Expand All @@ -82,6 +95,25 @@ async function getUsers(cookie, url, list) {
}
}

async function getDiscordId(userId) {
if (!campaign) {
campaign = new Campaign({
patreonToken: process.env.PATREON_ACCESS_TOKEN,
campaignId: 1623609,
});
}
const memberId = members[userId];

if (cache[memberId]) {
return cache[memberId];
}

const patron = await campaign.fetchPatron(memberId);
const discordId = patron.discord_user_id;
cache[memberId] = discordId;
return discordId;
}

/*
var url = require('url');
var patreon = require('patreon');
Expand Down
1 change: 1 addition & 0 deletions src/commands/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ function initParam(msg, command, args, main, context) {
dateUtil: main.dateUtil,
neo4j: main.neo4j,
giveaway: main.giveaway,
patreonUtil: main.patreonUtil,
};
param.setCooldown = function (cooldown) {
main.cooldown.setCooldown(param, aliasToCommand[command], cooldown);
Expand Down
1 change: 0 additions & 1 deletion src/commands/commandList/admin/getPatreonRewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ async function getPatreons(p) {
console.error(err);
return;
}
console.log(patreons);
let result = [];
if (p.args[0] != 'ignoresql') {
let sql =
Expand Down
3 changes: 1 addition & 2 deletions src/commands/commandList/battle/util/teamUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const animalUtil = require('./animalUtil.js');
const WeaponInterface = require('../WeaponInterface.js');
const global = require('../../../../utils/global.js');
const mysql = require('../../../../botHandlers/mysqlHandler.js');
const patreonUtil = require('../../patreon/utils/patreonUtil.js');
const defaultMaxTeams = 2;
let weaponUtil;

Expand Down Expand Up @@ -589,7 +588,7 @@ exports.setWeaponUtil = function (util) {

exports.getMaxTeams = async function (user, patreonRank) {
let maxTeams = defaultMaxTeams;
let patreon = patreonRank || (await patreonUtil.getSupporterRank(this, user));
let patreon = patreonRank || (await this.patreonUtil.getSupporterRank(this, user));
if (patreon?.benefitRank >= 3) {
maxTeams++;
}
Expand Down
2 changes: 2 additions & 0 deletions src/owo.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class OwO extends Base {
this.patreon = require('./utils/patreon.js');
this.patreon.init(this);

this.patreonUtil = require('./commands/commandList/patreon/utils/patreonUtil.js');

try {
this.badwords = require('./../../tokens/badwords.json');
} catch (err) {
Expand Down

0 comments on commit 24078f2

Please sign in to comment.