116 lines
No EOL
3 KiB
JavaScript
116 lines
No EOL
3 KiB
JavaScript
const api = require("./api");
|
|
const { guild_id, api_role_id, package_roles } = require("../config");
|
|
|
|
const users = {};
|
|
|
|
module.exports = async client => {
|
|
const guild = await client.guilds.fetch(guild_id).catch(() => {});
|
|
if (!guild) {
|
|
return;
|
|
}
|
|
|
|
let oauthUsers = await api.getOauthUsers();
|
|
oauthUsers.forEach(user => {
|
|
users[user.user_id] = user.data.id;
|
|
});
|
|
|
|
checkNewOauths();
|
|
async function checkNewOauths() {
|
|
setTimeout(checkNewOauths, 120000);
|
|
const newOauths = await api.getNewOauths();
|
|
if (!newOauths) {
|
|
return;
|
|
}
|
|
|
|
newOauths.data.forEach(async oauth => {
|
|
users[oauth.user_id] = oauth.data.id;
|
|
const member = await guild.members.fetch(oauth.data.id).catch(() => {});
|
|
if (!member) {
|
|
return;
|
|
}
|
|
|
|
if (member.roles.cache.has(api_role_id)) {
|
|
return;
|
|
}
|
|
|
|
return member.roles.add(api_role_id).catch(() => {});
|
|
});
|
|
}
|
|
|
|
checkActiveOrders();
|
|
async function checkActiveOrders() {
|
|
setTimeout(checkActiveOrders, 300000);
|
|
const activeOrders = await api.getActiveOrders();
|
|
if (!activeOrders) {
|
|
return;
|
|
}
|
|
|
|
activeOrders.forEach(async order => {
|
|
const discordId = users[order.user_id];
|
|
if (!discordId) {
|
|
return;
|
|
}
|
|
|
|
const member = await guild.members.fetch(discordId).catch(() => {});
|
|
if (!member) {
|
|
return;
|
|
}
|
|
|
|
const packageRole = package_roles.find(role => role.packageID === order.package_id);
|
|
if (!packageRole) {
|
|
return;
|
|
}
|
|
|
|
return member.roles.add(packageRole.roleID).catch(() => {});
|
|
});
|
|
}
|
|
|
|
checkPackageRoles();
|
|
async function checkPackageRoles() {
|
|
setTimeout(checkPackageRoles, 1800000);
|
|
|
|
for (const packageRole of package_roles) {
|
|
const role = await guild.roles.fetch(packageRole.roleID).catch(() => {
|
|
console.log("Role not found.");
|
|
});
|
|
if (!role) {
|
|
continue;
|
|
}
|
|
|
|
const memberIds = role.members.keys();
|
|
if (memberIds.size === 0) {
|
|
continue;
|
|
}
|
|
|
|
for (const memberId of memberIds) {
|
|
const oauthUser = oauthUsers.find(user => user.data.id === memberId);
|
|
if (!oauthUser) {
|
|
continue;
|
|
}
|
|
|
|
await delay(5000);
|
|
const apiUser = await api.getUser(oauthUser.email);
|
|
if (!apiUser || apiUser.data.length === 0) {
|
|
continue;
|
|
}
|
|
|
|
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) {
|
|
await delay(5000);
|
|
await member.roles.remove(packageRole.roleID).catch(() => {
|
|
console.log("Role couldn't be found.");
|
|
});
|
|
console.log(`Removed ${role.name} from ${member.displayName}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); |