Skip to content

Instantly share code, notes, and snippets.

@theparanoiddroid
Created May 24, 2018 18:51
Show Gist options
  • Save theparanoiddroid/3296b0e592df6faaa49b70e799caf414 to your computer and use it in GitHub Desktop.
Save theparanoiddroid/3296b0e592df6faaa49b70e799caf414 to your computer and use it in GitHub Desktop.
A Discord bot that returns current weather conditions based on the postal code you enter in the parameter. I am very mediocre at coding.
# Weather update for discord
# Author: Mark Brimmage
import os, bs4, re, smtplib
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup as stew
import discord, asyncio
from discord.ext import commands
from discord.ext.commands import Bot
token = 'YOUR TOKEN HERE' #<======= INSERT YOUR BOT TOKEN IN THE VARIABLE
#DEFINING FUNCTIONS SCROLL DOWN FOR THE BOT STUFF
def weatherget(zipcode):
if (zipcode.isdecimal() == False) or (len(zipcode) != 5):
return('Please enter a valid 5 digit postal code and try again.')
else:
try:
html = stew(urlopen(Request('https://forecast.weather.gov/zipcity.php?inputstring={}'.format(zipcode), headers={'User-Agent': 'Mozilla/5.0'})).read(),'xml')
target = html.select('td')
summary = [] #list init
for i in range(len(target)): #list fill elements excluding whitespace
summary.append(target[i].getText().strip())
continue
final = '\n'.join(summary) #joins list by newline to look pretty
print(final)
except Exception as e:
print(e)
return(e)
return(final)
bot = commands.Bot(command_prefix = '%')
@bot.event
async def on_ready():
print('Ready')
@bot.command(pass_context=True)
async def ping(ctx):
await bot.say(":ping_pong: pong!")
@bot.command(pass_context=True)
async def weather(ctx, a: str):
await bot.say('{}'.format(weatherget(a)))
bot.run(token)
@theparanoiddroid
Copy link
Author

bs4, discord, asyncio, urllib, and smtplib packages must satisfy the pip list. This program only works with python 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment