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 75aff545..b1126490 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -93,21 +93,26 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(randomString); helpListener.addHelpEmbed(randomString.getHelpEmbed()); - Greeter g = new Greeter(channelName); - api.addMessageCreateListener(g); - helpListener.addHelpEmbed(g.getHelpEmbed()); + BitcoinListener bc = new BitcoinListener(channelName); + api.addMessageCreateListener(bc); + helpListener.addHelpEmbed(bc.getHelpEmbed()); - ListMakerMessageListener LM = new ListMakerMessageListener(channelName); - api.addMessageCreateListener(LM); - helpListener.addHelpEmbed(LM.getHelpEmbed()); + + // Greeter g = new Greeter(channelName); +// api.addMessageCreateListener(g); + // helpListener.addHelpEmbed(g.getHelpEmbed()); + +// ListMakerMessageListener LM = new ListMakerMessageListener(channelName); +// api.addMessageCreateListener(LM); +// helpListener.addHelpEmbed(LM.getHelpEmbed()); Dice d = new Dice(channelName); api.addMessageCreateListener(d); helpListener.addHelpEmbed(d.getHelpEmbed()); - UnbeatableRockPaperScissors rps = new UnbeatableRockPaperScissors(channelName); - api.addMessageCreateListener(rps); - helpListener.addHelpEmbed(rps.getHelpEmbed()); + // UnbeatableRockPaperScissors rps = new UnbeatableRockPaperScissors(channelName); +// api.addMessageCreateListener(rps); +// helpListener.addHelpEmbed(rps.getHelpEmbed()); //old way to add listeners api.addMessageCreateListener(helpListener); diff --git a/src/main/java/org/jointheleague/modules/BitcoinListener.java b/src/main/java/org/jointheleague/modules/BitcoinListener.java new file mode 100644 index 00000000..25e2d568 --- /dev/null +++ b/src/main/java/org/jointheleague/modules/BitcoinListener.java @@ -0,0 +1,120 @@ +package org.jointheleague.modules; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonReader; + +import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.modules.pojo.HelpEmbed; +import org.jointheleague.modules.pojo.apiExample.ApiExampleWrapper; +import org.jointheleague.modules.pojo.apiExample.Article; +import org.jointheleague.pojo.bitcoin.Bitcoin; +import org.jointheleague.pojo.bitcoin.EUR; +import org.jointheleague.pojo.bitcoin.GBP; +import org.jointheleague.pojo.bitcoin.Time; +import org.jointheleague.pojo.bitcoin.USD; + +import com.google.gson.Gson; + +//this example uses the "News API" +//documentation for the API can be found here: https://newsapi.org/docs/get-started +public class BitcoinListener extends CustomMessageCreateListener{ + + //API key is received through creating an account on the web site. + //API may not require a key, or may require the key as a header as in PictureOf.java + private static final String COMMAND = "!Bitcoin"; + private final Gson gson = new Gson(); + + public BitcoinListener(String channelName) { + super(channelName); + helpEmbed = new HelpEmbed(COMMAND, "Example of using an API to get information from another service"); + + } + + @Override + public void handle(MessageCreateEvent event) { + if(event.getMessageContent().contains(COMMAND)) { + + //remove the command so we are only left with the search term + String msg = event.getMessageContent().replaceAll(" ", "").replace(COMMAND, ""); + + if (msg.equals("USD")||msg.equals("EUR")||msg.equals("GBP")&&!event.getMessageAuthor().isBotUser()) { + String definition = Price(msg); + event.getChannel().sendMessage(definition); + } else if(!event.getMessageAuthor().isBotUser()){ + event.getChannel().sendMessage("Please input !Bitcoin and USD, EUR, or GBP after the command"); + } + + } + } + + public String Price(String topic) { + + //create the request URL (can be found in the documentation) + String requestURL = "https://api.coindesk.com/v1/bpi/currentprice.json"; + + try { + + //the following code will probably be the same for your feature + URL url = new URL(requestURL); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + JsonReader repoReader = Json.createReader(con.getInputStream()); + JsonObject userJSON = ((JsonObject) repoReader.read()); + con.disconnect(); + + //turn the json response into a java object + //you will need to create a java class that represents the response in org.jointheleague.modules.pojo + //you can use a tools like Postman and jsonschema2pojo.com to help with that + + //you can use postman to make the request and receive a response, then take that and put it right into jsonschema2pojo.com + //If using jsonschema2pojo.com, select Target Langue = java, Source Type = JSON, Annotation Style = Gson + Bitcoin bc= gson.fromJson(userJSON.toString(), Bitcoin.class); + + //get the first article (these are just java objects now) + USD us = bc.getBpi().getUSD(); + GBP gp = bc.getBpi().getGBP(); + EUR euro = bc.getBpi().getEUR(); + String t= bc.getTime().getUpdated(); + String price=" "; + //get the title of the article + if (topic.equals("USD")) { + price= us.getDescription()+ " $" +us.getRateFloat(); + } + else if (topic.equals("GBP")) { + price= gp.getDescription()+ " £" +gp.getRateFloat(); + } + else if (topic.equals("EUR")){ + price= euro.getDescription()+ " €" +euro.getRateFloat(); + } + + //get the content of the article + + + //create the message + String message = t + " " + topic + " " + price; + + //send the message + return message; + + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return "That currency does not exist"; + } + + + } + diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/Bitcoin.java b/src/main/java/org/jointheleague/pojo/bitcoin/Bitcoin.java new file mode 100644 index 00000000..e4609bcd --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/Bitcoin.java @@ -0,0 +1,53 @@ + +package org.jointheleague.pojo.bitcoin; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Bitcoin { + + @SerializedName("time") + @Expose + private Time time; + @SerializedName("disclaimer") + @Expose + private String disclaimer; + @SerializedName("chartName") + @Expose + private String chartName; + @SerializedName("bpi") + @Expose + private Bpi bpi; + + public Time getTime() { + return time; + } + + public void setTime(Time time) { + this.time = time; + } + + public String getDisclaimer() { + return disclaimer; + } + + public void setDisclaimer(String disclaimer) { + this.disclaimer = disclaimer; + } + + public String getChartName() { + return chartName; + } + + public void setChartName(String chartName) { + this.chartName = chartName; + } + + public Bpi getBpi() { + return bpi; + } + + public void setBpi(Bpi bpi) { + this.bpi = bpi; + } + +} diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/Bpi.java b/src/main/java/org/jointheleague/pojo/bitcoin/Bpi.java new file mode 100644 index 00000000..87fd872a --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/Bpi.java @@ -0,0 +1,43 @@ + +package org.jointheleague.pojo.bitcoin; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Bpi { + + @SerializedName("USD") + @Expose + private USD uSD; + @SerializedName("GBP") + @Expose + private GBP gBP; + @SerializedName("EUR") + @Expose + private EUR eUR; + + public USD getUSD() { + return uSD; + } + + public void setUSD(USD uSD) { + this.uSD = uSD; + } + + public GBP getGBP() { + return gBP; + } + + public void setGBP(GBP gBP) { + this.gBP = gBP; + } + + public EUR getEUR() { + return eUR; + } + + public void setEUR(EUR eUR) { + this.eUR = eUR; + } + +} diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/EUR.java b/src/main/java/org/jointheleague/pojo/bitcoin/EUR.java new file mode 100644 index 00000000..b3bc0369 --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/EUR.java @@ -0,0 +1,65 @@ + +package org.jointheleague.pojo.bitcoin; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class EUR { + + @SerializedName("code") + @Expose + private String code; + @SerializedName("symbol") + @Expose + private String symbol; + @SerializedName("rate") + @Expose + private String rate; + @SerializedName("description") + @Expose + private String description; + @SerializedName("rate_float") + @Expose + private Double rateFloat; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getRate() { + return rate; + } + + public void setRate(String rate) { + this.rate = rate; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Double getRateFloat() { + return rateFloat; + } + + public void setRateFloat(Double rateFloat) { + this.rateFloat = rateFloat; + } + +} diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/GBP.java b/src/main/java/org/jointheleague/pojo/bitcoin/GBP.java new file mode 100644 index 00000000..fd03a7c9 --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/GBP.java @@ -0,0 +1,65 @@ + +package org.jointheleague.pojo.bitcoin; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class GBP { + + @SerializedName("code") + @Expose + private String code; + @SerializedName("symbol") + @Expose + private String symbol; + @SerializedName("rate") + @Expose + private String rate; + @SerializedName("description") + @Expose + private String description; + @SerializedName("rate_float") + @Expose + private Double rateFloat; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getRate() { + return rate; + } + + public void setRate(String rate) { + this.rate = rate; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Double getRateFloat() { + return rateFloat; + } + + public void setRateFloat(Double rateFloat) { + this.rateFloat = rateFloat; + } + +} diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/Time.java b/src/main/java/org/jointheleague/pojo/bitcoin/Time.java new file mode 100644 index 00000000..7ab3f720 --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/Time.java @@ -0,0 +1,43 @@ + +package org.jointheleague.pojo.bitcoin; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Time { + + @SerializedName("updated") + @Expose + private String updated; + @SerializedName("updatedISO") + @Expose + private String updatedISO; + @SerializedName("updateduk") + @Expose + private String updateduk; + + public String getUpdated() { + return updated; + } + + public void setUpdated(String updated) { + this.updated = updated; + } + + public String getUpdatedISO() { + return updatedISO; + } + + public void setUpdatedISO(String updatedISO) { + this.updatedISO = updatedISO; + } + + public String getUpdateduk() { + return updateduk; + } + + public void setUpdateduk(String updateduk) { + this.updateduk = updateduk; + } + +} diff --git a/src/main/java/org/jointheleague/pojo/bitcoin/USD.java b/src/main/java/org/jointheleague/pojo/bitcoin/USD.java new file mode 100644 index 00000000..f8bc2bd8 --- /dev/null +++ b/src/main/java/org/jointheleague/pojo/bitcoin/USD.java @@ -0,0 +1,65 @@ + +package org.jointheleague.pojo.bitcoin; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class USD { + + @SerializedName("code") + @Expose + private String code; + @SerializedName("symbol") + @Expose + private String symbol; + @SerializedName("rate") + @Expose + private String rate; + @SerializedName("description") + @Expose + private String description; + @SerializedName("rate_float") + @Expose + private Double rateFloat; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getRate() { + return rate; + } + + public void setRate(String rate) { + this.rate = rate; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Double getRateFloat() { + return rateFloat; + } + + public void setRateFloat(Double rateFloat) { + this.rateFloat = rateFloat; + } + +} diff --git a/src/main/resources/config.json b/src/main/resources/config.json index eaac8cdf..cf497d84 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -3,5 +3,6 @@ "channels": ["channel"], "token": "token" + }