Commit 71849b5d authored by Jeremy Pallats's avatar Jeremy Pallats 💬
Browse files

FIX: Issue with slicing cmd from content.

- Off by one on string slice. Fix with tested function.
parent ce8aea30
Loading
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ class CogBot(discord.Client):

            with cogdb.session_scope(cogdb.Session) as session:
                # Check permissions before full parsing
                cmd = content[len(self.prefix):content.find(' ')]
                cmd = cmd_from_content(self.prefix, content)
                cogdb.query.check_perms(session, message, cmd)

                args = self.parser.parse_args(re.split(r'\s+', content))
@@ -492,6 +492,20 @@ async def simple_heartbeat(delay=30):
        await asyncio.sleep(delay)


def cmd_from_content(prefix, content):
    """
    Determine the command that was asked for from the content of the message.

    Returns: The command without the prefix or any other content.
    """
    cont = content[len(prefix):]
    index = content.find(' ')
    if index != -1:
        cont = cont[:index - 1]

    return cont


def main():  # pragma: no cover
    """ Entry here! """
    sqlalchemy_log = '--db' in sys.argv
+15 −1
Original line number Diff line number Diff line
"""
Test the bot's main functions.
"""
from cog.bot import EmojiResolver
from cog.bot import EmojiResolver, cmd_from_content
from tests.conftest import Guild, Emoji


@@ -44,3 +44,17 @@ def test_emoji_fix():
    emo.update([guild])

    assert emo.fix(":duck: :sleep:", guild) == "[duck] [sleep]"


def test_cmd_from_content_no_space():
    content = "!fort"
    prefix = '!'

    assert "fort" == cmd_from_content(prefix, content)


def test_cmd_from_content_has_space():
    content = "!bgs subCmd some text"
    prefix = '!'

    assert "bgs" == cmd_from_content(prefix, content)