-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DISC-110] Adding Course Chats 1-year "Subscription" (#191)
* Added database implementation of user roles when assigned and removed * Added time checking function for expired user_roles and added time assigned column to user_roles table * Added on ready file with cron.js to perform daily check of old_roles * Adding new package cron for scheduling * fixing lint issues * Fixing prettier issues * Package changes * Restoring develop package lock.json and fixing db_ready * Moving guild and roles out of for loop to remove redundant fetching
- Loading branch information
1 parent
5298768
commit eecb1b5
Showing
5 changed files
with
149 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// @ts-check | ||
const { DBuser } = require("../lib/database/database"); | ||
const { CronJob } = require("cron"); | ||
|
||
const CSESOC_SERVER_ID = "693779865916276746"; | ||
// const TEST_SERVER_ID = "1220297696829509713"; | ||
|
||
module.exports = { | ||
name: "ready", | ||
once: true, | ||
async execute(client) { | ||
/** @type {DBuser} */ | ||
const userDB = new DBuser(); | ||
global.userDB = userDB; | ||
|
||
// Set up an automatic database check to see if there is any out of date roles. | ||
const role_job = new CronJob("0 0 12 * * *", async function () { | ||
console.log("Performing daily check of old roles at 12:00pm"); | ||
|
||
const old_roles = await userDB.checkTimeAssigned(); | ||
const guild = await client.guilds.fetch(CSESOC_SERVER_ID); | ||
const roles = await guild.roles.fetch(); | ||
|
||
for (const removed_role of old_roles) { | ||
try { | ||
const member = await guild.members.fetch(removed_role.userid); | ||
const role = roles.find((r) => r.name === removed_role.role_name); | ||
|
||
if (member && role) { | ||
await member.roles.remove(role); | ||
await userDB.remove_user_role(removed_role.userid, removed_role.role_name); | ||
// console.log(`Removed role ${removed_role.role_name} from user ${removed_role.userid}`); | ||
} else { | ||
console.log( | ||
`Could not find role ${removed_role.role_name} or user ${removed_role.userid}`, | ||
); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
}); | ||
|
||
role_job.start(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters