Skip to content

Commit 62b86fd

Browse files
Add the /math command with Javaluator API
1 parent 75f25a3 commit 62b86fd

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
<artifactId>okhttp</artifactId>
6464
<version>4.9.3</version>
6565
</dependency>
66+
<!-- http://javaluator.sourceforge.net/en/home/ -->
67+
<dependency>
68+
<groupId>com.fathzer</groupId>
69+
<artifactId>javaluator</artifactId>
70+
<version>3.0.3</version>
71+
</dependency>
6672
</dependencies>
6773

6874
<build>

src/main/java/technobot/commands/CommandRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public CommandRegistry(TechnoBot bot) {
134134
new PollCommand(bot),
135135
new InviteCommand(bot),
136136
new PremiumCommand(bot),
137+
new MathCommand(bot),
137138
new HelpCommand(bot) // The 'help' command MUST come last!!!
138139
);
139140
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package technobot.commands.utility;
2+
3+
import com.fathzer.soft.javaluator.DoubleEvaluator;
4+
import net.dv8tion.jda.api.EmbedBuilder;
5+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
6+
import net.dv8tion.jda.api.interactions.commands.OptionType;
7+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
8+
import technobot.TechnoBot;
9+
import technobot.commands.Category;
10+
import technobot.commands.Command;
11+
import technobot.util.embeds.EmbedColor;
12+
import technobot.util.embeds.EmbedUtils;
13+
14+
import java.text.DecimalFormat;
15+
16+
/**
17+
* Solves expressions like a calculator.
18+
*
19+
* @author TechnoVision
20+
*/
21+
public class MathCommand extends Command {
22+
23+
private static final DecimalFormat FORMATTER = new DecimalFormat("#,###.####");
24+
25+
public MathCommand(TechnoBot bot) {
26+
super(bot);
27+
this.name = "math";
28+
this.description = "Calculate a mathematical expression.";
29+
this.category = Category.UTILITY;
30+
this.args.add(new OptionData(OptionType.STRING, "expression", "The math expression to solve.", true));
31+
}
32+
33+
@Override
34+
public void execute(SlashCommandInteractionEvent event) {
35+
String expression = event.getOption("expression").getAsString();
36+
try {
37+
Double result = new DoubleEvaluator().evaluate(expression);
38+
EmbedBuilder embed = new EmbedBuilder()
39+
.setColor(EmbedColor.DEFAULT.color)
40+
.addField("Expression", "`"+expression+"`", false)
41+
.addField("Result", FORMATTER.format(result), false);
42+
event.replyEmbeds(embed.build()).queue();
43+
} catch (IllegalArgumentException e) {
44+
event.replyEmbeds(EmbedUtils.createError("That is not a valid math expression! Example: `(2^3-1)*sin(pi/4)/ln(pi^2)`")).setEphemeral(true).queue();
45+
}
46+
}
47+
}

src/main/java/technobot/commands/utility/PremiumCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import technobot.commands.Category;
88
import technobot.commands.Command;
99
import technobot.util.embeds.EmbedColor;
10-
import technobot.util.embeds.EmbedUtils;
1110

1211
/**
1312
* Creates button links to the Patreon to buy premium.

0 commit comments

Comments
 (0)