Exemplo n.º 1
0
Arquivo: jokes.py Projeto: RossKM/Nano
    async def add_to_cache(self, number, data) -> bool:
        if not is_number(number):
            return False

        if not self.exists_in_cache(number):
            self.cache.hmset(int(number), data)
            return True
        else:
            return False
Exemplo n.º 2
0
    async def on_message(self, message, **kwargs):
        trans = self.trans
        mc = self.mc

        prefix = kwargs.get("prefix")
        lang = kwargs.get("lang")

        # Check if this is a valid command
        if not is_valid_command(message.content, commands, prefix):
            return
        else:
            self.stats.add(MESSAGE)

        def startswith(*matches):
            for match in matches:
                if message.content.startswith(match):
                    return True

            return False

        # !mc
        if startswith(prefix + "mc"):
            argument = message.content[len(prefix + "mc "):].strip(" ").lower()

            if not argument:
                await message.channel.send(trans.get("MSG_MC_PLSARUGMENTS", lang))
                return

            # !mc help
            if argument == "help":
                await message.channel.send(trans.get("MSG_MC_HELP", lang).replace("_", prefix))
                return

            # Argument is name
            if not is_number(argument.split(":")[0]):
                # Check for groupings
                gr = mc.get_group_by_name(argument)
                if gr:
                    data = gr
                # Not a group
                else:
                    data = mc.find_by_name(str(argument))

            # Argument is id and meta
            else:
                try:
                    i_type, i_meta = argument.split(":")
                except ValueError:
                    i_type, i_meta = argument, 0

                data = mc.find_by_id_meta(i_type, i_meta)

            if not data:
                await message.channel.send(trans.get("MSG_MC_NO_ITEMS", lang))
                self.stats.add(WRONG_ARG)
                return

            # One item, not a group
            if not isinstance(data, list):
                details = trans.get("MSG_MC_DETAILS", lang).format(data.get("name"), data.get("type"), data.get("meta"))

                # Details are uploaded simultaneously with the picture

                # No image
                path = mc.get_picture_path_by_item(data)
                if not path:
                    await message.channel.send(details)
                    self.stats.add(IMAGE_SENT)
                else:
                    with open(mc.get_picture_path_by_item(data), "rb") as pic:
                        await message.channel.send(details, file=File(pic))
                        self.stats.add(IMAGE_SENT)

            # Multiple items, a group
            else:
                combined = []
                for item in data:
                    details = trans.get("MSG_MC_DETAILS", lang).format(item.get("name"), item.get("type"), item.get("meta"))
                    combined.append(details)

                await message.channel.send("".join(combined))
Exemplo n.º 3
0
 def qs(self):
     menu_parent = self.data.get('menu_parent', None)
     if menu_parent and not is_number(menu_parent):
         self.data['menu_parent'] = decode_id(menu_parent)
     return super().qs
Exemplo n.º 4
0
Arquivo: jokes.py Projeto: RossKM/Nano
    async def on_message(self, message, **kwargs):
        trans = self.trans

        prefix = kwargs.get("prefix")
        lang = kwargs.get("lang")

        # Check if this is a valid command
        if not is_valid_command(message.content, commands, prefix):
            return
        else:
            self.stats.add(MESSAGE)

        def startswith(*matches):
            for match in matches:
                if message.content.startswith(match):
                    return True

            return False

        # !cat gif/jpg/png
        if startswith(prefix + "cat"):
            fmt = str(message.content[len(prefix + "cat"):]).strip(" ")

            # GIF is the default type!
            if fmt == "jpg":
                type_ = "jpg"
            elif fmt == "png":
                type_ = "png"
            else:
                type_ = "gif"

            pic = await self.cats.random_cat(type_)

            if pic:
                # Teal (blue-ish)
                embed = Embed(colour=Colour(0x00796b))
                embed.set_image(url=pic)
                embed.set_footer(text=trans.get("MSG_CAT_FOOTER", lang))

                await message.channel.send(embed=embed)
            else:
                await message.channel.send(trans.get("MSG_CAT_FAILED", lang))

            self.stats.add(IMAGE_SENT)

        # !xkcd random/number/latest
        elif startswith(prefix + "xkcd"):
            fmt = str(message.content[len(prefix + "xkcd"):]).strip(" ")

            # Decides mode
            fetch = "random"
            if fmt:
                if is_number(fmt):
                    # Check if number is valid
                    if int(fmt) > self.xkcd.last_num:
                        await message.channel.send(
                            trans.get("MSG_XKCD_NO_SUCH", lang))
                        return
                    else:
                        fetch = "number"
                elif fmt == trans.get("INFO_RANDOM", lang) or fmt == "random":
                    fetch = "random"
                # Any other argument means latest
                else:
                    fetch = "latest"
            # Default: random
            else:
                fetch == "random"

            if fetch == "random":
                xkcd = await self.xkcd.get_random_xkcd()
            elif fetch == "number":
                xkcd = await self.xkcd.get_xkcd_by_number(fmt)
            # Can only mean latest
            else:
                xkcd = await self.xkcd.get_latest_xkcd()

            # In case something went wrong
            if not xkcd:
                await message.channel.send(trans.get("MSG_XKCD_FAILED", lang))
                log_to_file("XKCD: string {}, fetch: {}, got None".format(
                    fmt, fetch))

            xkcd_link = self.xkcd.make_link(xkcd["num"])

            embed = Embed(title=trans.get("MSG_XKCD",
                                          lang).format(xkcd["num"]),
                          description=filter_text(xkcd["safe_title"]))
            embed.set_image(url=xkcd["img"])
            embed.set_footer(
                text=trans.get("MSG_XKCD_SOURCE", lang).format(xkcd_link))

            await message.channel.send(embed=embed)

        # !joke (yo mama/chuck norris)
        elif startswith(prefix + "joke"):
            content = filter_text(self.joke.random_joke())

            embed = Embed(description=content)
            await message.channel.send(embed=embed)