From 7b5447f78d43f589fce1bdf98173c5aaaae8e34d Mon Sep 17 00:00:00 2001 From: root Date: Thu, 13 May 2021 01:26:47 +0000 Subject: [PATCH 1/5] started to work on weather forecast, use weather.gov for api. --- .../modules/WeatherForecast.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/org/jointheleague/modules/WeatherForecast.java diff --git a/src/main/java/org/jointheleague/modules/WeatherForecast.java b/src/main/java/org/jointheleague/modules/WeatherForecast.java new file mode 100644 index 00000000..cac50158 --- /dev/null +++ b/src/main/java/org/jointheleague/modules/WeatherForecast.java @@ -0,0 +1,27 @@ +package org.jointheleague.modules; + +import org.javacord.api.event.message.MessageCreateEvent; + +import net.aksingh.owmjapis.api.APIException; + +public class WeatherForecast extends CustomMessageCreateListener{ + + public final static String COMMAND = "/weather"; + + public WeatherForecast(String channelName) { + super(channelName); + // TODO Auto-generated constructor stub + } + + @Override + public void handle(MessageCreateEvent event) throws APIException { + // TODO Auto-generated method stub + String[] str = event.getMessageContent().split(" "); + if(str[0].equals(COMMAND)) { + if(str[1].equals("checkLongLat")) { + event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net"); + } + } + } + +} From 418681286da9301982a7bd8d1cd364552275d13c Mon Sep 17 00:00:00 2001 From: root Date: Thu, 13 May 2021 01:42:31 +0000 Subject: [PATCH 2/5] continue work with api using weather.gov and latlong.net --- .../org/jointheleague/modules/WeatherForecast.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jointheleague/modules/WeatherForecast.java b/src/main/java/org/jointheleague/modules/WeatherForecast.java index cac50158..4fdf8399 100644 --- a/src/main/java/org/jointheleague/modules/WeatherForecast.java +++ b/src/main/java/org/jointheleague/modules/WeatherForecast.java @@ -7,7 +7,9 @@ public class WeatherForecast extends CustomMessageCreateListener{ public final static String COMMAND = "/weather"; - + String findGrid = "https://api.weather.gov/points/[latitude],[longitude]"; + String latitude = ""; + String longitude = ""; public WeatherForecast(String channelName) { super(channelName); // TODO Auto-generated constructor stub @@ -18,8 +20,12 @@ public void handle(MessageCreateEvent event) throws APIException { // TODO Auto-generated method stub String[] str = event.getMessageContent().split(" "); if(str[0].equals(COMMAND)) { - if(str[1].equals("checkLongLat")) { + if(str[1].equalsIgnoreCase("checkLongLat")) { event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net"); + } else if(str[1].equalsIgnoreCase("checkgrid")) { + event.getChannel().sendMessage("your grid is" + "\n" + ""); + } else if(str[1].equalsIgnoreCase("checkweather")) { + } } } From e140ef909d3fdb8cd62aa4662c273f54712d8231 Mon Sep 17 00:00:00 2001 From: Samson Date: Wed, 26 May 2021 17:10:47 -0700 Subject: [PATCH 3/5] worked on weather api --- .../discord_bot_example/Bot.java | 11 +++- .../modules/WeatherForecast.java | 56 ++++++++++++++++--- .../modules/WeatherForecastData.java | 10 ++++ .../modules/WeatherForecastProperties.java | 10 ++++ 4 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/jointheleague/modules/WeatherForecastData.java create mode 100644 src/main/java/org/jointheleague/modules/WeatherForecastProperties.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..3fd5d9ce 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -47,9 +47,9 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(dl); helpListener.addHelpEmbed(dl.getHelpEmbed()); - CurrencyConverter cc = new CurrencyConverter(channelName); - api.addMessageCreateListener(cc); - helpListener.addHelpEmbed(cc.getHelpEmbed()); +// CurrencyConverter cc = new CurrencyConverter(channelName); +// api.addMessageCreateListener(cc); +// helpListener.addHelpEmbed(cc.getHelpEmbed()); ToDoList list = new ToDoList(channelName); api.addMessageCreateListener(list); @@ -143,10 +143,15 @@ public void connect(boolean printInvite) { UnbeatableRockPaperScissors rps = new UnbeatableRockPaperScissors(channelName); api.addMessageCreateListener(rps); helpListener.addHelpEmbed(rps.getHelpEmbed()); + Depression depress = new Depression(channelName); api.addMessageCreateListener(depress); helpListener.addHelpEmbed(depress.getHelpEmbed()); + WeatherForecast wf = new WeatherForecast(channelName); + api.addMessageCreateListener(wf); + helpListener.addHelpEmbed(wf.getHelpEmbed()); + //old way to add listeners api.addMessageCreateListener(helpListener); diff --git a/src/main/java/org/jointheleague/modules/WeatherForecast.java b/src/main/java/org/jointheleague/modules/WeatherForecast.java index 4fdf8399..cc00c22b 100644 --- a/src/main/java/org/jointheleague/modules/WeatherForecast.java +++ b/src/main/java/org/jointheleague/modules/WeatherForecast.java @@ -1,15 +1,22 @@ package org.jointheleague.modules; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + import org.javacord.api.event.message.MessageCreateEvent; import net.aksingh.owmjapis.api.APIException; -public class WeatherForecast extends CustomMessageCreateListener{ +public class WeatherForecast extends CustomMessageCreateListener { public final static String COMMAND = "/weather"; - String findGrid = "https://api.weather.gov/points/[latitude],[longitude]"; + String findGrid = "https://api.weather.gov/points/"; // add [latitude],[longitude] to call String latitude = ""; String longitude = ""; + public WeatherForecast(String channelName) { super(channelName); // TODO Auto-generated constructor stub @@ -19,15 +26,48 @@ public WeatherForecast(String channelName) { public void handle(MessageCreateEvent event) throws APIException { // TODO Auto-generated method stub String[] str = event.getMessageContent().split(" "); - if(str[0].equals(COMMAND)) { - if(str[1].equalsIgnoreCase("checkLongLat")) { + str = event.getMessageContent().split(","); + if (str[0].equals(COMMAND)) { + if (str[1].equalsIgnoreCase("checkLongLat")) { event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net"); - } else if(str[1].equalsIgnoreCase("checkgrid")) { - event.getChannel().sendMessage("your grid is" + "\n" + ""); - } else if(str[1].equalsIgnoreCase("checkweather")) { - + } else if (str[1].equalsIgnoreCase("checkgrid")) { + try { + String get = findGrid + str[2] + "," + str[3]; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + System.out.println(response); + event.getChannel().sendMessage(response); + } catch(Exception e) { + e.printStackTrace(); + } + } else if (str[1].equalsIgnoreCase("checkweather")) { + } } } + private BufferedReader makeGetRequest(String get) throws IOException { + URL url = new URL(get); + HttpURLConnection connector = (HttpURLConnection) url.openConnection(); + connector.setRequestMethod("GET"); + BufferedReader br = new BufferedReader(new InputStreamReader(connector.getInputStream())); + return br; + } + + private String parseResponse(BufferedReader br) { + String response = ""; + String line = ""; + try { + line = br.readLine(); + while (line != null) { + response += line; + line = br.readLine(); + } + br.close(); + response = response.trim(); + } catch (Exception e) { + // TODO: handle exception + } + return response; + } } diff --git a/src/main/java/org/jointheleague/modules/WeatherForecastData.java b/src/main/java/org/jointheleague/modules/WeatherForecastData.java new file mode 100644 index 00000000..8a055254 --- /dev/null +++ b/src/main/java/org/jointheleague/modules/WeatherForecastData.java @@ -0,0 +1,10 @@ +package org.jointheleague.modules; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class WeatherForecastData { + @SerializedName("properties") + @Expose + WeatherForecastProperties properties; +} diff --git a/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java b/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java new file mode 100644 index 00000000..f31ceba7 --- /dev/null +++ b/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java @@ -0,0 +1,10 @@ +package org.jointheleague.modules; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class WeatherForecastProperties { + @SerializedName("forecast") + @Expose + String forecast; +} From 06f6838380bae7cac8e26afcefda7fa21e4da450 Mon Sep 17 00:00:00 2001 From: Samson Date: Wed, 26 May 2021 18:49:31 -0700 Subject: [PATCH 4/5] figure out why helpembed isnt showing up, then finished! --- .../modules/WeatherForecast.java | 70 ++++++++++++++++--- .../modules/WeatherForecastPeriods.java | 39 +++++++++++ .../modules/WeatherForecastProperties.java | 3 + src/main/resources/config.json | 4 +- 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/jointheleague/modules/WeatherForecastPeriods.java diff --git a/src/main/java/org/jointheleague/modules/WeatherForecast.java b/src/main/java/org/jointheleague/modules/WeatherForecast.java index cc00c22b..3d22fa6f 100644 --- a/src/main/java/org/jointheleague/modules/WeatherForecast.java +++ b/src/main/java/org/jointheleague/modules/WeatherForecast.java @@ -7,6 +7,9 @@ import java.net.URL; import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.modules.pojo.HelpEmbed; + +import com.google.gson.Gson; import net.aksingh.owmjapis.api.APIException; @@ -16,36 +19,81 @@ public class WeatherForecast extends CustomMessageCreateListener { String findGrid = "https://api.weather.gov/points/"; // add [latitude],[longitude] to call String latitude = ""; String longitude = ""; - + String allData = ""; + public WeatherForecast(String channelName) { super(channelName); // TODO Auto-generated constructor stub + helpEmbed = new HelpEmbed(COMMAND, "There are 3 commands: \n " + COMMAND + "checklonglat to find out how to find your current longitudinal and latitudinal position(idk if thats a word(it now is)) \n " + COMMAND + "weekWeather to check what the weather will be like this week \n" + COMMAND + "dailyWeather to check what the weather is at that 12 hour period"); } @Override public void handle(MessageCreateEvent event) throws APIException { // TODO Auto-generated method stub String[] str = event.getMessageContent().split(" "); - str = event.getMessageContent().split(","); if (str[0].equals(COMMAND)) { - if (str[1].equalsIgnoreCase("checkLongLat")) { - event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net"); - } else if (str[1].equalsIgnoreCase("checkgrid")) { + if (str[1].equalsIgnoreCase("checklonglat")) { + event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net \n and type in your current location."); + } else if (str[1].equalsIgnoreCase("weekWeather")) { try { - String get = findGrid + str[2] + "," + str[3]; + String[] str2 = str[2].split(","); + String get = findGrid + str2[0] + "," + str2[1]; BufferedReader br = makeGetRequest(get); String response = parseResponse(br); - System.out.println(response); - event.getChannel().sendMessage(response); + WeatherForecastData wfd = getPos(Float.parseFloat(str2[0]), Float.parseFloat(str2[1])); + get = wfd.properties.forecast; + WeatherForecastData wfd2 = getForecast(wfd); + for (int i = 0; i < wfd2.properties.periods.length; i++) { + allData = "Forecast for: " + (String) wfd2.properties.periods[i].toString() + "\n" + "---|||---" + "\n"; + event.getChannel().sendMessage(allData); + } } catch(Exception e) { e.printStackTrace(); } - } else if (str[1].equalsIgnoreCase("checkweather")) { - + } else if(str[1].equalsIgnoreCase("dailyWeather")) { + try { + String[] str2 = str[2].split(","); + String get = findGrid + str2[0] + "," + str2[1]; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + WeatherForecastData wfd = getPos(Float.parseFloat(str2[0]), Float.parseFloat(str2[1])); + get = wfd.properties.forecast; + WeatherForecastData wfd2 = getForecast(wfd); + allData = "Forecast for: " + (String) wfd2.properties.periods[0].toString(); + event.getChannel().sendMessage(allData); + } catch(Exception e) { + e.printStackTrace(); + } } } } - + + private WeatherForecastData getPos(float lat, float longi) { + final Gson gson = new Gson(); + try { + String get = (String) findGrid + lat + "," + longi; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + WeatherForecastData wfd = gson.fromJson(response, WeatherForecastData.class); + return wfd; + } catch (Exception e) { + } + return null; + } + + private WeatherForecastData getForecast(WeatherForecastData wfd) { + final Gson gson = new Gson(); + try { + String get = wfd.properties.forecast; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + wfd = gson.fromJson(response, WeatherForecastData.class); + return wfd; + } catch (Exception e) { + } + return null; + } + private BufferedReader makeGetRequest(String get) throws IOException { URL url = new URL(get); HttpURLConnection connector = (HttpURLConnection) url.openConnection(); diff --git a/src/main/java/org/jointheleague/modules/WeatherForecastPeriods.java b/src/main/java/org/jointheleague/modules/WeatherForecastPeriods.java new file mode 100644 index 00000000..893334fe --- /dev/null +++ b/src/main/java/org/jointheleague/modules/WeatherForecastPeriods.java @@ -0,0 +1,39 @@ +package org.jointheleague.modules; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class WeatherForecastPeriods { + @SerializedName("name") + @Expose + String name; + @SerializedName("isDaytime") + @Expose + boolean isDaytime; + @SerializedName("temperature") + @Expose + int temperature; + @SerializedName("windSpeed") + @Expose + String windSpeed; + @SerializedName("windDirection") + @Expose + String windDirection; + @SerializedName("shortForecast") + @Expose + String shortForecast; + @SerializedName("detailedForecast") + @Expose + String detailedForecast; + + public String toString() { + String isDaytimes = ""; + if(isDaytime = true) { + isDaytimes = "it is not night right now!"; + } else { + isDaytimes = "it is night right now"; + } + String str = name + "\n" + isDaytimes + "\n" + temperature + "\n" + windSpeed + "\n" + windDirection + "\n" + shortForecast + "\n" + detailedForecast; + return str; + } +} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java b/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java index f31ceba7..15fd8847 100644 --- a/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java +++ b/src/main/java/org/jointheleague/modules/WeatherForecastProperties.java @@ -4,6 +4,9 @@ import com.google.gson.annotations.SerializedName; public class WeatherForecastProperties { + @SerializedName("periods") + @Expose + WeatherForecastPeriods[] periods; @SerializedName("forecast") @Expose String forecast; diff --git a/src/main/resources/config.json b/src/main/resources/config.json index 0b58c8d9..6d4d7941 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -1,4 +1,4 @@ { - "channels": ["channelName"], - "token": "discordToken" + "channels": ["samson"], + "token": "ODI0NDQxMTIzMDUzMTA5MzAw.YFvaoQ.UsPoqnHd29OvseJdIw5cFrbXUr4" } From 1a3f2e79322ac81e2123e37ec571ed3fd40943b5 Mon Sep 17 00:00:00 2001 From: Samson Date: Wed, 9 Jun 2021 18:19:41 -0700 Subject: [PATCH 5/5] finished up the bot --- .../discord_bot_example/Bot.java | 7 +- .../modules/WeatherForecast.java | 101 +++++++++++++----- 2 files changed, 76 insertions(+), 32 deletions(-) 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 3fd5d9ce..959a77d8 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -46,6 +46,10 @@ public void connect(boolean printInvite) { DiscordLibrary dl = new DiscordLibrary(channelName); api.addMessageCreateListener(dl); helpListener.addHelpEmbed(dl.getHelpEmbed()); + + WeatherForecast wf = new WeatherForecast(channelName); + api.addMessageCreateListener(wf); + helpListener.addHelpEmbed(wf.getHelpEmbed()); // CurrencyConverter cc = new CurrencyConverter(channelName); // api.addMessageCreateListener(cc); @@ -148,9 +152,6 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(depress); helpListener.addHelpEmbed(depress.getHelpEmbed()); - WeatherForecast wf = new WeatherForecast(channelName); - api.addMessageCreateListener(wf); - helpListener.addHelpEmbed(wf.getHelpEmbed()); //old way to add listeners diff --git a/src/main/java/org/jointheleague/modules/WeatherForecast.java b/src/main/java/org/jointheleague/modules/WeatherForecast.java index 3d22fa6f..6c4246d0 100644 --- a/src/main/java/org/jointheleague/modules/WeatherForecast.java +++ b/src/main/java/org/jointheleague/modules/WeatherForecast.java @@ -20,11 +20,14 @@ public class WeatherForecast extends CustomMessageCreateListener { String latitude = ""; String longitude = ""; String allData = ""; - + public WeatherForecast(String channelName) { super(channelName); // TODO Auto-generated constructor stub - helpEmbed = new HelpEmbed(COMMAND, "There are 3 commands: \n " + COMMAND + "checklonglat to find out how to find your current longitudinal and latitudinal position(idk if thats a word(it now is)) \n " + COMMAND + "weekWeather to check what the weather will be like this week \n" + COMMAND + "dailyWeather to check what the weather is at that 12 hour period"); + helpEmbed = new HelpEmbed(COMMAND, "There are 3 commands: \n " + COMMAND + + " checklonglat: to find out how to find your current longitudinal and latitudinal position(An example is 40, 20) \n " + + COMMAND + " weekWeather: to check what the weather will be like this week \n" + COMMAND + + " dailyWeather: to check what the weather is at that 12 hour period"); } @Override @@ -32,42 +35,82 @@ public void handle(MessageCreateEvent event) throws APIException { // TODO Auto-generated method stub String[] str = event.getMessageContent().split(" "); if (str[0].equals(COMMAND)) { + if (str.length == 1) { + event.getChannel() + .sendMessage("you have to add a command, pick one: CheckLongLat, WeekWeather, or DailyWeather"); + } if (str[1].equalsIgnoreCase("checklonglat")) { - event.getChannel().sendMessage("to find your latitude and longitude, go to: \n latlong.net \n and type in your current location."); + event.getChannel().sendMessage( + "to find your latitude and longitude, go to: \n latlong.net \n and type in your current location. Type in the website your address and it gives you the latitude and longitude of your position. \n Example calls: \n /weather weekweather 10,4 \n /weather dailyweather 3,2 \n NO SPACES BETWEEN THE LATITUDE AND LONGITUDE"); } else if (str[1].equalsIgnoreCase("weekWeather")) { - try { - String[] str2 = str[2].split(","); - String get = findGrid + str2[0] + "," + str2[1]; - BufferedReader br = makeGetRequest(get); - String response = parseResponse(br); - WeatherForecastData wfd = getPos(Float.parseFloat(str2[0]), Float.parseFloat(str2[1])); - get = wfd.properties.forecast; - WeatherForecastData wfd2 = getForecast(wfd); - for (int i = 0; i < wfd2.properties.periods.length; i++) { - allData = "Forecast for: " + (String) wfd2.properties.periods[i].toString() + "\n" + "---|||---" + "\n"; - event.getChannel().sendMessage(allData); + if (str.length == 4 && str[2].contains(",")) { + str[2] = str[2] + str[3]; + } + if (str.length >= 3) { + try { + String num1; + String num2; + if (str[2].contains(",")) { + String[] str2 = str[2].split(","); + num1 = str2[0]; + num2 = str2[1]; + } else { + num1 = str[2]; + num2 = str[3]; + } + String get = findGrid + num1 + "," + num2; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + WeatherForecastData wfd = getPos(Float.parseFloat(num1), Float.parseFloat(num2)); + get = wfd.properties.forecast; + WeatherForecastData wfd2 = getForecast(wfd); + for (int i = 0; i < wfd2.properties.periods.length; i++) { + allData = "Forecast for: " + (String) wfd2.properties.periods[i].toString() + "\n" + + "---|||---" + "\n"; + event.getChannel().sendMessage(allData); + } + } catch (Exception e) { + e.printStackTrace(); + } + } else { + event.getChannel().sendMessage( + "make sure it includes your grid which can be found at latlong.net. Example: /weather weekweather 40,20 \n NO SPACES BETWEEN THE LATITUDE AND LONGITUDE"); } - } catch(Exception e) { - e.printStackTrace(); + } else if (str[1].equalsIgnoreCase("dailyWeather")) { + if (str.length == 4 && str[2].contains(",")) { + str[2] = str[2] + str[3]; } - } else if(str[1].equalsIgnoreCase("dailyWeather")) { - try { - String[] str2 = str[2].split(","); - String get = findGrid + str2[0] + "," + str2[1]; - BufferedReader br = makeGetRequest(get); - String response = parseResponse(br); - WeatherForecastData wfd = getPos(Float.parseFloat(str2[0]), Float.parseFloat(str2[1])); - get = wfd.properties.forecast; - WeatherForecastData wfd2 = getForecast(wfd); + if (str.length >= 3) { + try { + String num1; + String num2; + if (str[2].contains(",")) { + String[] str2 = str[2].split(","); + num1 = str2[0]; + num2 = str2[1]; + } else { + num1 = str[2]; + num2 = str[3]; + } + String get = findGrid + num1 + "," + num2; + BufferedReader br = makeGetRequest(get); + String response = parseResponse(br); + WeatherForecastData wfd = getPos(Float.parseFloat(num1), Float.parseFloat(num2)); + get = wfd.properties.forecast; + WeatherForecastData wfd2 = getForecast(wfd); allData = "Forecast for: " + (String) wfd2.properties.periods[0].toString(); event.getChannel().sendMessage(allData); - } catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } + } else { + event.getChannel().sendMessage( + "make sure it includes your grid which can be found at latlong.net. Example: /weather dailyweather 10,22 \n NO SPACES BETWEEN THE LATITUDE AND LONGITUDE"); + } } } } - + private WeatherForecastData getPos(float lat, float longi) { final Gson gson = new Gson(); try { @@ -80,7 +123,7 @@ private WeatherForecastData getPos(float lat, float longi) { } return null; } - + private WeatherForecastData getForecast(WeatherForecastData wfd) { final Gson gson = new Gson(); try { @@ -93,7 +136,7 @@ private WeatherForecastData getForecast(WeatherForecastData wfd) { } return null; } - + private BufferedReader makeGetRequest(String get) throws IOException { URL url = new URL(get); HttpURLConnection connector = (HttpURLConnection) url.openConnection();