Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c9ed2b

Browse files
committedAug 6, 2024·
Complete implementation of rejoin notices
1 parent ccf2372 commit 2c9ed2b

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed
 

‎events/guildMemberAdd.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { Events } = require("discord.js");
22

33
const CSESOC_SERVER_ID = "693779865916276746";
4-
const REPORT_CHANNEL_ID = "1225243371036082187";
4+
const REPORT_CHANNEL_ID = "1270283342176059443";
55

66
module.exports = {
77
name: Events.GuildMemberAdd,
@@ -13,11 +13,22 @@ module.exports = {
1313
// Get report channel
1414
if (member.user.bot || member.user.system || member.guild.id !== CSESOC_SERVER_ID) return;
1515

16-
userDB.user_join(member.id).then((joinType) => {
17-
if (joinType === "rejoin") {
18-
const reportChannel = member.guild.channels.cache.get(REPORT_CHANNEL_ID);
19-
reportChannel.send(`${member.user} (${member.user.tag}) has rejoined the server.`);
20-
}
16+
// Get old user info before joining
17+
userDB.get_user_info(member.id).then((user_data) => {
18+
userDB.user_join(member.id).then((joinType) => {
19+
if (joinType === "rejoin") {
20+
// Fetch the channel to output details
21+
const reportChannel = member.guild.channels.cache.get(REPORT_CHANNEL_ID);
22+
23+
// Fetch formatted date values from joining and leaving events
24+
const joinDate = user_data.joinDate.toLocaleDateString("en-AU");
25+
const leaveDate = user_data.leaveDate.toLocaleDateString("en-AU");
26+
27+
reportChannel.send(
28+
`${member.user} (${member.user.tag}) has rejoined the server. [Last in server: ${leaveDate}, Last joined: ${joinDate}]`,
29+
);
30+
}
31+
});
2132
});
2233
},
2334
};

‎lib/database/database.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,38 @@ class DBuser {
250250
return type;
251251
}
252252

253+
async get_user_info(userid) {
254+
const client = await this.pool.connect();
255+
let info = null;
256+
257+
try {
258+
await client.query("BEGIN");
259+
260+
const query = "select * from users where userid = $1";
261+
const values = [userid];
262+
const result = await client.query(query, values);
263+
264+
if (result.rows.length != 0) {
265+
const row = result.rows[0];
266+
267+
info = {
268+
userId: row["userid"],
269+
joinDate: row["joindate"],
270+
leaveDate: row["leavedate"],
271+
userLeft: row["userleft"],
272+
};
273+
}
274+
} catch (ex) {
275+
console.log(`An unexpected error occurred: ${ex}`);
276+
} finally {
277+
await client.query("ROLLBACK");
278+
client.release();
279+
// console.log("Client released successfully.")
280+
}
281+
282+
return info;
283+
}
284+
253285
// Adding a user role
254286
async add_user_role(userid, role) {
255287
const client = await this.pool.connect();

0 commit comments

Comments
 (0)
Please sign in to comment.