Skip to content

Commit d96fce0

Browse files
committed
feat: added Events.GuildMemberUpdate event
1 parent 3e7863a commit d96fce0

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ bun run dev:bot
4242
## Stable 1.0
4343
* A new, redesigned site using NextJS
4444
* Users that are no longer in your server will be hidden from the public leaderboard
45+
* Implemented `Events.GuildMemberUpdate` event
4546

4647
## Beta 0.1
4748
Thanks to @ToastedDev for his contributions to the bot. Here are some changes that were made
@@ -58,7 +59,8 @@ Thanks to @ToastedDev for his contributions to the bot. Here are some changes th
5859
# Roadmap
5960
Anything crossed out means that it's been done on the dev branch, but not pushed out to production
6061
* ~~Rewritten site using NextJS~~
61-
* Auto-updating cached user information
62+
* ~~Auto-updating cached user information~~ (GUILD ONLY)
63+
* Auto-updating cached user information (GLOBAL)
6264
* Better privacy controls
6365
* ~~Live updates~~
6466
* Track guilds and users xp

api/src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ app.post("/post/:guild/:user", authMiddleware, async (req, res) => {
7373
console.log(req.body);
7474
const xpValue = parseInt(xp);
7575

76+
if (xpValue == 0) {
77+
const updateQuery = `
78+
INSERT INTO users
79+
(id, guild_id, pfp, name, nickname)
80+
VALUES (?, ?, ?, ?, ?)
81+
ON DUPLICATE KEY UPDATE
82+
pfp = VALUES(pfp),
83+
name = VALUES(name),
84+
nickname = VALUES(nickname)
85+
`;
86+
87+
pool.query(
88+
updateQuery,
89+
[
90+
user,
91+
guild,
92+
pfp,
93+
name,
94+
nickname,
95+
],
96+
(err) => {
97+
if (err) {
98+
console.error("Error updating XP:", err);
99+
return res
100+
.status(500)
101+
.json({ success: false, message: "Internal server error" });
102+
} else {
103+
res
104+
.status(200)
105+
.json({
106+
success: true
107+
});
108+
}
109+
},
110+
);
111+
}
112+
76113
const [err, result] = await getUser(user, guild);
77114

78115
if (err) {

bot/src/events/guildMemberUpdate.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Events } from "discord.js";
2+
import client from "../index";
3+
import { makePOSTRequest } from "../utils/requestAPI";
4+
5+
client.on(Events.GuildMemberUpdate, async (_oldMember, newMember) => {
6+
console.log(`Updating user ${newMember.user.username} for ${newMember.guild.name}`);
7+
if (newMember.user.bot) return;
8+
try {
9+
await makePOSTRequest(newMember.guild.id, newMember.id, null, null, newMember.displayAvatarURL(), newMember.user.username, newMember.nickname ?? newMember.user.globalName ?? newMember.user.username);
10+
console.log(`Updated user ${newMember.user.username} for ${newMember.guild.name}`);
11+
} catch (e) {
12+
console.error(e);
13+
}
14+
})

bot/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const client = new Client({
2323
GatewayIntentBits.Guilds,
2424
GatewayIntentBits.GuildMessages,
2525
GatewayIntentBits.MessageContent,
26+
GatewayIntentBits.GuildMembers,
2627
],
2728
});
2829

bot/src/utils/requestAPI.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import handleLevelChange from "./handleLevelChange";
22

3-
export async function makePOSTRequest(guild: string, user: string, channel: string, xp: number, pfp: string, name: string, nickname: string) {
3+
export async function makePOSTRequest(guild: string, user: string, channel: string | null, xp: number | null, pfp: string, name: string, nickname: string) {
4+
xp = xp ?? 0
45
await fetch(`http://localhost:18103/post/${guild}/${user}`, {
56
headers: {
67
'Content-Type': 'application/json',
@@ -11,6 +12,7 @@ export async function makePOSTRequest(guild: string, user: string, channel: stri
1112
}).then(res => {
1213
return res.json()
1314
}).then(data => {
15+
if (!channel) return
1416
if (data.sendUpdateEvent) handleLevelChange(guild, user, channel, data.level)
1517
})
1618
}

web/README.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

web/pages/leaderboard/[server].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class IndexPage extends Component<object, PageState> {
170170

171171
return {
172172
odometerPoints: points,
173-
odometerMembersBeingTracked: data.guild.members,
173+
odometerMembersBeingTracked: data.leaderboard.length,
174174
odometerMembers: data.guild.members,
175175
chartOptions: {
176176
...prevState.chartOptions,
@@ -364,7 +364,7 @@ export async function getServerSideProps(context: { query: { server: string }; }
364364
discordGuildName: data.guild.name,
365365
odometerPoints: data.totalXp,
366366
odometerMembers: data.guild.members,
367-
odometerMembersBeingTracked: data.guild.members,
367+
odometerMembersBeingTracked: data.leaderboard.length,
368368
leaderboard: data.leaderboard,
369369
}
370370
};

0 commit comments

Comments
 (0)