From cb1ea9af1aa34bbf3d488148ba19182c662a8379 Mon Sep 17 00:00:00 2001 From: millerwatson Date: Sun, 11 Jul 2021 20:50:37 -0400 Subject: [PATCH 1/3] calc functions of log and power --- .../discord_bot_example/Bot.java | 4 + .../org/jointheleague/modules/Millbot.java | 147 ++++++++++++++++++ src/main/resources/config.json | 4 +- 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/jointheleague/modules/Millbot.java diff --git a/src/main/java/org/jointheleague/discord_bot_example/Bot.java b/src/main/java/org/jointheleague/discord_bot_example/Bot.java index 06e330bf..95ebdce2 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -47,6 +47,10 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(dl); helpListener.addHelpEmbed(dl.getHelpEmbed()); + Millbot mb = new Millbot(channelName); + api.addMessageCreateListener(mb); + helpListener.addHelpEmbed(mb.getHelpEmbed()); + CurrencyConverter cc = new CurrencyConverter(channelName); api.addMessageCreateListener(cc); helpListener.addHelpEmbed(cc.getHelpEmbed()); diff --git a/src/main/java/org/jointheleague/modules/Millbot.java b/src/main/java/org/jointheleague/modules/Millbot.java new file mode 100644 index 00000000..f8ad22fe --- /dev/null +++ b/src/main/java/org/jointheleague/modules/Millbot.java @@ -0,0 +1,147 @@ +package org.jointheleague.modules; + +import java.util.Iterator; + +import org.javacord.api.event.message.MessageCreateEvent; + +import net.aksingh.owmjapis.api.APIException; + +public class Millbot extends CustomMessageCreateListener{ + + public Millbot(String channelName) { + super(channelName); + } + static final String ADD = "!add "; + static final String SUBTRACT = "!subtract "; + static final String MULTIPLY = "!multiply "; + static final String DIVIDE = "!divide "; + static final String LOG = "!log "; + static final String POWER = "!power "; + static final String helpMe = "!help"; + + @Override + public void handle(MessageCreateEvent event) throws APIException { + + String m = event.getMessageContent(); + + if(m.contains(ADD)) { + event.getChannel().sendMessage("Command received"); + String newM = m.replace(ADD, ""); + String[] addends = newM.split(","); + int counter = 0; + String botMessage = ""; + for (int i = 0; i < addends.length; i++) { + botMessage = botMessage + addends[i] + " + "; + counter = counter + Integer.parseInt(addends[i]); + } + botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; + System.out.println(botMessage + "this is a bot message"); + System.out.println(botMessage); + event.getChannel().sendMessage(botMessage); + } else if (m.contains(SUBTRACT)) { + event.getChannel().sendMessage("Command received"); + String newM = m.replace(SUBTRACT, ""); + String[] subtractends = newM.split(","); + int counter = Integer.parseInt(subtractends[0]); + String botMessage = subtractends[0] + " - "; + for (int i = 1; i < subtractends.length; i++) { + botMessage = botMessage + subtractends[i] + " - "; + counter = counter - Integer.parseInt(subtractends[i]); + } + botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; + System.out.println(botMessage + "this is a bot message"); + System.out.println(botMessage); + event.getChannel().sendMessage(botMessage); + } else if (m.contains(MULTIPLY)){ + event.getChannel().sendMessage("Command received"); + String newM = m.replace(MULTIPLY, ""); + String[] products = newM.split(","); + int counter = 1; + String botMessage = ""; + for (int i = 0; i < products.length; i++) { + botMessage = botMessage + products[i] + " × "; + counter = counter * Integer.parseInt(products[i]); + } + botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; + System.out.println(botMessage + "this is a bot message"); + System.out.println(botMessage); + event.getChannel().sendMessage(botMessage); + } else if (m.contains(DIVIDE)) { + event.getChannel().sendMessage("Command received"); + String newM = m.replace(DIVIDE, ""); + String[] theStuff = newM.split(","); + int counter = Integer.parseInt(theStuff[0]); + String botMessage = theStuff[0] + " / "; + for (int i = 1; i < theStuff.length; i++) { + botMessage = botMessage + theStuff[i] + " / "; + counter = counter / Integer.parseInt(theStuff[i]); + } + botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; + System.out.println(botMessage + "this is a bot message"); + System.out.println(botMessage); + event.getChannel().sendMessage(botMessage); + } else if (m.contains(LOG)) { + event.getChannel().sendMessage("Command received"); + String newM = m.replace(LOG, ""); + String[] theStuff = newM.split(","); + if (theStuff.length == 2) { + double answer = Math.log(Integer.parseInt(theStuff[1])) / Math.log(Integer.parseInt(theStuff[0])); + String message = "log" + subscript(theStuff[0]) + "(" + theStuff[1] + ") = " + Double.toString(answer); + System.out.println(message + "this is a bot message"); + System.out.println(message); + event.getChannel().sendMessage(message); + } else { + event.getChannel().sendMessage("You must provide two numbers in the format !log base,answer"); + } + + } else if (m.contains(POWER)){ + event.getChannel().sendMessage("Command received"); + String newM = m.replace(POWER, ""); + String[] theStuff = newM.split(","); + String botMessage = ""; + int counter = 1; + for (int i = 1; i < theStuff.length; i++) { + counter = counter * Integer.parseInt(theStuff[i]); + } + double answer = Math.pow(Integer.parseInt(theStuff[0]), counter); + String message = theStuff[0] + superscript(Integer.toString(counter)) + " = " + answer; + event.getChannel().sendMessage(message); + } + + else if(m.equals(helpMe)) { + event.getChannel().sendMessage("Type ![name of function] and put the numbers you want to add after it in a list like so:\n!add A,B,C"); + event.getChannel().sendMessage("Supported functions are addition, subtraction, multiplication, division, logarithms, exponents, and all trigonometric functions."); + event.getChannel().sendMessage("For logarithmic functions, type numbers in order of base then answer."); + } + + } + + public static String superscript(String str) { + str = str.replaceAll("0", "⁰"); + str = str.replaceAll("1", "¹"); + str = str.replaceAll("2", "²"); + str = str.replaceAll("3", "³"); + str = str.replaceAll("4", "⁴"); + str = str.replaceAll("5", "⁵"); + str = str.replaceAll("6", "⁶"); + str = str.replaceAll("7", "⁷"); + str = str.replaceAll("8", "⁸"); + str = str.replaceAll("9", "⁹"); + return str; + } + + public static String subscript(String str) { + str = str.replaceAll("0", "₀"); + str = str.replaceAll("1", "₁"); + str = str.replaceAll("2", "₂"); + str = str.replaceAll("3", "₃"); + str = str.replaceAll("4", "₄"); + str = str.replaceAll("5", "₅"); + str = str.replaceAll("6", "₆"); + str = str.replaceAll("7", "₇"); + str = str.replaceAll("8", "₈"); + str = str.replaceAll("9", "₉"); + return str; + } + +} \ No newline at end of file diff --git a/src/main/resources/config.json b/src/main/resources/config.json index 0b58c8d9..1575f539 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -1,4 +1,4 @@ { - "channels": ["channelName"], - "token": "discordToken" + "channels": ["miller"], + "token": "ODQzNjM5MzMyNjk0NjU1MDI2.YKGyWg.9YDOHurLDaup-fezV4jhe9_2ovA" } From 430c74e3e388ea3204f9b91092564debea80da54 Mon Sep 17 00:00:00 2001 From: millerwatson Date: Sun, 25 Jul 2021 20:37:04 -0400 Subject: [PATCH 2/3] millbot complete --- .../org/jointheleague/modules/Millbot.java | 58 +++++++++++-------- src/main/resources/config.json | 2 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/jointheleague/modules/Millbot.java b/src/main/java/org/jointheleague/modules/Millbot.java index f8ad22fe..e155afe0 100644 --- a/src/main/java/org/jointheleague/modules/Millbot.java +++ b/src/main/java/org/jointheleague/modules/Millbot.java @@ -17,7 +17,13 @@ public Millbot(String channelName) { static final String DIVIDE = "!divide "; static final String LOG = "!log "; static final String POWER = "!power "; - static final String helpMe = "!help"; + static final String SIN = "!sin "; + static final String COS = "!cos "; + static final String TAN = "!tan "; + static final String ANSWER = "/ans/"; + static final String HELPME = "!help"; + public static String answer = ""; + @Override public void handle(MessageCreateEvent event) throws APIException { @@ -25,67 +31,72 @@ public void handle(MessageCreateEvent event) throws APIException { String m = event.getMessageContent(); if(m.contains(ADD)) { - event.getChannel().sendMessage("Command received"); String newM = m.replace(ADD, ""); + newM = newM.replace(ANSWER, answer); String[] addends = newM.split(","); - int counter = 0; + double counter = 0; String botMessage = ""; for (int i = 0; i < addends.length; i++) { botMessage = botMessage + addends[i] + " + "; - counter = counter + Integer.parseInt(addends[i]); + counter = counter + Double.parseDouble(addends[i]); } + answer = Double.toString(counter); botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; System.out.println(botMessage + "this is a bot message"); System.out.println(botMessage); - event.getChannel().sendMessage(botMessage); + event.getChannel().sendMessage(botMessage); } else if (m.contains(SUBTRACT)) { - event.getChannel().sendMessage("Command received"); String newM = m.replace(SUBTRACT, ""); + newM = newM.replace(ANSWER, answer); String[] subtractends = newM.split(","); - int counter = Integer.parseInt(subtractends[0]); + double counter = Double.parseDouble(subtractends[0]); String botMessage = subtractends[0] + " - "; for (int i = 1; i < subtractends.length; i++) { botMessage = botMessage + subtractends[i] + " - "; - counter = counter - Integer.parseInt(subtractends[i]); + counter = counter - Double.parseDouble(subtractends[i]); } + answer = Double.toString(counter); botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; System.out.println(botMessage + "this is a bot message"); System.out.println(botMessage); event.getChannel().sendMessage(botMessage); } else if (m.contains(MULTIPLY)){ - event.getChannel().sendMessage("Command received"); String newM = m.replace(MULTIPLY, ""); + newM = newM.replace(ANSWER, answer); String[] products = newM.split(","); - int counter = 1; + double counter = 1; String botMessage = ""; for (int i = 0; i < products.length; i++) { botMessage = botMessage + products[i] + " × "; - counter = counter * Integer.parseInt(products[i]); + counter = counter * Double.parseDouble(products[i]); } + answer = Double.toString(counter); botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; System.out.println(botMessage + "this is a bot message"); System.out.println(botMessage); event.getChannel().sendMessage(botMessage); } else if (m.contains(DIVIDE)) { - event.getChannel().sendMessage("Command received"); String newM = m.replace(DIVIDE, ""); + newM = m.replace(ANSWER, answer); String[] theStuff = newM.split(","); - int counter = Integer.parseInt(theStuff[0]); + double counter = Double.parseDouble(theStuff[0]); String botMessage = theStuff[0] + " / "; for (int i = 1; i < theStuff.length; i++) { botMessage = botMessage + theStuff[i] + " / "; - counter = counter / Integer.parseInt(theStuff[i]); + counter = counter / Double.parseDouble(theStuff[i]); } + answer = Double.toString(counter); botMessage = botMessage.substring(0, botMessage.length() - 3) + " = " + counter; System.out.println(botMessage + "this is a bot message"); System.out.println(botMessage); event.getChannel().sendMessage(botMessage); } else if (m.contains(LOG)) { - event.getChannel().sendMessage("Command received"); String newM = m.replace(LOG, ""); + newM = newM.replace(ANSWER, answer); String[] theStuff = newM.split(","); if (theStuff.length == 2) { - double answer = Math.log(Integer.parseInt(theStuff[1])) / Math.log(Integer.parseInt(theStuff[0])); + double answer = Math.log(Double.parseDouble(theStuff[1])) / Math.log(Double.parseDouble(theStuff[0])); + this.answer = Double.toString(answer); String message = "log" + subscript(theStuff[0]) + "(" + theStuff[1] + ") = " + Double.toString(answer); System.out.println(message + "this is a bot message"); System.out.println(message); @@ -95,22 +106,23 @@ public void handle(MessageCreateEvent event) throws APIException { } } else if (m.contains(POWER)){ - event.getChannel().sendMessage("Command received"); String newM = m.replace(POWER, ""); + newM = newM.replace(ANSWER, answer); String[] theStuff = newM.split(","); String botMessage = ""; - int counter = 1; + double counter = 1; for (int i = 1; i < theStuff.length; i++) { - counter = counter * Integer.parseInt(theStuff[i]); + counter = counter * Double.parseDouble(theStuff[i]); } - double answer = Math.pow(Integer.parseInt(theStuff[0]), counter); - String message = theStuff[0] + superscript(Integer.toString(counter)) + " = " + answer; + double answer = Math.pow(Double.parseDouble(theStuff[0]), counter); + this.answer = Double.toString(answer); + String message = theStuff[0] + superscript(Double.toString(counter)) + " = " + answer; event.getChannel().sendMessage(message); } - else if(m.equals(helpMe)) { + else if(m.equals(HELPME)) { event.getChannel().sendMessage("Type ![name of function] and put the numbers you want to add after it in a list like so:\n!add A,B,C"); - event.getChannel().sendMessage("Supported functions are addition, subtraction, multiplication, division, logarithms, exponents, and all trigonometric functions."); + event.getChannel().sendMessage("Supported functions are addition, subtraction, multiplication, division, logarithms, and exponents."); event.getChannel().sendMessage("For logarithmic functions, type numbers in order of base then answer."); } diff --git a/src/main/resources/config.json b/src/main/resources/config.json index 1575f539..d22b53cb 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -1,4 +1,4 @@ { "channels": ["miller"], - "token": "ODQzNjM5MzMyNjk0NjU1MDI2.YKGyWg.9YDOHurLDaup-fezV4jhe9_2ovA" + "token": "ODQzNjM5MzMyNjk0NjU1MDI2.YKGyWg.Dfvnf_-kt32OfZDunJWLES70GWo" } From a907988c3fc32d32328c935dc9bde9c72ecc05ef Mon Sep 17 00:00:00 2001 From: millerwatson Date: Sun, 25 Jul 2021 20:38:21 -0400 Subject: [PATCH 3/3] Update config.json --- src/main/resources/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/config.json b/src/main/resources/config.json index d22b53cb..f714cbc9 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -1,4 +1,4 @@ { "channels": ["miller"], - "token": "ODQzNjM5MzMyNjk0NjU1MDI2.YKGyWg.Dfvnf_-kt32OfZDunJWLES70GWo" + "token": "" }