-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from berkingurcan/add-survey-leaderboard
Add survey leaderboard
- Loading branch information
Showing
17 changed files
with
590 additions
and
13 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
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
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,48 @@ | ||
import { | ||
ModalBuilder, | ||
TextInputBuilder, | ||
ActionRowBuilder, | ||
TextInputStyle, | ||
ChatInputCommandInteraction, | ||
} from "discord.js"; | ||
|
||
const editSurveyCountModal = async ( | ||
interaction: ChatInputCommandInteraction, | ||
) => { | ||
const modal = new ModalBuilder() | ||
.setCustomId(`editSurveyCountModal`) | ||
.setTitle("Edit Survey Count"); | ||
|
||
const usernameInput = new TextInputBuilder() | ||
.setCustomId("usernameInput") | ||
.setLabel("USERNAME") | ||
.setStyle(TextInputStyle.Short) | ||
.setMaxLength(45) | ||
.setRequired(true); | ||
|
||
const countInput = new TextInputBuilder() | ||
.setCustomId("countInput") | ||
.setLabel("Edit Survey Count +/-NUMBER") | ||
.setStyle(TextInputStyle.Short) | ||
.setMaxLength(10) | ||
.setRequired(true); | ||
|
||
const pointsInput = new TextInputBuilder() | ||
.setCustomId("pointsInput") | ||
.setLabel("Edit Survey Points +/-POINTS") | ||
.setStyle(TextInputStyle.Short) | ||
.setMaxLength(10) | ||
.setRequired(true); | ||
|
||
const firstActionRow = new ActionRowBuilder().addComponents(usernameInput); | ||
const secondActionRow = new ActionRowBuilder().addComponents(countInput); | ||
const thirdActionRow = new ActionRowBuilder().addComponents(pointsInput); | ||
|
||
modal.addComponents(firstActionRow, secondActionRow, thirdActionRow); | ||
|
||
await interaction.showModal(modal); | ||
}; | ||
|
||
export const handleEditSurveyCount = async (interaction: any) => { | ||
await editSurveyCountModal(interaction); | ||
}; |
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,99 @@ | ||
import { ChatInputCommandInteraction, GuildMember } from "discord.js"; | ||
|
||
export const handleLeaderboard = async ( | ||
interaction: ChatInputCommandInteraction, | ||
redisClient: any, | ||
) => { | ||
const userSurveyPoints = await redisClient.hGetAll("user:survey_points"); | ||
|
||
if (!userSurveyPoints || Object.keys(userSurveyPoints).length === 0) { | ||
await interaction.reply({ | ||
content: "No data available for the leaderboard.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
const entries = Object.entries(userSurveyPoints) | ||
.map(([username, pointsStr]) => ({ | ||
username, | ||
points: parseInt(pointsStr, 10), | ||
})) | ||
.filter((entry) => entry.points > 0); | ||
|
||
if (entries.length === 0) { | ||
await interaction.reply({ | ||
content: "No users have earned survey points yet.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
entries.sort((a, b) => b.points - a.points); | ||
|
||
const topEntries = entries; | ||
|
||
const trophyEmojis = ["🥇", "🥈", "🥉"]; | ||
const currentDate = new Date().toLocaleDateString("en-US", { | ||
month: "long", | ||
day: "numeric", | ||
year: "numeric", | ||
}); | ||
|
||
let leaderboardMessage = `🏆 **Survey Leaderboard | ${currentDate}** 🏆\n\n`; | ||
|
||
const members = interaction.guild?.members.cache; | ||
|
||
if (members) { | ||
const memberList = members.map((member: GuildMember) => ({ | ||
[member.user.username]: member.user.id, | ||
})); | ||
} | ||
|
||
for (const [index, entry] of topEntries.entries()) { | ||
const rank = index + 1; | ||
const username = entry.username; | ||
const points = entry.points; | ||
|
||
const rankStr = | ||
rank <= trophyEmojis.length ? trophyEmojis[rank - 1] : `${rank}.`; | ||
|
||
const member = members.find( | ||
(m: GuildMember) => | ||
m.user.username === username || m.user.globalName === username, | ||
); | ||
|
||
const userDisplayName = member ? `<@${member.user.id}>` : username; | ||
|
||
leaderboardMessage += `${rankStr} **${userDisplayName}** **|** ${points} Points\n`; | ||
} | ||
|
||
if (leaderboardMessage.length > 2000) { | ||
const messages = splitMessage(leaderboardMessage); | ||
await interaction.reply(messages[0]); | ||
for (let i = 1; i < messages.length; i++) { | ||
await interaction.followUp(messages[i]); | ||
} | ||
} else { | ||
await interaction.reply(leaderboardMessage); | ||
} | ||
}; | ||
|
||
function splitMessage(message: string, maxLength = 2000): string[] { | ||
const messages = []; | ||
let currentMessage = ""; | ||
|
||
const lines = message.split("\n"); | ||
for (const line of lines) { | ||
if ((currentMessage + line + "\n").length > maxLength) { | ||
messages.push(currentMessage); | ||
currentMessage = line + "\n"; | ||
} else { | ||
currentMessage += line + "\n"; | ||
} | ||
} | ||
if (currentMessage) { | ||
messages.push(currentMessage); | ||
} | ||
return messages; | ||
} |
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
Oops, something went wrong.