Example #1
0
    async def add(self, ctx, *, msg: str = None):
        """Add a new customcmd. [p]help add for more info

        Simply do: [p]add
        This will trigger the menu which you can navigate through and add your custom command that way.

        -----------------------------------------------------------

        Legacy method:

        There are two ways to add custom commands. The first way:
        ----Simple----
        [p]add <command> <response> Now, if you do .<command> you will receive <response>.
        Example: [p]add nervous http://i.imgur.com/K9gMjWo.gifv
        Then, doing .nervous will output this imgur link (images and gifs will auto embed) Assuming that your customcmd_prefix is set to "."

        ---Multiple responses to the same command----
        [p]add <command> <response_name> <response>. This way, you can add multiple responses to the same command.
        Example:
        [p]add cry k-on http://i.imgur.com/tWtXttk.gif

        Then you can add another to the .cry command:
        [p]add cry nichijou https://media.giphy.com/media/3fmRTfVIKMRiM/giphy.gif

        Note: If anything you are adding/removing is more than one word, you MUST put each part in quotes.
        Example: [p]add "cry" "mugi why" "http://i.imgur.com/tWtXttk.gif" or [p]add "copypasta" "I identify as an attack helicopter."

        Then invoke a specific response with .<command> <response_name> or get a random response for that command with .<command>
        So: .cry k-on would give you that specific link but .cry would give you one of the two you added to the cry command."""
        if not msg:

            await ctx.message.delete()
            pre = ctx.message.content.split('add')[0]
            customcmd_prefix = load_config()['customcmd_prefix']
            menu = await ctx.send(self.bot.bot_prefix + '```\n\u2795 Choose type of customcmd to add. Enter a number:\n\n1. Simple customcmd (1 cmd with 1 response).\n2. Customcmd with multiple responses.\n3. View current customcmds.```')

            reply = await self.check(ctx, 4, pre)
            if reply:
                await reply.delete()
                # Add simple customcmd
                if reply.content == "1":
                    await menu.edit(content=self.bot.bot_prefix + '```\n\u2795 Enter a cmd name. This is how you will invoke your response.```')
                    reply = await self.check(ctx, 0, pre)

                    # Grab the cmd name
                    if reply:
                        await reply.delete()
                        entry_cmd = reply.content
                        await menu.edit(content=
                                        self.bot.bot_prefix + '```\n\u2795 Enter the response for this cmd. This is what the bot will output when you send the cmd you specified.```')
                        reply = await self.check(ctx, 0, pre)

                        # Grab the response
                        if reply:
                            try:
                                await reply.delete()
                            except:
                                pass
                            entry_response = reply.content
                            with open('settings/commands.json', 'r+') as commands:
                                cmds = json.load(commands)
                                save = cmds
                                commands.seek(0)
                                commands.truncate()
                                try:
                                    cmds[entry_cmd] = entry_response
                                    json.dump(cmds, commands, indent=4)
                                    await menu.edit(content=
                                                    self.bot.bot_prefix + 'Successfully added ``{}`` to ``{}`` Invoke this response by doing: ``{}``'.format(
                                                        entry_response, entry_cmd,
                                                        customcmd_prefix + entry_cmd))
                                except Exception as e:

                                    json.dump(save, commands, indent=4)
                                    await menu.edit(content=
                                                    self.bot.bot_prefix + 'Error, something went wrong. Exception: ``%s``' % e)

                # Add complex customcmd
                elif reply.content == "2":
                    await menu.edit(content=
                                    self.bot.bot_prefix + '```\n\u2795 What to add? Pick a number.\n\n1. Add new command.\n2. Add response to existing command.```')
                    reply = await self.check(ctx, 3, pre)
                    if reply:
                        await reply.delete()

                        # Create new list cmd
                        if reply.content == '1':
                            await menu.edit(content=
                                            self.bot.bot_prefix + '```\n\u2795 Enter the cmd name.```')

                            reply = await self.check(ctx, 0, pre)

                            # Grab cmd name
                            if reply:
                                await reply.delete()
                                entry_cmd = reply.content
                                await menu.edit(content=
                                                self.bot.bot_prefix + '```\n\u2795 Since you selected to have this cmd have multiple responses, these multiple responses must have different names to map them. Enter a response name.```')
                                reply = await self.check(ctx, 0, pre)

                                # Grab response name
                                if reply:
                                    await reply.delete()
                                    entry_response = reply.content
                                    await menu.edit(content=
                                                    self.bot.bot_prefix + '```\n\u2795 Now enter the response.```')
                                    reply = await self.check(ctx, 0, pre)

                                    # Grab the response
                                    if reply:
                                        try:
                                            await reply.delete()
                                        except:
                                            pass
                                        response = reply.content
                                        with open('settings/commands.json', 'r+') as commands:
                                            cmds = json.load(commands)
                                            save = cmds
                                            commands.seek(0)
                                            commands.truncate()
                                            try:
                                                cmds[entry_cmd] = [[entry_response, response]]

                                                json.dump(cmds, commands, indent=4)
                                                await menu.edit(content=
                                                                self.bot.bot_prefix + 'Successfully added response with response name ``{}`` to command ``{}`` Invoke this specific response with ``{}`` or get a random response from the list of responses for this command with ``{}``'.format(
                                                                    entry_response, entry_cmd,
                                                                    customcmd_prefix + entry_cmd + ' ' + entry_response,
                                                                    customcmd_prefix + entry_cmd))
                                            except Exception as e:

                                                json.dump(save, commands, indent=4)
                                                await menu.edit(content=
                                                                self.bot.bot_prefix + 'Error, something went wrong. Exception: ``%s``' % e)

                        # Add to existing list cmd
                        elif reply.content == '2':
                            list_cmds = []
                            with open('settings/commands.json') as commands:
                                cmds = json.load(commands)
                            for i in cmds:
                                if type(cmds[i]) is list:
                                    list_cmds.append(i)
                            msg = '1. '
                            count = 0
                            for count, word in enumerate(list_cmds):
                                msg += '{}  {}.'.format(word, count + 2)

                            msg = msg[:-(len(str(count + 2)) + 2)]
                            if count == 0:
                                return await menu.edit(content=
                                                       self.bot.bot_prefix + 'There are no cmds you can add multiple responses to. Create a cmd that enables multiple responses and then add a response to it.')
                            await menu.edit(content=
                                            self.bot.bot_prefix + '```\n\u2795 Enter the number of the cmd name to add a response to.\n\n {}```'.format(msg))

                            reply = await self.check(ctx, count + 2, pre)

                            if reply:
                                await reply.delete()
                                entry_cmd = list_cmds[int(reply.content) - 1]

                                await menu.edit(content=
                                                self.bot.bot_prefix + '```\n\u2795 Enter a response name.```')
                                reply = await self.check(ctx, 0, pre)

                                # Grab response name
                                if reply:
                                    await reply.delete()
                                    entry_response = reply.content
                                    await menu.edit(content=
                                                    self.bot.bot_prefix + '```\n\u2795 Now enter the response.```')
                                    reply = await self.check(ctx, 0, pre)

                                    # Grab the response
                                    if reply:
                                        try:
                                            await reply.delete()
                                        except:
                                            pass
                                        response = reply.content
                                        with open('settings/commands.json', 'r+') as commands:
                                            save = cmds
                                            commands.seek(0)
                                            commands.truncate()
                                            try:
                                                cmds[entry_cmd].append([entry_response, response])

                                                json.dump(cmds, commands, indent=4)
                                                await menu.edit(content=
                                                                self.bot.bot_prefix + 'Successfully added response with response name ``{}`` to command ``{}`` Invoke this specific response with ``{}`` or get a random response from the list of responses for this command with ``{}``'.format(
                                                                    entry_response, entry_cmd,
                                                                    customcmd_prefix + entry_cmd + ' ' + entry_response,
                                                                    customcmd_prefix + entry_cmd))
                                            except Exception as e:

                                                json.dump(save, commands, indent=4)
                                                await menu.edit(content=
                                                                self.bot.bot_prefix + 'Error, something went wrong. Exception: ``%s``' % e)

                elif reply.content == '3':
                    await menu.delete()
                    await self.customcommands(ctx)

        else:
            words = msg.strip()

            with open('settings/commands.json', 'r') as commands:
                cmds = json.load(commands)
                save = cmds

            try:

                # If there are quotes in the message (meaning multiple words for each param)
                if '"' in words:
                    entry = re.findall('"([^"]+)"', words)

                    # Item for key is list
                    if len(entry) == 3:

                        # Key exists
                        if entry[0] in cmds:
                            entries = []
                            for i in cmds[entry[0]]:
                                entries.append(tuple((i[0], i[1])))
                            entries.append(tuple([entry[1], entry[2]]))
                            cmds[entry[0]] = entries
                        else:
                            cmds[entry[0]] = [(entry[1], entry[2])]

                    # Item for key is string
                    else:
                        if entry[0] in cmds:
                            if type(cmds[entry[0]]) is list:
                                return await ctx.send(self.bot.bot_prefix + 'Error, this is a list command. To append to this command, you need a <response name>. Ex: ``>add cmd response_name response``')
                        cmds[entry[0]] = entry[1]

                # No quotes so spaces seperate params
                else:

                    # Item for key is list
                    if len(words.split(' ')) == 3:
                        entry = words.split(' ', 2)

                        # Key exists
                        if entry[0] in cmds:
                            entries = []
                            for i in cmds[entry[0]]:
                                entries.append(tuple((i[0], i[1])))
                            entries.append(tuple([entry[1], entry[2]]))
                            cmds[entry[0]] = entries
                        else:
                            cmds[entry[0]] = [(entry[1], entry[2])]

                    # Item for key is string
                    else:
                        entry = words.split(' ', 1)
                        if entry[0] in cmds:
                            if type(cmds[entry[0]]) is list:
                                return await ctx.send(self.bot.bot_prefix + 'Error, this is a list command. To append to this command, you need a <response name>. Ex: ``>add cmd response_name response``')
                        cmds[entry[0]] = entry[1]

                await ctx.send(
                    self.bot.bot_prefix + 'Successfully added ``%s`` to ``%s``' % (entry[1], entry[0]))

            except Exception as e:
                with open('settings/commands.json', 'w') as commands:
                    commands.truncate()
                    json.dump(save, commands, indent=4)
                await ctx.send(self.bot.bot_prefix + 'Error, something went wrong. Exception: ``%s``' % e)

            # Update commands.json
            with open('settings/commands.json', 'w') as commands:
                commands.truncate()
                json.dump(cmds, commands, indent=4)
Example #2
0
    async def add(self, ctx, *, msg: str = None):
        """Add a new customcmd. [p]help add for more info

        Simply do: [p]add
        This will trigger the menu which you can navigate through and add your custom command that way.

        -----------------------------------------------------------

        Legacy method:

        There are two ways to add custom commands. The first way:
        ----Simple----
        [p]add <command> <response> Now, if you do .<command> you will receive <response>.
        Example: [p]add nervous http://i.imgur.com/K9gMjWo.gifv
        Then, doing .nervous will output this imgur link (images and gifs will auto embed) Assuming that your customcmd_prefix is set to "."

        ---Multiple responses to the same command----
        [p]add <command> <response_name> <response>. This way, you can add multiple responses to the same command.
        Example:
        [p]add cry k-on http://i.imgur.com/tWtXttk.gif

        Then you can add another to the .cry command:
        [p]add cry nichijou https://media.giphy.com/media/3fmRTfVIKMRiM/giphy.gif

        Note: If anything you are adding/removing is more than one word, you MUST put each part in quotes.
        Example: [p]add "cry" "mugi why" "http://i.imgur.com/tWtXttk.gif" or [p]add "copypasta" "I identify as an attack helicopter."

        Then invoke a specific response with .<command> <response_name> or get a random response for that command with .<command>
        So: .cry k-on would give you that specific link but .cry would give you one of the two you added to the cry command."""
        if not msg:

            await ctx.message.delete()
            pre = ctx.message.content.split('add')[0]
            customcmd_prefix = load_config()['customcmd_prefix']
            menu = await ctx.send(
                self.bot.bot_prefix +
                '```\n\u2795 Choose type of customcmd to add. Enter a number:\n\n1. Simple customcmd (1 cmd with 1 response).\n2. Customcmd with multiple responses.\n3. View current customcmds.```'
            )

            reply = await self.check(ctx, 4, pre)
            if reply:
                await reply.delete()
                # Add simple customcmd
                if reply.content == "1":
                    await menu.edit(
                        content=self.bot.bot_prefix +
                        '```\n\u2795 Enter a cmd name. This is how you will invoke your response.```'
                    )
                    reply = await self.check(ctx, 0, pre)

                    # Grab the cmd name
                    if reply:
                        await reply.delete()
                        entry_cmd = reply.content
                        await menu.edit(
                            content=self.bot.bot_prefix +
                            '```\n\u2795 Enter the response for this cmd. This is what the bot will output when you send the cmd you specified.```'
                        )
                        reply = await self.check(ctx, 0, pre)

                        # Grab the response
                        if reply:
                            try:
                                await reply.delete()
                            except:
                                pass
                            entry_response = reply.content
                            with open('settings/commands.json',
                                      'r+') as commands:
                                cmds = json.load(commands)
                                save = cmds
                                commands.seek(0)
                                commands.truncate()
                                try:
                                    cmds[entry_cmd] = entry_response
                                    json.dump(cmds, commands, indent=4)
                                    await menu.edit(
                                        content=self.bot.bot_prefix +
                                        'Successfully added ``{}`` to ``{}`` Invoke this response by doing: ``{}``'
                                        .format(entry_response, entry_cmd,
                                                customcmd_prefix + entry_cmd))
                                except Exception as e:

                                    json.dump(save, commands, indent=4)
                                    await menu.edit(
                                        content=self.bot.bot_prefix +
                                        'Error, something went wrong. Exception: ``%s``'
                                        % e)

                # Add complex customcmd
                elif reply.content == "2":
                    await menu.edit(
                        content=self.bot.bot_prefix +
                        '```\n\u2795 What to add? Pick a number.\n\n1. Add new command.\n2. Add response to existing command.```'
                    )
                    reply = await self.check(ctx, 3, pre)
                    if reply:
                        await reply.delete()

                        # Create new list cmd
                        if reply.content == '1':
                            await menu.edit(
                                content=self.bot.bot_prefix +
                                '```\n\u2795 Enter the cmd name.```')

                            reply = await self.check(ctx, 0, pre)

                            # Grab cmd name
                            if reply:
                                await reply.delete()
                                entry_cmd = reply.content
                                await menu.edit(
                                    content=self.bot.bot_prefix +
                                    '```\n\u2795 Since you selected to have this cmd have multiple responses, these multiple responses must have different names to map them. Enter a response name.```'
                                )
                                reply = await self.check(ctx, 0, pre)

                                # Grab response name
                                if reply:
                                    await reply.delete()
                                    entry_response = reply.content
                                    await menu.edit(
                                        content=self.bot.bot_prefix +
                                        '```\n\u2795 Now enter the response.```'
                                    )
                                    reply = await self.check(ctx, 0, pre)

                                    # Grab the response
                                    if reply:
                                        try:
                                            await reply.delete()
                                        except:
                                            pass
                                        response = reply.content
                                        with open('settings/commands.json',
                                                  'r+') as commands:
                                            cmds = json.load(commands)
                                            save = cmds
                                            commands.seek(0)
                                            commands.truncate()
                                            try:
                                                cmds[entry_cmd] = [[
                                                    entry_response, response
                                                ]]

                                                json.dump(cmds,
                                                          commands,
                                                          indent=4)
                                                await menu.edit(
                                                    content=self.bot.bot_prefix
                                                    +
                                                    'Successfully added response with response name ``{}`` to command ``{}`` Invoke this specific response with ``{}`` or get a random response from the list of responses for this command with ``{}``'
                                                    .format(
                                                        entry_response,
                                                        entry_cmd,
                                                        customcmd_prefix +
                                                        entry_cmd + ' ' +
                                                        entry_response,
                                                        customcmd_prefix +
                                                        entry_cmd))
                                            except Exception as e:

                                                json.dump(save,
                                                          commands,
                                                          indent=4)
                                                await menu.edit(
                                                    content=self.bot.bot_prefix
                                                    +
                                                    'Error, something went wrong. Exception: ``%s``'
                                                    % e)

                        # Add to existing list cmd
                        elif reply.content == '2':
                            list_cmds = []
                            with open('settings/commands.json') as commands:
                                cmds = json.load(commands)
                            for i in cmds:
                                if type(cmds[i]) is list:
                                    list_cmds.append(i)
                            msg = '1. '
                            count = 0
                            for count, word in enumerate(list_cmds):
                                msg += '{}  {}.'.format(word, count + 2)

                            msg = msg[:-(len(str(count + 2)) + 2)]
                            if count == 0:
                                return await menu.edit(
                                    content=self.bot.bot_prefix +
                                    'There are no cmds you can add multiple responses to. Create a cmd that enables multiple responses and then add a response to it.'
                                )
                            await menu.edit(
                                content=self.bot.bot_prefix +
                                '```\n\u2795 Enter the number of the cmd name to add a response to.\n\n {}```'
                                .format(msg))

                            reply = await self.check(ctx, count + 2, pre)

                            if reply:
                                await reply.delete()
                                entry_cmd = list_cmds[int(reply.content) - 1]

                                await menu.edit(
                                    content=self.bot.bot_prefix +
                                    '```\n\u2795 Enter a response name.```')
                                reply = await self.check(ctx, 0, pre)

                                # Grab response name
                                if reply:
                                    await reply.delete()
                                    entry_response = reply.content
                                    await menu.edit(
                                        content=self.bot.bot_prefix +
                                        '```\n\u2795 Now enter the response.```'
                                    )
                                    reply = await self.check(ctx, 0, pre)

                                    # Grab the response
                                    if reply:
                                        try:
                                            await reply.delete()
                                        except:
                                            pass
                                        response = reply.content
                                        with open('settings/commands.json',
                                                  'r+') as commands:
                                            save = cmds
                                            commands.seek(0)
                                            commands.truncate()
                                            try:
                                                cmds[entry_cmd].append(
                                                    [entry_response, response])

                                                json.dump(cmds,
                                                          commands,
                                                          indent=4)
                                                await menu.edit(
                                                    content=self.bot.bot_prefix
                                                    +
                                                    'Successfully added response with response name ``{}`` to command ``{}`` Invoke this specific response with ``{}`` or get a random response from the list of responses for this command with ``{}``'
                                                    .format(
                                                        entry_response,
                                                        entry_cmd,
                                                        customcmd_prefix +
                                                        entry_cmd + ' ' +
                                                        entry_response,
                                                        customcmd_prefix +
                                                        entry_cmd))
                                            except Exception as e:

                                                json.dump(save,
                                                          commands,
                                                          indent=4)
                                                await menu.edit(
                                                    content=self.bot.bot_prefix
                                                    +
                                                    'Error, something went wrong. Exception: ``%s``'
                                                    % e)

                elif reply.content == '3':
                    await menu.delete()
                    await self.customcommands(ctx)

        else:
            words = msg.strip()

            with open('settings/commands.json', 'r') as commands:
                cmds = json.load(commands)
                save = cmds

            try:

                # If there are quotes in the message (meaning multiple words for each param)
                if '"' in words:
                    entry = re.findall('"([^"]+)"', words)

                    # Item for key is list
                    if len(entry) == 3:

                        # Key exists
                        if entry[0] in cmds:
                            entries = []
                            for i in cmds[entry[0]]:
                                entries.append(tuple((i[0], i[1])))
                            entries.append(tuple([entry[1], entry[2]]))
                            cmds[entry[0]] = entries
                        else:
                            cmds[entry[0]] = [(entry[1], entry[2])]

                    # Item for key is string
                    else:
                        if entry[0] in cmds:
                            if type(cmds[entry[0]]) is list:
                                return await ctx.send(
                                    self.bot.bot_prefix +
                                    'Error, this is a list command. To append to this command, you need a <response name>. Ex: ``>add cmd response_name response``'
                                )
                        cmds[entry[0]] = entry[1]

                # No quotes so spaces seperate params
                else:

                    # Item for key is list
                    if len(words.split(' ')) == 3:
                        entry = words.split(' ', 2)

                        # Key exists
                        if entry[0] in cmds:
                            entries = []
                            for i in cmds[entry[0]]:
                                entries.append(tuple((i[0], i[1])))
                            entries.append(tuple([entry[1], entry[2]]))
                            cmds[entry[0]] = entries
                        else:
                            cmds[entry[0]] = [(entry[1], entry[2])]

                    # Item for key is string
                    else:
                        entry = words.split(' ', 1)
                        if entry[0] in cmds:
                            if type(cmds[entry[0]]) is list:
                                return await ctx.send(
                                    self.bot.bot_prefix +
                                    'Error, this is a list command. To append to this command, you need a <response name>. Ex: ``>add cmd response_name response``'
                                )
                        cmds[entry[0]] = entry[1]

                await ctx.send(self.bot.bot_prefix +
                               'Successfully added ``%s`` to ``%s``' %
                               (entry[1], entry[0]))

            except Exception as e:
                with open('settings/commands.json', 'w') as commands:
                    commands.truncate()
                    json.dump(save, commands, indent=4)
                await ctx.send(
                    self.bot.bot_prefix +
                    'Error, something went wrong. Exception: ``%s``' % e)

            # Update commands.json
            with open('settings/commands.json', 'w') as commands:
                commands.truncate()
                json.dump(cmds, commands, indent=4)