Skip to content

[DISC-110] Adding Course Chats 1-year "Subscription" #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 17, 2024
Merged
9 changes: 9 additions & 0 deletions commands/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ module.exports = {
ephemeral: true,
});
}
// // Add it to the existing database to track.
/** @type {DBuser} */
const userDB = global.userDB;
await userDB.add_user_role(interaction.user.id, role.name);

// If they don't, let's add the role to them
await interaction.member.roles.add(role);

return await interaction.reply({
content: `✅ | Added you to the chat for \`${course_with_alias}\`.`,
ephemeral: true,
Expand Down Expand Up @@ -190,6 +195,10 @@ module.exports = {
in_overwrites(permissions, role.id)
) {
// If they do remove the role
/** @type {DBuser} */
const userDB = global.userDB;
userDB.remove_user_role(interaction.user.id, role.name);

await interaction.member.roles.remove(role);
return await interaction.reply({
content: `✅ | Removed you from the role and chat for \`${course}\`.`,
Expand Down
46 changes: 46 additions & 0 deletions events/db_ready.js
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();
},
};
77 changes: 49 additions & 28 deletions lib/database/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DBuser {
load_db_login() {
// Get document, or throw exception on error
try {
const doc = yaml.load(fs.readFileSync("../../config/database.yml"));
const doc = yaml.load(fs.readFileSync("./config/database.yml"));
return doc;
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -90,14 +90,14 @@ class DBuser {
const client = await this.pool.connect();
try {
if ((await this.check_table("users")) == false) {
// console.log("Running create_table")
console.log("Running creating user table");
await client.query("BEGIN");
const query = `CREATE TABLE users (
userid INTEGER PRIMARY KEY,
joindate DATE NOT NULL,
leavedate DATE,
userleft BOOLEAN
);`;
)`;
await client.query(query);
await client.query("COMMIT");
}
Expand All @@ -114,21 +114,19 @@ class DBuser {
async create_table_user_roles() {
const client = await this.pool.connect();
try {
if ((await this.check_table("user_roles")) == false) {
// console.log("Running create_table")
await client.query("BEGIN");
const query = `CREATE TABLE user_roles (
rid INTEGER PRIMARY KEY,
userid INTEGER NOT NULL,
role varchar(64),
FOREIGN KEY (userid)
REFERENCES users (userid)
);`;
await client.query(query);
await client.query("COMMIT");
}
// if ((await this.check_table("user_roles")) == false) {

await client.query("BEGIN");
const query = `CREATE TABLE user_roles (
rid INTEGER PRIMARY KEY,
userid BIGINT NOT NULL,
role varchar(64),
time_assigned TIMESTAMP
)`;
await client.query(query);
await client.query("COMMIT");
} catch (ex) {
console.log(`Something wrong happend ${ex}`);
console.log(`Something wrong happende:${ex}`);
} finally {
await client.query("ROLLBACK");
client.release();
Expand Down Expand Up @@ -170,12 +168,12 @@ class DBuser {
// console.log("Running create_table")
await client.query("BEGIN");
const query = `CREATE TABLE user_permissions (
pid INTEGER PRIMARY KEY,
userid INTEGER NOT NULL,
permission varchar(64),
FOREIGN KEY (userid)
REFERENCES users (userid)
);`;
pid INTEGER PRIMARY KEY,
userid INTEGER NOT NULL,
permission varchar(64),
FOREIGN KEY (userid)
REFERENCES users (userid)
);`;
await client.query(query);
await client.query("COMMIT");
}
Expand Down Expand Up @@ -250,18 +248,18 @@ class DBuser {
const client = await this.pool.connect();
try {
await client.query("BEGIN");

let query = "SELECT max(rid) from user_roles";
let result = await client.query(query);

const count = result.rows[0]["max"] + 1;
query = "INSERT INTO user_roles (RID, USERID, ROLE) VALUES ($1,$2,$3)";
query =
"INSERT INTO user_roles (RID, USERID, ROLE, TIME_ASSIGNED) VALUES ($1,$2,$3,NOW())";
const values = [count, userid, role];
result = await client.query(query, values);

await client.query("COMMIT");
} catch (ex) {
console.log(`Something wrong happend ${ex}`);
console.log(`Something wrong happend when trying to add user role to table ${ex}`);
} finally {
await client.query("ROLLBACK");
client.release();
Expand All @@ -275,8 +273,7 @@ class DBuser {
try {
await client.query("BEGIN");
const values = [userid, role];
const query = `DELETE FROM user_roles
where userid = $1 and role = $2`;
const query = `DELETE FROM user_roles where userid = $1 and role = $2`;
await client.query(query, values);
await client.query("COMMIT");
} catch (ex) {
Expand Down Expand Up @@ -456,6 +453,30 @@ class DBuser {
// console.log("Client released successfully.")
}
}

async checkTimeAssigned() {
const client = await this.pool.connect();
try {
// Query to select rows where time_assigned is older than 1 hour
const query = `
SELECT * FROM user_roles WHERE time_assigned < NOW() - interval '1 year'
`;

const result = await client.query(query);

const old_roles = result.rows.map((row) => ({
role_name: row.role,
userid: row.userid,
}));

return old_roles;
} catch (error) {
console.error(error);
return [];
} finally {
client.release();
}
}
}

module.exports = {
Expand Down
44 changes: 43 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"chartjs-node-canvas": "^4.1.6",
"cheerio": "^1.0.0-rc.12",
"closest-match": "1.3.3",
"cron": "^3.1.7",
"csv-parser": "3.0.0",
"csv-writer": "1.6.0",
"discord-api-types": "0.37.90",
Expand All @@ -41,7 +42,7 @@
"dotenv": "16.4.5",
"js-yaml": "4.1.0",
"mathjs": "^13.0.0",
"node-cron": "^3.0.2",
"node-cron": "^3.0.3",
"nodemailer": "6.9.13",
"nodemon": "^3.0.0",
"pg": "8.12.0",
Expand Down
Loading