Exemplo n.º 1
0
    async def on_message(self, message):

        if message.author == self.client.user:
            # Just jump out if its the bots message.
            return

        # split the new messages by spaces
        parts = message.content.split()

        command = None
        args = None

        # If the first mention in the message is the bot itself
        # and there is a possible command in the message
        if len(message.mentions) == 1 and message.mentions[0] == self.client.user\
            and len(parts) > 1:
            command = parts[1]
            args = parts[2:]
        # If there are multiple mentions send each one (excluded the bot itself)
        # the help message.
        # Like: hey @dustinface and @whoever check out the @SmartProposals
        # The above would send @dustinface and @whoever the help message of the bot.
        elif len(
                message.mentions) > 1 and self.client.user in message.mentions:

            for mention in message.mentions:
                if not mention == self.client.user:

                    # Check if the user is already in the databse
                    result = commandhandler.checkUser(self, mention)

                    if result['response']:
                        await self.sendMessage(mention, result['response'])

                    if result['added']:
                        continue

                    await self.sendMessage(mention,
                                           messages.help(self.messenger))

            return
        # If there are no mentions and we are in a private chat
        elif len(message.mentions) == 0 and not isinstance(
                message.author, discord.Member):
            command = parts[0]
            args = parts[1:]
        # If we got mentioned but no command is available in the message just send the help
        elif len(message.mentions) and message.mentions[0] == self.client.user and\
              len(parts) == 1:
            command = 'help'
        # No message of which the bot needs to know about.
        else:
            logger.debug("on_message - jump out {}".format(self.client.user))
            return

        # If we got here call the command handler to see if there is any action required now.
        await self.commandHandler(message, command.lower(), args)
Exemplo n.º 2
0
    def help(self, bot, update):

        self.sendMessage(update.message.chat_id, messages.help(self.messenger))
Exemplo n.º 3
0
    def started(self, bot, update):

        self.sendMessage(update.message.chat_id, '**Welcome**\n\n' + messages.help(self.messenger))
Exemplo n.º 4
0
    async def commandHandler(self, message, command, args):

        logger.info("commandHandler - {}, command: {}, args: {}".format(message.author, command, args))

        # Check if the user is already in the databse
        result = common.checkUser(self, message)

        if result['response']:
            await self.sendMessage(message.author, result['response'])

        if result['added'] and not isinstance(message.author, discord.Member):
            return

        # per default assume the message gets back from where it came
        receiver = message.author

        ####
        # List of available commands
        # Public = 0
        # DM-Only = 1
        # Admin only = 2
        ####
        commands = {
                    # Common commands
                    'help':0, 'info':0, 'faq':0,
                    # User commmands
                    'me':1,'status':1,'reward':1,'network':1, 'timeout':1,
                    # Node commands
                    'add':1,'update':1,'remove':1,'nodes':1, 'detail':1,'history':1, 'balance':1, 'lookup':1, 'top':1,
                    # Admin commands
                    'stats':2, 'broadcast':2, 'payouts':2
        }

        choices = fuzzy.extract(command,commands.keys(),limit=2)

        if choices[0][1] == choices[1][1] or choices[0][1] < 60:
            logger.debug('Invalid fuzzy result {}'.format(choices))
            command = 'unknown'
        else:
            command = choices[0][0]

        # If the command is DM only
        if command in commands and commands[command] == 1:

            if isinstance(message.author, discord.Member):
             await self.client.send_message(message.channel,\
             message.author.mention + ', the command `{}` is only available in private chat with me!'.format(command))
             await self.client.send_message(message.author, messages.markdown('<b>Try it here!<b>\n', self.messenger))
             return

        else:
            receiver = message.channel

        # If the command is admin only
        if command in commands and commands[command] == 2:

            # Admin command got fired in a public chat
            if isinstance(message.author, discord.Member):
                # Just send the unknown command message and jump out
                await self.sendMessage(receiver, (message.author.mention + ", " + commandhandler.unknown(self)))
                logger.info("Admin only, public")
                return

            def tryAdmin(message, args):

                if message.author.id in self.admins:

                    if self.password:
                        if len(args) >= 1 and args[0] == self.password:
                            return True
                    else:
                        return True

                return False

            # Admin command got fired from an unauthorized user
            if tryAdmin(message, args):
                receiver = message.author
            else:
                logger.info("Admin only, other")
                # Just send the unknown command message and jump out
                await self.sendMessage(receiver, (message.author.mention + ", " + common.unknown(self)))
                return


        ### Common command handler ###
        if command == 'info':
            response = common.info(self,message)
            await self.sendMessage(receiver, response)
        elif command == 'faq':
            response = faq.parse(self, args)
            await self.sendMessage(receiver, response)
        ### Node command handler ###
        elif command == 'add':
            response = node.nodeAdd(self,message,args)
            await self.sendMessage(receiver, response)
        elif command == 'update':
            response = node.nodeUpdate(self,message,args)
            await self.sendMessage(receiver, response)
        elif command == 'remove':
            response = node.nodeRemove(self,message,args)
            await self.sendMessage(receiver, response)
        elif command == 'nodes':
            response = node.nodes(self,message)
            await self.sendMessage(receiver, response)
        elif command == 'detail':
            response = node.detail(self,message)
            await self.sendMessage(receiver, response)
        elif command == 'history':
            response = node.history(self,message)
            await self.sendMessage(receiver, response)
        elif command == 'top':
            response = node.top(self,message, args)
            await self.sendMessage(receiver, response)
        elif command == 'balance':

            failed = None
            nodes = []

            dbUser = self.database.getUser(message.author.id)
            userNodes = self.database.getAllNodes(message.author.id)

            # If there is no nodes added yet send an error and return
            if dbUser == None or userNodes == None or len(userNodes) == 0:

                response = messages.markdown("<u><b>Balances<b><u>\n\n",self.messenger)
                response += messages.nodesRequired(self.messenger)

                await self.sendMessage(message.author, response)
                return

            collaterals = list(map(lambda x: x['collateral'],userNodes))
            nodes = self.nodeList.getNodes(collaterals)
            check = self.explorer.balances(nodes)

            # Needed cause the balanceChecks dict also gets modified from other
            # threads.
            self.balanceSem.acquire()

            if check:
                self.balanceChecks[check] = message.author.id
            else:
                logger.info("Balance check failed instant.")
                failed = uuid.uuid4()
                self.balanceChecks[failed] = message.author.id

            # Needed cause the balanceChecks dict also gets modified from other
            # threads.
            self.balanceSem.release()

            if failed:
                self.balancesCB(failed,None)

        elif command == 'lookup':
            response = messages.markdown(node.lookup(self,message, args),self.messenger)
            await self.sendMessage(receiver, response)
        ### User command handler ###
        elif command == 'me':
            response = user.me(self,message)
            await self.sendMessage(receiver, response)
        elif command == 'status':
            response = user.status(self,message, args)
            await self.sendMessage(receiver, response)
        elif command == 'reward':
            response = user.reward(self,message, args)
            await self.sendMessage(receiver, response)
        elif command == 'timeout':
            response = user.timeout(self,message, args)
            await self.sendMessage(receiver, response)
        elif command == 'network':
            response = user.network(self,message, args)
            await self.sendMessage(receiver, response)

        ### Admin command handler ###
        elif command == 'stats':
            response = common.stats(self)
            await self.sendMessage(receiver, response)
        elif command == 'payouts':
            response = common.payouts(self,args[1:])
            await self.sendMessage(receiver, response)
        elif command == 'broadcast':

            response = " ".join(args[1:])

            for dbUser in self.database.getUsers():

                member = self.findMember(dbUser['id'])

                if member:
                    await self.sendMessage(member, response)

        # Help message
        elif command == 'help':
            await self.sendMessage(receiver, messages.help(self.messenger))

        # Could not match any command. Send the unknwon command message.
        else:
            await self.sendMessage(receiver, (message.author.mention + ", " + common.unknown(self)))
Exemplo n.º 5
0
    async def commandHandler(self, message, command, args):

        logger.info("commandHandler - {}, command: {}, args: {}".format(message.author, command, args))

        # Check if the user is already in the databse
        result = commandhandler.checkUser(self, message)

        if result['response']:
            await self.sendMessage(message.author, result['response'])

        if result['added'] and not isinstance(message.author, discord.Member):
            return

        # per default assume the message gets back from where it came
        receiver = message.author

        ####
        # List of available commands
        # Public = 0
        # DM-Only = 1
        # Admin only = 2
        ####
        commands = {
                    # DM Only
                    'subscribe':1,'unsubscribe':1,'add':1,'remove':1,'watchlist':1,
                    # Public
                    'help':0,'open':0,'latest':0,'passing':0,'failing':0, 'detail':0,'ending':0,
                    # Admin commands
                    'stats':2, 'broadcast':2, 'publish':2, 'new':2,
        }

        choices = fuzzy.extract(command,commands.keys(),limit=2)

        if choices[0][1] == choices[1][1] or choices[0][1] < 60:
            logger.debug('Invalid fuzzy result {}'.format(choices))
            command = 'unknown'
        else:
            command = choices[0][0]

        # If the command is DM only
        if command in commands and commands[command] == 1:

            if isinstance(message.author, discord.Member):
             await self.client.send_message(message.channel,\
             message.author.mention + ', the command `{}` is only available in private chat with me!'.format(command))

             if not added:
                 await self.client.send_message(message.author, messages.markdown('<b>Try it here with: {}<b>\n'.format(command), self.messenger))
                 await self.client.send_message(message.author, messages.help(self.messenger))

             return

        else:
            receiver = message.channel

        # If the command is admin only
        if command in commands and commands[command] == 2:

            # Admin command got fired in a public chat
            if isinstance(message.author, discord.Member):
                # Just send the unknown command message and jump out
                await self.sendMessage(receiver, (message.author.mention + ", " + commandhandler.unknown(self)))
                logger.info("Admin only, public")
                return

            def tryAdmin(message, args):
                logger.info("PW {}".format(self.password))
                if message.author.id in self.admins:
                    logger.info("yo")
                    if self.password != None:
                        logger.info("yo1")
                        if len(args) >= 1 and args[0] == self.password:
                            return True
                    else:
                        logger.info("yo2")
                        return True

                return False

            # Admin command got fired from an unauthorized user
            if tryAdmin(message, args):
                receiver = message.author
            else:
                logger.info("Admin only, other")
                # Just send the unknown command message and jump out
                await self.sendMessage(receiver, (message.author.mention + ", " + commandhandler.unknown(self)))
                return

        ### DM Only ###
        if command == 'subscribe':
            response = commandhandler.subscription(self,message,True)
            await self.sendMessage(receiver, response)
        elif command == 'unsubscribe':
            response = commandhandler.subscription(self,message,False)
            await self.sendMessage(receiver, response)
        elif command == 'add':
            response = commandhandler.add(self, message, args)
            await self.sendMessage(receiver, response)
        elif command == 'remove':
            response = commandhandler.remove(self, message, args)
            await self.sendMessage(receiver, response)
        elif command == 'watchlist':
            response = commandhandler.watchlist(self, message)
            await self.sendMessage(receiver, response)
        ### Public ###
        elif command == 'open':
            response = commandhandler.open(self)
            await self.sendMessage(receiver, response)
        elif command == 'latest':
            response = commandhandler.latest(self)
            await self.sendMessage(receiver, response)
        elif command == 'ending':
            response = commandhandler.ending(self)
            await self.sendMessage(receiver, response)
        elif command == 'detail':
            response = commandhandler.detail(self,args)
            await self.sendMessage(receiver, response)
        elif command == 'passing':
            response = commandhandler.passing(self)
            await self.sendMessage(receiver, response)
        elif command == 'failing':
            response = commandhandler.failing(self)
            await self.sendMessage(receiver, response)
        ### Admin command handler ###
        elif command == 'stats':
            response = commandhandler.stats(self)
            await self.sendMessage(receiver, response)
        elif command == 'new':
            response = commandhandler.new(self)
            await self.sendMessage(receiver, response)
        elif command == 'publish':
            result = commandhandler.publish(self, message, args)

            if result['fire']:
                response = self.publishProposal(result['author'], result['proposal'])
            else:
                response = result['message']
            await self.sendMessage(receiver, response)

        elif command == 'start':
            response = commandhandler.stats(self)
            await self.sendMessage(receiver, messages.welcome(self.messenger))
        elif command == 'broadcast':

            response = None

            if self.password:
                response = " ".join(args[1:])
            else:
                response = " ".join(args)

            for dbUser in self.database.getUsers():

                member = self.findMember(dbUser['id'])

                if member:
                    await self.sendMessage(member, response)

        # Help message
        elif command == 'help':
            await self.sendMessage(receiver, messages.help(self.messenger))

        # Could not match any command. Send the unknwon command message.
        else:
            await self.sendMessage(receiver, (message.author.mention + ", " + commandhandler.unknown(self)))