Ejemplo n.º 1
0
    async def open(self, ctx, name: str = None):
        if name and len(name) > 20:
            raise ArgumentFormatException(
                'Session name length exceeds 20 characters limit')

        # Auto-generated name
        if not name:
            name = str(len(sessions().sessions))

        content = parse_template(settings().get('terminal.template'),
                                 name=name,
                                 state='OPENING',
                                 content='Waiting for tty..')
        message = await ctx.send(content)

        # Prepare terminal
        sh_path = settings().get('terminal.shell_path')
        terminal = Terminal(name,
                            sh_path=sh_path,
                            on_change=sessions().update_message)
        sessions().add(message, terminal)
        terminal.open()

        # Run macro on terminal startup
        startup_macro = settings().get('terminal.startup_macro')
        if startup_macro:
            await execute_macro(ctx, startup_macro)
Ejemplo n.º 2
0
async def execute_macro(ctx, name):
    terminal = sessions().get_by_channel(ctx.channel)

    if name not in settings().macros:
        raise MacroNotFoundException()

    macro = settings().macros[name]
    for line in macro.split('\n'):
        if extract_prefix(line):
            ctx.message.content = line
            await ctx.bot.process_commands(ctx.message)
        else:
            terminal.input(line)
Ejemplo n.º 3
0
    async def on_ready(self):
        self.logger.info(f'Logged in as {self.user.name} ({self.user.id})')
        self.logger.info(
            f'You can add bot to your server via {oauth_url(self.user.id)}')

        presence = parse_template(settings().get("discord.presence"),
                                  prefix=self.command_prefix)
        await self.change_presence(status=discord.Status.online,
                                   activity=discord.Game(presence))
Ejemplo n.º 4
0
    def update_message(self, terminal: Terminal, content: str):
        message = self.get_message(terminal)

        content = parse_template(settings().get('terminal.template'),
                                 name=terminal.name,
                                 state=terminal.state.name,
                                 content=block_escape(content))

        execute_async(message.edit, content=content)
Ejemplo n.º 5
0
    async def close(self, ctx):
        terminal = sessions().get_by_channel(ctx.message.channel)

        if not terminal:
            raise SessionDontExistException()

        terminal.close()

        if settings().get('terminal.delete_on_close'):
            message = sessions().get_message(terminal)
            await message.delete()

        sessions().remove(terminal)
        await ctx.send(f"`Closed terminal #{terminal.name}`")
Ejemplo n.º 6
0
def launch():
    setup_logger()
    settings().load()
    settings().load_macros()

    prefix = settings().get('commands.prefixes', ['$'])[0]
    token = settings().get('discord.token')

    if token == 'TOKEN_HERE':
        logger.error('You need to specify bot TOKEN in config.toml')
        return

    try:
        BashBot(prefix).run(token)
    except LoginFailure as e:
        logger.error(e.args[0])
Ejemplo n.º 7
0
def is_command(content):
    return any(
        content.startswith(prefix + '.')
        for prefix in settings().get('commands.prefixes'))
Ejemplo n.º 8
0
def extract_prefix(content):
    for prefix in settings().get('commands.prefixes'):
        if content.startswith(prefix):
            return prefix