Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/main/java/org/jointheleague/discord_bot_example/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/org/jointheleague/modules/BitcoinListener.java
Original file line number Diff line number Diff line change
@@ -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";
}


}

53 changes: 53 additions & 0 deletions src/main/java/org/jointheleague/pojo/bitcoin/Bitcoin.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
43 changes: 43 additions & 0 deletions src/main/java/org/jointheleague/pojo/bitcoin/Bpi.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
65 changes: 65 additions & 0 deletions src/main/java/org/jointheleague/pojo/bitcoin/EUR.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
65 changes: 65 additions & 0 deletions src/main/java/org/jointheleague/pojo/bitcoin/GBP.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading