Exemple #1
0
    async def set_command(
        self,
        ctx: Context,
        tag_name: TagNameConverter,
        *,
        tag_content: TagContentConverter,
    ) -> None:
        """Create a new tag."""
        body = {
            'title': tag_name.lower().strip(),
            'embed': {
                'title': tag_name,
                'description': tag_content
            }
        }

        await self.bot.api_client.post('bot/tags', json=body)
        self._cache[tag_name.lower()] = await self.bot.api_client.get(f'bot/tags/{tag_name}')

        log.debug(f"{ctx.author} successfully added the following tag to our database: \n"
                  f"tag_name: {tag_name}\n"
                  f"tag_content: '{tag_content}'\n")

        await ctx.send(embed=Embed(
            title="Tag successfully added",
            description=f"**{tag_name}** added to tag database.",
            colour=Colour.blurple()
        ))
Exemple #2
0
    async def set_command(
        self,
        ctx: Context,
        tag_name: TagNameConverter,
        *,
        tag_content: TagContentConverter,
    ):
        """
        Create a new tag or update an existing one.

        :param ctx: discord message context
        :param tag_name: The name of the tag to create or edit.
        :param tag_content: The content of the tag.
        """

        body = {
            'title': tag_name.lower().strip(),
            'embed': {
                'title': tag_name,
                'description': tag_content
            }
        }

        await self.bot.api_client.post('bot/tags', json=body)

        log.debug(f"{ctx.author} successfully added the following tag to our database: \n"
                  f"tag_name: {tag_name}\n"
                  f"tag_content: '{tag_content}'\n")

        await ctx.send(embed=Embed(
            title="Tag successfully added",
            description=f"**{tag_name}** added to tag database.",
            colour=Colour.blurple()
        ))
Exemple #3
0
    async def edit_command(
        self,
        ctx: Context,
        tag_name: TagNameConverter,
        *,
        tag_content: TagContentConverter,
    ) -> None:
        """Edit an existing tag."""
        body = {
            'embed': {
                'title': tag_name,
                'description': tag_content
            }
        }

        await self.bot.api_client.patch(f'bot/tags/{tag_name}', json=body)
        self._cache[tag_name.lower()] = await self.bot.api_client.get(f'bot/tags/{tag_name}')

        log.debug(f"{ctx.author} successfully edited the following tag in our database: \n"
                  f"tag_name: {tag_name}\n"
                  f"tag_content: '{tag_content}'\n")

        await ctx.send(embed=Embed(
            title="Tag successfully edited",
            description=f"**{tag_name}** edited in the database.",
            colour=Colour.blurple()
        ))
Exemple #4
0
    async def delete_command(self, ctx: Context, *, tag_name: TagNameConverter):
        """
        Remove a tag from the database.

        :param ctx: discord message context
        :param tag_name: The name of the tag to delete.
        """

        tag_name = tag_name.lower().strip()
        embed = Embed()
        embed.colour = Colour.red()
        tag_data = await self.delete_tag_data(tag_name)

        if tag_data.get("success") is True:
            log.debug(f"{ctx.author} successfully deleted the tag called '{tag_name}'")
            embed.colour = Colour.blurple()
            embed.title = tag_name
            embed.description = f"Tag successfully removed: {tag_name}."

        elif tag_data.get("success") is False:
            log.debug(f"{ctx.author} tried to delete a tag called '{tag_name}', but the tag does not exist.")
            embed.colour = Colour.red()
            embed.title = tag_name
            embed.description = "Tag doesn't appear to exist."

        else:
            log.error("There was an unexpected database error when trying to delete the following tag: \n"
                      f"tag_name: {tag_name}\n"
                      f"response: {tag_data}")
            embed.title = "Database error"
            embed.description = ("There was an unexpected error with deleting the data from the tags database. "
                                 "Please try again. If the problem persists, see the error logs.")

        return await ctx.send(embed=embed)
Exemple #5
0
    async def delete_command(self, ctx: Context, *, tag_name: TagNameConverter) -> None:
        """Remove a tag from the database."""
        await self.bot.api_client.delete(f'bot/tags/{tag_name}')
        self._cache.pop(tag_name.lower(), None)

        log.debug(f"{ctx.author} successfully deleted the tag called '{tag_name}'")
        await ctx.send(embed=Embed(
            title=tag_name,
            description=f"Tag successfully removed: {tag_name}.",
            colour=Colour.blurple()
        ))
Exemple #6
0
    def test_tag_name_converter_for_valid(self):
        """TagNameConverter should return the correct values for valid tag names."""
        test_values = (
            ('tracebacks', 'tracebacks'),
            ('Tracebacks', 'tracebacks'),
            ('  Tracebacks  ', 'tracebacks'),
        )

        for name, expected_conversion in test_values:
            with self.subTest(name=name, expected_conversion=expected_conversion):
                conversion = asyncio.run(TagNameConverter.convert(self.context, name))
                self.assertEqual(conversion, expected_conversion)
Exemple #7
0
    def test_tag_name_converter_for_invalid(self):
        """TagNameConverter should raise the correct exception for invalid tag names."""
        test_values = (
            ('👋', "Don't be ridiculous, you can't use that character!"),
            ('', "Tag names should not be empty, or filled with whitespace."),
            ('  ', "Tag names should not be empty, or filled with whitespace."),
            ('42', "Tag names can't be numbers."),
            ('x' * 128, "Are you insane? That's way too long!"),
        )

        for invalid_name, exception_message in test_values:
            with self.subTest(invalid_name=invalid_name, exception_message=exception_message):
                with self.assertRaises(BadArgument, msg=exception_message):
                    asyncio.run(TagNameConverter.convert(self.context, invalid_name))
Exemple #8
0
    async def set_command(self,
                          ctx: Context,
                          tag_name: TagNameConverter,
                          tag_content: TagContentConverter,
                          image_url: ValidURL = None):
        """
        Create a new tag or edit an existing one.

        :param ctx: discord message context
        :param tag_name: The name of the tag to create or edit.
        :param tag_content: The content of the tag.
        :param image_url: An optional image for the tag.
        """

        tag_name = tag_name.lower().strip()
        tag_content = tag_content.strip()

        embed = Embed()
        embed.colour = Colour.red()
        tag_data = await self.post_tag_data(tag_name, tag_content, image_url)

        if tag_data.get("success"):
            log.debug(
                f"{ctx.author} successfully added the following tag to our database: \n"
                f"tag_name: {tag_name}\n"
                f"tag_content: '{tag_content}'\n"
                f"image_url: '{image_url}'")
            embed.colour = Colour.blurple()
            embed.title = "Tag successfully added"
            embed.description = f"**{tag_name}** added to tag database."
        else:
            log.error(
                "There was an unexpected database error when trying to add the following tag: \n"
                f"tag_name: {tag_name}\n"
                f"tag_content: '{tag_content}'\n"
                f"image_url: '{image_url}'\n"
                f"response: {tag_data}")
            embed.title = "Database error"
            embed.description = (
                "There was a problem adding the data to the tags database. "
                "Please try again. If the problem persists, see the error logs."
            )

        return await ctx.send(embed=embed)
Exemple #9
0
def test_tag_name_converter_for_invalid(value: str, expected: str):
    context = MagicMock()
    context.author = 'bob'

    with pytest.raises(BadArgument, match=expected):
        asyncio.run(TagNameConverter.convert(context, value))
Exemple #10
0
def test_tag_name_converter_for_valid(value: str, expected: str):
    assert asyncio.run(TagNameConverter.convert(None, value)) == expected