Merge pull request #381 from miaowware/maplan

Moved map/plan code to a single function
This commit is contained in:
0x5c 2021-03-27 18:07:14 -04:00 committed by GitHub
commit c569fdb422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 45 deletions

View File

@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- `?tex` command to render a LaTeX expression.
- Configuration option to use another rTeX instance for `?tex`.
### Changed
- Main name and aliases of `?bandplan`.
### Fixed
- Lack of input sanitisation in `?xkcd`.

View File

@ -100,7 +100,7 @@ class ImagesGroup(collections.abc.Mapping):
def __len__(self):
return len(self._images)
def __getitem__(self, key: str):
def __getitem__(self, key: str) -> ImageMetadata:
return self._images[key]
def __iter__(self):

View File

@ -27,55 +27,15 @@ class ImageCog(commands.Cog):
self.maps = cmn.ImagesGroup(cmn.paths.resources / "maps.1.json")
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
@commands.command(name="bandplan", aliases=["plan", "bands"], category=cmn.cat.ref)
async def _bandplan(self, ctx: commands.Context, region: str = ""):
@commands.command(name="bandchart", aliases=["bandplan", "plan", "bands"], category=cmn.cat.ref)
async def _bandcharts(self, ctx: commands.Context, chart_id: str = ""):
"""Gets the frequency allocations chart for a given country."""
async with ctx.typing():
arg = region.lower()
embed = cmn.embed_factory(ctx)
if arg not in self.bandcharts:
desc = "Possible arguments are:\n"
for key, img in self.bandcharts.items():
desc += f"`{key}`: {img.name}{(' ' + img.emoji if img.emoji else '')}\n"
embed.title = "Bandplan Not Found!"
embed.description = desc
embed.colour = cmn.colours.bad
await ctx.send(embed=embed)
return
metadata: cmn.ImageMetadata = self.bandcharts[arg]
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=opt.resources_url + metadata.filename)
await ctx.send(embed=embed)
await ctx.send(embed=create_embed(ctx, "Bandchart", self.bandcharts, chart_id))
@commands.command(name="map", category=cmn.cat.maps)
async def _map(self, ctx: commands.Context, map_id: str = ""):
"""Posts a ham-relevant map."""
async with ctx.typing():
arg = map_id.lower()
embed = cmn.embed_factory(ctx)
if arg not in self.maps:
desc = "Possible arguments are:\n"
for key, img in self.maps.items():
desc += f"`{key}`: {img.name}{(' ' + img.emoji if img.emoji else '')}\n"
embed.title = "Map Not Found!"
embed.description = desc
embed.colour = cmn.colours.bad
await ctx.send(embed=embed)
return
metadata: cmn.ImageMetadata = self.maps[arg]
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=opt.resources_url + metadata.filename)
await ctx.send(embed=embed)
await ctx.send(embed=create_embed(ctx, "Map", self.maps, map_id))
@commands.command(name="grayline", aliases=["greyline", "grey", "gray", "gl"], category=cmn.cat.maps)
async def _grayline(self, ctx: commands.Context):
@ -88,5 +48,28 @@ class ImageCog(commands.Cog):
await ctx.send(embed=embed)
def create_embed(ctx: commands.Context, not_found_name: str, db: cmn.ImagesGroup, img_id: str):
"""Creates an embed for the image and its metadata, or list available images in the group."""
img_id = img_id.lower()
embed = cmn.embed_factory(ctx)
if img_id not in db:
desc = "Possible arguments are:\n"
for key, img in db.items():
desc += f"`{key}`: {img.name}{(' ' + img.emoji if img.emoji else '')}\n"
embed.title = f"{not_found_name} Not Found!"
embed.description = desc
embed.colour = cmn.colours.bad
return embed
metadata = db[img_id]
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=opt.resources_url + metadata.filename)
return embed
def setup(bot: commands.Bot):
bot.add_cog(ImageCog(bot))