Fixed bad file/embed sending logic in multiple commands

-> image/*, weather/*
- Addressed #167 for those commands
- weather/greyline: moved gl_url to the class

Fixes #139
Touches #167
This commit is contained in:
0x5c 2020-01-20 03:17:50 -05:00
parent 1a1b4c9329
commit 12886fad89
No known key found for this signature in database
GPG Key ID: 82039FC95E3FE970
2 changed files with 56 additions and 53 deletions

View File

@ -18,6 +18,9 @@ import common as cmn
class ImageCog(commands.Cog):
gl_url = ('http://www.fourmilab.ch/cgi-bin/uncgi/Earth?img=NOAAtopo.evif'
'&imgsize=320&dynimg=y&opt=-p&lat=&lon=&alt=&tle=&date=0&utc=&jd=')
def __init__(self, bot: commands.Bot):
self.bot = bot
self.bandcharts = cmn.ImagesGroup(cmn.paths.bandcharts / "meta.json")
@ -27,9 +30,8 @@ class ImageCog(commands.Cog):
@commands.command(name="bandplan", aliases=['plan', 'bands'], category=cmn.cat.ref)
async def _bandplan(self, ctx: commands.Context, region: str = ''):
'''Posts an image of Frequency Allocations.'''
arg = region.lower()
with ctx.typing():
async with ctx.typing():
arg = region.lower()
embed = cmn.embed_factory(ctx)
if arg not in self.bandcharts:
desc = 'Possible arguments are:\n'
@ -39,25 +41,24 @@ class ImageCog(commands.Cog):
embed.description = desc
embed.colour = cmn.colours.bad
await ctx.send(embed=embed)
else:
metadata: cmn.ImageMetadata = self.bandcharts[arg]
img = discord.File(cmn.paths.bandcharts / metadata.filename,
filename=metadata.filename)
if metadata.description:
embed.description = metadata.description
if metadata.source:
embed.add_field(name="Source", value=metadata.source)
embed.title = metadata.long_name + (" " + metadata.emoji if metadata.emoji else "")
embed.colour = cmn.colours.good
embed.set_image(url='attachment://' + metadata.filename)
await ctx.send(embed=embed, file=img)
return
metadata: cmn.ImageMetadata = self.bandcharts[arg]
img = discord.File(cmn.paths.bandcharts / metadata.filename,
filename=metadata.filename)
if metadata.description:
embed.description = metadata.description
if metadata.source:
embed.add_field(name="Source", value=metadata.source)
embed.title = metadata.long_name + (" " + metadata.emoji if metadata.emoji else "")
embed.colour = cmn.colours.good
embed.set_image(url='attachment://' + metadata.filename)
await ctx.send(embed=embed, file=img)
@commands.command(name="map", category=cmn.cat.maps)
async def _map(self, ctx: commands.Context, map_id: str = ''):
'''Posts an image of a ham-relevant map.'''
arg = map_id.lower()
with ctx.typing():
async with ctx.typing():
arg = map_id.lower()
embed = cmn.embed_factory(ctx)
if arg not in self.maps:
desc = 'Possible arguments are:\n'
@ -67,36 +68,35 @@ class ImageCog(commands.Cog):
embed.description = desc
embed.colour = cmn.colours.bad
await ctx.send(embed=embed)
else:
metadata: cmn.ImageMetadata = self.maps[arg]
img = discord.File(cmn.paths.maps / metadata.filename,
filename=metadata.filename)
if metadata.description:
embed.description = metadata.description
if metadata.source:
embed.add_field(name="Source", value=metadata.source)
embed.title = metadata.long_name + (" " + metadata.emoji if metadata.emoji else "")
embed.colour = cmn.colours.good
embed.set_image(url='attachment://' + metadata.filename)
await ctx.send(embed=embed, file=img)
return
metadata: cmn.ImageMetadata = self.maps[arg]
img = discord.File(cmn.paths.maps / metadata.filename,
filename=metadata.filename)
if metadata.description:
embed.description = metadata.description
if metadata.source:
embed.add_field(name="Source", value=metadata.source)
embed.title = metadata.long_name + (" " + metadata.emoji if metadata.emoji else "")
embed.colour = cmn.colours.good
embed.set_image(url='attachment://' + metadata.filename)
await ctx.send(embed=embed, file=img)
@commands.command(name="grayline", aliases=['greyline', 'grey', 'gray', 'gl'], category=cmn.cat.maps)
async def _grayline(self, ctx: commands.Context):
'''Posts a map of the current greyline, where HF propagation is the best.'''
gl_url = ('http://www.fourmilab.ch/cgi-bin/uncgi/Earth?img=NOAAtopo.evif'
'&imgsize=320&dynimg=y&opt=-p&lat=&lon=&alt=&tle=&date=0&utc=&jd=')
with ctx.typing():
async with ctx.typing():
embed = cmn.embed_factory(ctx)
embed.title = 'Current Greyline Conditions'
embed.colour = cmn.colours.good
async with self.session.get(gl_url) as resp:
async with self.session.get(self.gl_url) as resp:
if resp.status != 200:
embed.description = 'Could not download file...'
embed.colour = cmn.colours.bad
else:
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://greyline.jpg')
await ctx.send(embed=embed, file=discord.File(data, 'greyline.jpg'))
await ctx.send(embed=embed)
return
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://greyline.jpg')
await ctx.send(embed=embed, file=discord.File(data, 'greyline.jpg'))
def setup(bot: commands.Bot):

View File

@ -28,7 +28,7 @@ class WeatherCog(commands.Cog):
@commands.command(name="bandconditions", aliases=['cond', 'condx', 'conditions'], category=cmn.cat.weather)
async def _band_conditions(self, ctx: commands.Context):
'''Posts an image of HF Band Conditions.'''
with ctx.typing():
async with ctx.typing():
embed = cmn.embed_factory(ctx)
embed.title = 'Current Solar Conditions'
embed.colour = cmn.colours.good
@ -36,10 +36,11 @@ class WeatherCog(commands.Cog):
if resp.status != 200:
embed.description = 'Could not download file...'
embed.colour = cmn.colours.bad
else:
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://condx.png')
await ctx.send(embed=embed, file=discord.File(data, 'condx.png'))
await ctx.send(embed=embed)
return
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://condx.png')
await ctx.send(embed=embed, file=discord.File(data, 'condx.png'))
@commands.group(name="weather", aliases=['wttr'], category=cmn.cat.weather)
async def _weather_conditions(self, ctx: commands.Context):
@ -61,7 +62,7 @@ class WeatherCog(commands.Cog):
async def _weather_conditions_forecast(self, ctx: commands.Context, *, location: str):
'''Posts an image of Local Weather Conditions for the next three days from [wttr.in](http://wttr.in/).
See help for weather command for possible location types. Add a `-c` or `-f` to use Celcius or Fahrenheit.'''
with ctx.typing():
async with ctx.typing():
try:
units_arg = re.search(self.wttr_units_regex, location).group(1)
except AttributeError:
@ -85,16 +86,17 @@ See help for weather command for possible location types. Add a `-c` or `-f` to
if resp.status != 200:
embed.description = 'Could not download file...'
embed.colour = cmn.colours.bad
else:
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://wttr_forecast.png')
await ctx.send(embed=embed, file=discord.File(data, 'wttr_forecast.png'))
await ctx.send(embed=embed)
return
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://wttr_forecast.png')
await ctx.send(embed=embed, file=discord.File(data, 'wttr_forecast.png'))
@_weather_conditions.command(name='now', aliases=['n'], category=cmn.cat.weather)
async def _weather_conditions_now(self, ctx: commands.Context, *, location: str):
'''Posts an image of current Local Weather Conditions from [wttr.in](http://wttr.in/).
See help for weather command for possible location types. Add a `-c` or `-f` to use Celcius or Fahrenheit.'''
with ctx.typing():
async with ctx.typing():
try:
units_arg = re.search(self.wttr_units_regex, location).group(1)
except AttributeError:
@ -118,10 +120,11 @@ See help for weather command for possible location types. Add a `-c` or `-f` to
if resp.status != 200:
embed.description = 'Could not download file...'
embed.colour = cmn.colours.bad
else:
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://wttr_now.png')
await ctx.send(embed=embed, file=discord.File(data, 'wttr_now.png'))
await ctx.send(embed=embed)
return
data = io.BytesIO(await resp.read())
embed.set_image(url=f'attachment://wttr_now.png')
await ctx.send(embed=embed, file=discord.File(data, 'wttr_now.png'))
def setup(bot: commands.Bot):