Skip to content

Commit 278c3ce

Browse files
Merge pull request #180 from Pdzly/feature/bump-leaderboards
2 parents be426ec + 0eb18e0 commit 278c3ce

13 files changed

Lines changed: 301 additions & 68 deletions

src/Config.prod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const config: Config = {
2222
commands: {
2323
daily: "1059214166075912225",
2424
},
25+
2526
roles: {
2627
tiers: [
2728
"821743100203368458", // @everyone (tier 0)

src/Config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const config: Config = {
1212
auditLog: "994623474557538415",
1313
modLog: "994623474557538415",
1414
general: "904478147351806015",
15+
leaderboard: "904478147351806015",
1516
},
1617
starboard: {
1718
emojiId: "⭐",

src/config.type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Config {
2020
modLog: string;
2121
introductions?: string;
2222
general: string;
23+
leaderboard?: string;
2324
};
2425
starboard: {
2526
emojiId: string;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import ImageForwarderModule from "./modules/imageForwarder.module.js";
1515
import { InformationModule } from "./modules/information/information.module.js";
1616
import JoinLeaveMessageModule from "./modules/joinLeaveMessage.module.js";
1717
import { LanguageStatusModule } from "./modules/languageStatus.module.js";
18+
import LeaderboardModule from "./modules/leaderboard/leaderboard.module.js";
1819
import { LearningModule } from "./modules/learning/learning.module.js";
1920
import { ModerationModule } from "./modules/moderation/moderation.module.js";
2021
import { ModmailModule } from "./modules/modmail/modmail.module.js";
@@ -65,6 +66,7 @@ export const moduleManager = new ModuleManager(
6566
ModerationModule,
6667
StarboardModule,
6768
ModmailModule,
69+
LeaderboardModule,
6870
],
6971
);
7072

src/modules/core/bump.listener.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
Client,
44
Message,
55
MessageInteraction,
6+
MessageInteractionMetadata,
67
PartialTextBasedChannelFields,
78
User,
89
} from "discord.js";
@@ -67,7 +68,7 @@ test("simple bump", async () => {
6768
const { ddUser, fakeUser, mockReact, mockChannel } = await setupMocks();
6869
await handleBumpStreak(
6970
ddUser,
70-
{ user: fakeUser } as unknown as MessageInteraction,
71+
{ user: fakeUser } as unknown as MessageInteractionMetadata,
7172
{ channel: mockChannel, react: mockReact } as unknown as Message & {
7273
channel: PartialTextBasedChannelFields;
7374
},
@@ -88,7 +89,7 @@ test("simple bump with streak", async () => {
8889

8990
await handleBumpStreak(
9091
ddUser,
91-
{ user: fakeUser } as unknown as MessageInteraction,
92+
{ user: fakeUser } as unknown as MessageInteractionMetadata,
9293
{ channel: mockChannel, react: mockReact } as unknown as Message & {
9394
channel: PartialTextBasedChannelFields;
9495
},
@@ -113,7 +114,7 @@ test("simple bump with big streak", async () => {
113114
}
114115
await handleBumpStreak(
115116
ddUser,
116-
{ user: fakeUser } as unknown as MessageInteraction,
117+
{ user: fakeUser } as unknown as MessageInteractionMetadata,
117118
{ channel: mockChannel, react: mockReact } as unknown as Message & {
118119
channel: PartialTextBasedChannelFields;
119120
},
@@ -137,7 +138,7 @@ test("speedy bump ", async () => {
137138
setLastBumpNotificationTime(new Date(Date.now() - 1000));
138139
await handleBumpStreak(
139140
ddUser,
140-
{ user: fakeUser } as unknown as MessageInteraction,
141+
{ user: fakeUser } as unknown as MessageInteractionMetadata,
141142
{ channel: mockChannel, react: mockReact } as unknown as Message & {
142143
channel: PartialTextBasedChannelFields;
143144
},
@@ -170,7 +171,9 @@ test("End other user's streak", async () => {
170171
});
171172
await handleBumpStreak(
172173
otherUser,
173-
{ user: createFakeUser(otherUserId) } as unknown as MessageInteraction,
174+
{
175+
user: createFakeUser(otherUserId),
176+
} as unknown as MessageInteractionMetadata,
174177
{ channel: mockChannel, react: mockReact } as unknown as Message & {
175178
channel: PartialTextBasedChannelFields;
176179
},
@@ -243,7 +246,9 @@ test("End other user's streak with real data", async () => {
243246

244247
await handleBumpStreak(
245248
otherUser,
246-
{ user: createFakeUser(otherUserId) } as unknown as MessageInteraction,
249+
{
250+
user: createFakeUser(otherUserId),
251+
} as unknown as MessageInteractionMetadata,
247252
{ channel: mockChannel, react: mockReact } as unknown as Message & {
248253
channel: PartialTextBasedChannelFields;
249254
},

src/modules/core/bump.listener.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type EmojiIdentifierResolvable,
55
InteractionType,
66
type Message,
7-
type MessageInteraction,
7+
type MessageInteractionMetadata,
88
type PartialTextBasedChannelFields,
99
} from "discord.js";
1010
import { config } from "../../Config.js";
@@ -16,6 +16,7 @@ import {
1616
getAllBumps,
1717
getBumpStreak,
1818
getStreaks,
19+
type Streak,
1920
} from "../../store/models/bumps.js";
2021
import { type DDUser, getOrCreateUserById } from "../../store/models/DDUser.js";
2122
import { fakeMention, mentionIfPingable } from "../../util/users.js";
@@ -42,10 +43,9 @@ export function setLastBumpNotificationTime(date: Date) {
4243
lastBumpNotificationTime = date;
4344
}
4445

45-
// noinspection JSDeprecatedSymbols
4646
export async function handleBumpStreak(
4747
bumper: DDUser,
48-
interactionOld: MessageInteraction,
48+
interactionOld: MessageInteractionMetadata,
4949
message: Message & {
5050
channel: PartialTextBasedChannelFields;
5151
},
@@ -69,15 +69,14 @@ export async function handleBumpStreak(
6969
streak.current === 1 // just started a new streak
7070
) {
7171
// allStreaks[-1] will be the current streak
72-
const mostRecent = allStreaks[allStreaks.length - 2]; // so check the one before that
72+
const mostRecent = allStreaks.at(-2) as {
73+
userId: bigint;
74+
} & Streak; // so check the one before that
7375
logger.debug(`Most recent streak:`, mostRecent);
74-
logger.debug(
75-
"Most recent streaks:",
76-
allStreaks.slice(allStreaks.length - 5),
77-
);
76+
logger.debug("Most recent streaks:", allStreaks.slice(-5));
7877
if (mostRecent.userId !== bumper.id && mostRecent.current > 2) {
7978
const user = await client.users.fetch(mostRecent.userId.toString());
80-
message.channel.send(
79+
await message.channel.send(
8180
`☠️ ${mentionIfPingable(interactionOld.user)} ended ${fakeMention(user)}'s bump streak of ${mostRecent.current}!`,
8281
);
8382
}
@@ -88,7 +87,7 @@ export async function handleBumpStreak(
8887
const timeSinceLastBump = Date.now() - lastBumpNotificationTime.getTime();
8988
if (timeSinceLastBump < 30000) {
9089
// this might seem generous, but in reality when you factor in the discord delay, even if you react instantaneously on your screen you can still be too slow
91-
message.channel.send(
90+
await message.channel.send(
9291
`⚡⚡⚡ ${fakeMention(interactionOld.user)} bumped in just **${timeSinceLastBump / 1000}s**!`,
9392
);
9493
} else {
@@ -104,12 +103,14 @@ export async function handleBumpStreak(
104103

105104
if (streak.current === streak.highest) {
106105
// new high score!
107-
message.channel.send(
106+
await message.channel.send(
108107
`${mentionIfPingable(interactionOld.user)}, you beat your max bump streak and are now on a streak of ${streak.current}! Keep it up!`,
109108
);
110109
}
111110

112-
const highestStreakEver = allStreaks.sort((a, b) => b.highest - a.highest)[0];
111+
const highestStreakEver = allStreaks.toSorted(
112+
(a, b) => b.highest - a.highest,
113+
)[0];
113114
logger.debug("Highest streak ever: %O", highestStreakEver);
114115
logger.debug("This streak: %O", streak);
115116
if (
@@ -121,17 +122,16 @@ export async function handleBumpStreak(
121122
) {
122123
// if they currently have the highest streak
123124
logger.debug("User has the highest streak");
124-
message.channel.send(
125+
await message.channel.send(
125126
`🔥🔥🔥🔥🔥 ${mentionIfPingable(interactionOld.user)}, you have the highest EVER bump streak in the server of ${highestStreakEver.highest}! Keep it up!`,
126127
);
127128
}
128129
}
129130

130-
// noinspection JSDeprecatedSymbols
131131
export async function handleBump(
132132
client: Client,
133133
bumper: DDUser,
134-
interactionOld: MessageInteraction,
134+
interactionOld: MessageInteractionMetadata,
135135
message: Message & {
136136
channel: PartialTextBasedChannelFields;
137137
},
@@ -144,37 +144,34 @@ export async function handleBump(
144144
}
145145

146146
export const BumpListener: EventListener = {
147-
ready: async (client) => {
147+
clientReady: async (client) => {
148148
scheduleBumpReminder(client);
149149
},
150150
messageCreate: async (client, message) => {
151151
const interaction = message.interactionMetadata;
152152

153-
if (
154-
!interaction ||
155-
!(interaction.type === InteractionType.ApplicationCommand)
156-
)
153+
if (!interaction || interaction.type !== InteractionType.ApplicationCommand)
157154
return;
158155
if (message.author.id !== "302050872383242240") return; // /disboard user id
159-
// noinspection JSDeprecatedSymbols don't think there's another way of doing this
156+
157+
// noinspection JSDeprecatedSymbols don't think there's another way of doing this ( yes there is )
160158
const interactionOld = message.interaction;
161159
if (interactionOld?.commandName !== "bump") return;
162-
163160
// since the bump failed message is ephemeral, we know if we can see the message then the bump succeeded!
164-
const ddUser = await getOrCreateUserById(BigInt(interactionOld.user.id));
161+
const ddUser = await getOrCreateUserById(BigInt(interaction.user.id));
165162

166163
// Bump
167164
await Bump.create({
168165
messageId: BigInt(message.id),
169-
userId: BigInt(interactionOld.user.id),
166+
userId: BigInt(interaction.user.id),
170167
timestamp: new Date(),
171168
});
172169
logger.info(
173-
`User ${interactionOld.user.id} bumped! Total bumps: ${await ddUser.countBumps()}`,
170+
`User ${interaction.user.id} bumped! Total bumps: ${await ddUser.countBumps()}`,
174171
);
175172
clearBumpsCache();
176173
await ddUser.save();
177-
await handleBump(client, ddUser, interactionOld, message);
174+
await handleBump(client, ddUser, interaction, message);
178175
},
179176
};
180177
const streakReacts: EmojiIdentifierResolvable[] = [

0 commit comments

Comments
 (0)