made tengwar command rate-limited

This commit is contained in:
Andrew Lalis 2017-08-03 08:37:32 +02:00 committed by Andrew Lalis
parent 3ef3f38fb0
commit 7a4b3e8c36
3 changed files with 11 additions and 17 deletions

View File

@ -6,9 +6,8 @@ import handiebot.utils.MessageUtils;
import net.agspace.TengwarImageGenerator; import net.agspace.TengwarImageGenerator;
import net.agspace.Translator; import net.agspace.Translator;
import java.io.FileNotFoundException;
import static handiebot.HandieBot.resourceBundle; import static handiebot.HandieBot.resourceBundle;
import static handiebot.utils.MessageUtils.sendFile;
import static handiebot.utils.MessageUtils.sendMessage; import static handiebot.utils.MessageUtils.sendMessage;
/** /**
@ -31,17 +30,14 @@ public class TengwarCommand extends ContextCommand {
String input = MessageUtils.getTextFromArgs(context.getArgs(), 1); String input = MessageUtils.getTextFromArgs(context.getArgs(), 1);
if (context.getArgs()[0].equalsIgnoreCase("to")){ if (context.getArgs()[0].equalsIgnoreCase("to")){
String result = Translator.translateToTengwar(input); String result = Translator.translateToTengwar(input);
try { sendFile(TengwarImageGenerator.generateImage(result,
//TODO: replace with rate-limited send method. 600,
context.getChannel().sendFile("Raw text: `" +result+'`', TengwarImageGenerator.generateImage(result, 24f,
600, false,
24f, false,
false, System.getProperty("user.home")+"/.handiebot/tengwarTemp.png"),
false, "Raw text: `" +result+'`',
System.getProperty("user.home")+"/.handiebot/tengwarTemp.png")); context.getChannel());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else if (context.getArgs()[0].equalsIgnoreCase("from")){ } else if (context.getArgs()[0].equalsIgnoreCase("from")){
sendMessage(Translator.translateToEnglish(input), context.getChannel()); sendMessage(Translator.translateToEnglish(input), context.getChannel());
} }

View File

@ -120,7 +120,6 @@ public class Playlist {
* Gets a 'shuffled index' from a given list length. That means: * Gets a 'shuffled index' from a given list length. That means:
* - A random number from 0 to (listLength-1) - threshold*(listLength), where threshold is some percentage of * - A random number from 0 to (listLength-1) - threshold*(listLength), where threshold is some percentage of
* recent songs that should be ignored; for example, the most recent 20% of the playlist can be ignored. * recent songs that should be ignored; for example, the most recent 20% of the playlist can be ignored.
* - A greater likelihood for numbers closer to 0 (those which have not been played in a while).
* @param listLength The number of items in a potential list to choose from. * @param listLength The number of items in a potential list to choose from.
* @return A pseudo-random choice as to which item to pick from the list. * @return A pseudo-random choice as to which item to pick from the list.
*/ */
@ -128,7 +127,6 @@ public class Playlist {
float threshold = 0.2f; float threshold = 0.2f;
int trueLength = listLength - (int)(threshold*(float)listLength); int trueLength = listLength - (int)(threshold*(float)listLength);
Random rand = new Random(); Random rand = new Random();
//TODO Add in a small gradient in chance for a song to be picked.
return rand.nextInt(trueLength); return rand.nextInt(trueLength);
} }

View File

@ -49,11 +49,11 @@ public class MessageUtils {
* @param channel The channel to send the message on. * @param channel The channel to send the message on.
* @return The message that was sent, or null if the file could not be found. * @return The message that was sent, or null if the file could not be found.
*/ */
public static IMessage sendFile(File file, IChannel channel){ public static IMessage sendFile(File file, String content, IChannel channel){
return RequestBuffer.request(() -> { return RequestBuffer.request(() -> {
IMessage msg = null; IMessage msg = null;
try { try {
msg = channel.sendFile(file); msg = channel.sendFile(content, file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }