Last active
December 15, 2020 01:19
-
-
Save CRodriguez25/8aad73e0848d249cc68f751bfbc69a5a to your computer and use it in GitHub Desktop.
A simple extension method for rendering progress bars with Discord.Net EmbedBuilders
This file contains hidden or 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
public static class EmbedBuilderExtensions | |
{ | |
public static EmbedBuilder AddProgressBar( | |
this EmbedBuilder embedBuilder, | |
string title, | |
float value, | |
float min, | |
float max, | |
bool inline = false, | |
string filledEmoji = ":green_square:", | |
string emptyEmoji = ":black_large_square:", | |
int progressBarSize = 13) | |
{ | |
float Normalize() | |
{ | |
return (value - min) / (max - min); | |
} | |
string RenderProgressBar() | |
{ | |
var normalized = Normalize(); | |
var numberOfGreenSquares = (int)Math.Floor(progressBarSize * normalized); | |
var numberOfEmptySquares = progressBarSize - numberOfGreenSquares; | |
var result = ""; | |
foreach(var index in Enumerable.Range(0, numberOfGreenSquares)) | |
result += filledEmoji; | |
foreach(var index in Enumerable.Range(0, numberOfEmptySquares)) | |
result += emptyEmoji; | |
return result; | |
} | |
return embedBuilder.AddField(title, RenderProgressBar(), inline); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of the rendered progress bars