-
Notifications
You must be signed in to change notification settings - Fork 18
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
Codeycoin leaderboard #263
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b4faa0
initial implementation of leaderboard
mcpenguin 3720b58
more changes
mcpenguin 5b250bd
added current position to leaderboard
mcpenguin 6a826e2
Added user balance
mcpenguin 5462416
Addressed comments
mcpenguin 914d3a2
Fixed linter
mcpenguin d1a3435
Addressed comments
mcpenguin 91771d7
Addressed comments again
mcpenguin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { container, SapphireClient } from '@sapphire/framework'; | ||
import { MessageEmbed } from 'discord.js'; | ||
import { | ||
CodeyCommandDetails, | ||
CodeyCommandResponseType, | ||
getUserIdFromMessage, | ||
SapphireMessageExecuteType, | ||
SapphireMessageResponse | ||
} from '../../codeyCommand'; | ||
import { | ||
getCurrentCoinLeaderboard, | ||
getCoinBalanceByUserId, | ||
UserCoinEntry, | ||
getUserIdCurrentCoinPosition | ||
} from '../../components/coin'; | ||
import { EMBED_COLOUR } from '../../utils/embeds'; | ||
|
||
// How many people are shown on the leaderboard | ||
const limit = 10; | ||
|
||
const getCurrentCoinLeaderboardEmbed = async ( | ||
client: SapphireClient<boolean>, | ||
leaderboard: UserCoinEntry[], | ||
currentUserId: string | ||
): Promise<MessageEmbed> => { | ||
// Initialise user's coin balance if they have not already | ||
const userBalance = await getCoinBalanceByUserId(currentUserId); | ||
const currentPosition = await getUserIdCurrentCoinPosition(currentUserId); | ||
|
||
let currentLeaderboardText = ''; | ||
for (let i = 0; i < limit; i++) { | ||
if (leaderboard.length <= i) break; | ||
const userCoinEntry = leaderboard[i]; | ||
const user = await client.users.fetch(userCoinEntry.user_id); | ||
const userTag = user?.tag ?? '<unknown>'; | ||
const userCoinEntryText = `${i + 1}. ${userTag} - ${userCoinEntry.balance} 🪙\n`; | ||
currentLeaderboardText += userCoinEntryText; | ||
} | ||
const currentLeaderboardEmbed = new MessageEmbed() | ||
.setColor(EMBED_COLOUR) | ||
.setTitle('CodeyCoin Leaderboard') | ||
.setDescription(currentLeaderboardText); | ||
|
||
currentLeaderboardEmbed.addFields({ | ||
name: 'Your Position', | ||
value: `You are currently **#${currentPosition}** in the leaderboard with ${userBalance} 🪙.` | ||
}); | ||
|
||
return currentLeaderboardEmbed; | ||
}; | ||
|
||
const coinCurrentLeaderboardExecuteCommand: SapphireMessageExecuteType = async ( | ||
client, | ||
messageFromUser, | ||
_args | ||
): Promise<SapphireMessageResponse> => { | ||
const userId = getUserIdFromMessage(messageFromUser); | ||
const leaderboard = await getCurrentCoinLeaderboard(); | ||
return getCurrentCoinLeaderboardEmbed(client, leaderboard, userId); | ||
}; | ||
|
||
export const coinCurrentLeaderboardCommandDetails: CodeyCommandDetails = { | ||
name: 'leaderboard', | ||
aliases: ['lb'], | ||
description: 'Get the current coin leaderboard.', | ||
detailedDescription: `**Examples:** | ||
\`${container.botPrefix}coin lb\` | ||
\`${container.botPrefix}coin leaderboard\``, | ||
|
||
isCommandResponseEphemeral: false, | ||
messageWhenExecutingCommand: 'Getting the current coin leaderboard...', | ||
executeCommand: coinCurrentLeaderboardExecuteCommand, | ||
codeyCommandResponseType: CodeyCommandResponseType.EMBED, | ||
|
||
options: [], | ||
subcommandDetails: {} | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't ctrl f on phone but can u just lmk where this is used XD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the leaderboard :))