Discord_API_Bot/helpers/cron.js

116 lines
3 KiB
JavaScript
Raw Normal View History

2024-04-02 04:02:22 +01:00
const api = require("./api");
2024-04-08 11:12:41 +01:00
const { guild_id, api_role_id, package_roles } = require("../config");
2024-04-02 04:02:22 +01:00
const users = {};
2024-04-08 11:12:41 +01:00
module.exports = async client => {
const guild = await client.guilds.fetch(guild_id).catch(() => {});
if (!guild) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
let oauthUsers = await api.getOauthUsers();
oauthUsers.forEach(user => {
users[user.user_id] = user.data.id;
2024-04-02 04:02:22 +01:00
});
2024-04-08 11:12:41 +01:00
checkNewOauths();
async function checkNewOauths() {
setTimeout(checkNewOauths, 120000);
const newOauths = await api.getNewOauths();
if (!newOauths) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
newOauths.data.forEach(async oauth => {
users[oauth.user_id] = oauth.data.id;
const member = await guild.members.fetch(oauth.data.id).catch(() => {});
if (!member) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
if (member.roles.cache.has(api_role_id)) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
return member.roles.add(api_role_id).catch(() => {});
2024-04-02 04:02:22 +01:00
});
}
2024-04-08 11:12:41 +01:00
checkActiveOrders();
async function checkActiveOrders() {
setTimeout(checkActiveOrders, 300000);
const activeOrders = await api.getActiveOrders();
if (!activeOrders) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
activeOrders.forEach(async order => {
const discordId = users[order.user_id];
if (!discordId) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
const member = await guild.members.fetch(discordId).catch(() => {});
if (!member) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
const packageRole = package_roles.find(role => role.packageID === order.package_id);
if (!packageRole) {
2024-04-02 04:02:22 +01:00
return;
}
2024-04-08 11:12:41 +01:00
return member.roles.add(packageRole.roleID).catch(() => {});
2024-04-02 04:02:22 +01:00
});
}
2024-04-08 11:12:41 +01:00
checkPackageRoles();
async function checkPackageRoles() {
setTimeout(checkPackageRoles, 1800000);
for (const packageRole of package_roles) {
const role = await guild.roles.fetch(packageRole.roleID).catch(() => {
2024-04-02 04:02:22 +01:00
console.log("Role not found.");
});
2024-04-08 11:12:41 +01:00
if (!role) {
continue;
2024-04-02 04:02:22 +01:00
}
2024-04-08 11:12:41 +01:00
const memberIds = role.members.keys();
if (memberIds.size === 0) {
continue;
2024-04-02 04:02:22 +01:00
}
2024-04-08 11:12:41 +01:00
for (const memberId of memberIds) {
const oauthUser = oauthUsers.find(user => user.data.id === memberId);
if (!oauthUser) {
2024-04-02 04:02:22 +01:00
continue;
}
2024-04-08 11:12:41 +01:00
2024-04-02 04:02:22 +01:00
await delay(5000);
2024-04-08 11:12:41 +01:00
const apiUser = await api.getUser(oauthUser.email);
if (!apiUser || apiUser.data.length === 0) {
2024-04-02 04:02:22 +01:00
continue;
}
2024-04-08 11:12:41 +01:00
const activeOrder = apiUser?.data[0]?.orders.find(order => {
return order.package_id === packageRole.packageID && order.status === "active";
});
if (!activeOrder) {
const member = await guild.members.fetch(memberId).catch(() => {});
if (member) {
2024-04-02 04:02:22 +01:00
await delay(5000);
2024-04-08 11:12:41 +01:00
await member.roles.remove(packageRole.roleID).catch(() => {
console.log("Role couldn't be found.");
2024-04-02 04:02:22 +01:00
});
2024-04-08 11:12:41 +01:00
console.log(`Removed ${role.name} from ${member.displayName}`);
2024-04-02 04:02:22 +01:00
}
}
}
}
}
2024-04-08 11:12:41 +01:00
};
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));