Example #1
0
    async def weather(self, ctx, *, loc: str):
        """Grabs the weather from OpenWeatherMap"""
        if self.owm:
            try:
                future = self.loop.run_in_executor(None,
                                                   self.owm.weather_at_place,
                                                   loc)
                observation = await future

            except pyowm.exceptions.not_found_error.NotFoundError:
                return await ctx.send(
                    'Location not found, maybe be more specific')
            except Exception as e:
                raise uerrs.ServiceError(e)
            w = observation.get_weather()
            _wg = lambda t: w.get_temperature(t)['temp']
            _icon = w.get_weather_icon_name()
            icon = OWM_ICONS.get(_icon, '*<no icon>*')
            status = w.get_detailed_status()
            em = discord.Embed(title=f"Weather for '{loc}'", colour=0x690E8)
            o_location = observation.get_location()
            em.add_field(name='Location', value=f'{o_location.get_name()}')
            em.add_field(name='Situation', value=f'{status} {icon}')
            em.add_field(
                name='Temperature',
                value=f'`{_wg("celsius")} °C, {_wg("fahrenheit")} °F`')
            await ctx.send(embed=em)
        else:
            raise uerrs.ServiceError(
                'This instance does not have a OpenWeatherMap API key configured.'
            )
Example #2
0
File: wa.py Project: Mstrodl/lolbot
    async def wolframalpha(self, ctx, *, q: str):
        """Access Wolfram|Alpha, through lolbot"""
        if self.wa:
            if len(q) < 1:
                raise uerrs.ServiceError(
                    'You can\'t make a query with less than 1 character...')

            ftr = self.bot.loop.run_in_executor(None, self.wa.query, q)
            res = None

            async with ctx.typing():
                try:
                    res = await asyncio.wait_for(ftr, 13)
                except asyncio.TimeoutError:
                    raise uerrs.ServiceError('Timeout reached')
                except Exception as e:
                    raise uerrs.ServiceError(f'W|A didn\'t wanna work: {e!r}')

            if res is None:
                raise uerrs.ServiceError('W|A didn\'t reply')

            if not res.success:
                raise uerrs.ServiceError(
                    'W|A failed for some reason. Maybe try again later?')

            if not getattr(res, 'pods', False):
                em = discord.Embed(title='No answer', colour=0x690E8)
                return await ctx.send(embed=em)

            pods = list(res.pods)

            pod = pod_finder(pods)

            def subpod_simplify(subpod):
                if subpod.get('img'):
                    return subpod['img']['@src']
                return subpod['plaintext']

            if isinstance(pod['subpod'], dict):
                # in this case, we only have one
                if 'MSPStoreType=image' in subpod_simplify(pod['subpod']):
                    em = discord.Embed(colour=0x690E8)
                    em.set_image(url=subpod_simplify(pod['subpod']))
                    await ctx.send(embed=em)
                else:
                    # but maybe we have 2, or 3, or 4.
                    # we'll just choose the first
                    if 'MSPStoreType=image' in subpod_simplify(
                            pod['subpod'][0]):
                        em = discord.Embed(colour=0x690E8)
                        em.set_image(url=subpod_simplify(pod['subpod']))
                        await ctx.send(embed=em)
                    else:
                        em = discord.Embed(description=subpod_simplify(
                            pod['subpod'][0]),
                                           colour=0x690E8)
                        await ctx.send(embed=em)
        else:
            raise uerrs.ServiceError('This instance does not have a'
                                     'Wolfram|Alpha key set up.')