Esempio n. 1
0
    def info(self, bot, update):

        response = common.info( self, update )
        self.sendMessage(update.message.chat_id, response)
Esempio n. 2
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)))