Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weather command, add ratelimit handling for POSTs #23

Merged
merged 9 commits into from
Apr 2, 2024
Merged
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
4 changes: 4 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def post(self, path, data, files=None, method='POST'):
if config.bot.debug:
print('=>', path, data)
response = self.rs.request(method, 'https://discord.com/api' + path, files=files, json=data)
if response.headers.get('X-RateLimit-Remaining') == '0':
wait_time = int(response.headers['X-RateLimit-Reset-After'])
log.write('waiting %d for rate limit' % wait_time)
time.sleep(wait_time)
if response.status_code >= 400:
log.write('response: %r' % response.content)
response.raise_for_status()
Expand Down
49 changes: 25 additions & 24 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ def roll(cmd):
cmd.reply('%s: error rolling' % cmd.sender['pretty_name'])

tzinfos = {
'PST': dateutil.tz.gettz('America/Los_Angeles'),
'PDT': dateutil.tz.gettz('America/Los_Angeles'),
'MST': dateutil.tz.gettz('America/Denver'),
'MDT': dateutil.tz.gettz('America/Denver'),
'CST': dateutil.tz.gettz('America/Chicago'),
'CDT': dateutil.tz.gettz('America/Chicago'),
'EST': dateutil.tz.gettz('America/New_York'),
'EDT': dateutil.tz.gettz('America/New_York'),
'PST': dateutil.tz.gettz('America/Los_Angeles'),
'PDT': dateutil.tz.gettz('America/Los_Angeles'),
'MST': dateutil.tz.gettz('America/Denver'),
'MDT': dateutil.tz.gettz('America/Denver'),
'CST': dateutil.tz.gettz('America/Chicago'),
'CDT': dateutil.tz.gettz('America/Chicago'),
'EST': dateutil.tz.gettz('America/New_York'),
'EDT': dateutil.tz.gettz('America/New_York'),
'WET': dateutil.tz.gettz('Europe/Lisbon'),
'WEST': dateutil.tz.gettz('Europe/Lisbon'),
}
def time(cmd):
if cmd.args:
Expand All @@ -157,27 +159,26 @@ def time(cmd):
def weather(cmd):
if not cmd.args:
return
split = cmd.args.split()
if split[0].startswith('-'):
flags = split[0][1:]
location = ' '.join(split[1:])
elif split[-1].startswith('-'):
flags = split[-1][1:]
location = ' '.join(split[:-1])
else:
flags = '1Fp'
location = cmd.args
if location.isdecimal() and len(location) == 5:
location += '-us'
url = 'https://wttr.in/%s.png?%s' % (urllib.parse.quote_plus(location), flags)
flags = ('format=**%l:**+%c+++🌡+`%t(%f)`++💦+`%h`++💨+`%w`++**☔**+`%p/3h`++**UVI:**+`%u`\n'
'**Time:**+`%T`++**Sunrise:**+`%S`++**Sunset:**+`%s`++**Moon:**+%m')
location = cmd.args
if location.isdecimal() and len(location) == 5:
location += '-us'
url = f'https://wttr.in/{urllib.parse.quote_plus(location)}?{flags}'
try:
response = rs.get(url)
if response.status_code == 503:
cmd.reply(f'{cmd.sender["pretty_name"]}: service unavailable for {location}')
return
if response.status_code == 404:
cmd.reply(f'{cmd.sender["pretty_name"]}: {location} not found')
return
response.raise_for_status()
except Exception:
cmd.reply('%s: error getting weather at %s' % (cmd.sender['pretty_name'], url),
{'description': '```%s```' % traceback.format_exc()[-500:]})
cmd.reply(f'{cmd.sender["pretty_name"]}: error getting weather at {url}',
{'description': f'```{traceback.format_exc()[-500:]}```'})
return
cmd.reply(None, files={'weather.png': response.content})
cmd.reply(response.text)

def ohno(cmd):
url = 'https://www.raylu.net/f/ohno/ohno%03d.png' % random.randint(1, 294)
Expand Down
Loading