0

Weather script for Sporks

Free lines JavaScript 4 revisions 50 18 days ago 18 days ago
discord-sporks
// To use this script first get a free api key from openweathermap.org
// then use !wkey to set your OWM api key. Delete that message from your server so nobody
// has your API key, then you can use !weather <city>, <country> or just !weather <city>.

// !weather command
var m = message.content.split(' ');
if (m[0] == "!weather") {
    var owm_api_key = load("owm_api_key");
    if (owm_api_key == undefined) {
      create_message(CHANNEL_ID, "Please obtain an API key from openweathermap.org and set it with ``!wkey <your api key>`` and try again.");
    } else {
      var search_location = message.content.substring(8, message.content.length);
      get("https://api.openweathermap.org/data/2.5/weather?q=" + encodeURI(search_location) + "&APPID=" + owm_api_key + "&units=metric", "weather_cb");
    }
}

// !wkey command
if (m[0] == "!wkey") {
  if (load("owm_api_key") == undefined) {
  save("owm_api_key", m[1]);
    create_message(CHANNEL_ID, "API key has been set. You may now use **!weather**");
  } else {
    create_message(CHANNEL_ID, "API key is already set.");
  }
}

// http callback
function weather_cb(data) {
  data = JSON.parse(data);
  if (data.cod == "404") {
    create_message(CHANNEL_ID, "I don't know what that city name is. :cry:");
    return;
  } else if (data.cod == "429") {
    create_message(CHANNEL_ID, "I'm rate limited. Don't spam **!weather** :cry:");
    return;
  } else if (data.cod == "200") {
    var cw = data.weather;
    if (cw == undefined) {
      cw = "Unavailable";
    } else {
      cw = data.weather[0].description;
    }
    create_embed(CHANNEL_ID, {
      "color": 0x20CF30, 
      "title": "Weather report for " + data.name,
      "fields": [
        {"name": "Current conditions", "value": cw, "inline": true},
        {"name": "Temperature", "value": data.main.temp.toString() + "C", "inline": true},
        {"name": "Humidity", "value": data.main.humidity.toString() + "%", "inline": true},
        {"name": "Pressure", "value": data.main.pressure.toString() + "hPa", "inline": true},
        {"name": "Visibility", "value": data.visibility.toString() + "m", "inline": true}
    	],
      "description": "",
      "footer": {
          "link": "https://openweathermap.org/",
          "text": "Powered by openweathermap.org",
          "icon_url": "http://openweathermap.org/img/wn/[email protected]"
      },
      "thumbnail": {
        "url": "http://openweathermap.org/img/wn/" + data.weather[0].icon + "@2x.png"
      }
    });
  }
}

This script provides a !weather command. Before using the command you need to go to openweathermap.org and sign up for a free api key. Use the !wkey command to set this key. Once set, the key cannot be updated (unless you modify this script). You can then delete the message from your channel where you set the key.

The key you set is guild-wide, so you can set it in one channel then use the script in another, if the script is also active there.

!weather accepts both a city name and city, country where just the city is ambiguous.