示例#1
0
def start():
    global ioloop, t
    import controller
    from threading import Thread
    from tornado.ioloop import IOLoop
    from src import messages

    messages.starting()

    ioloop = IOLoop.instance()
    t = Thread(target=lambda: ioloop.start())
    t.start()

    messages.welcome()
示例#2
0
def start():
    global ioloop, t
    import controller
    from threading import Thread
    from tornado.ioloop import IOLoop
    from src import messages

    messages.starting()

    ioloop = IOLoop.instance()
    t = Thread(target=lambda: ioloop.start())
    t.start()

    messages.welcome()
示例#3
0
def checkUser(bot, message):
    logger.info("checkUser")

    result = {'response': None, 'added': False}

    userInfo = util.crossMessengerSplit(message)
    userId = userInfo['user'] if 'user' in userInfo else None
    userName = userInfo['name'] if 'name' in userInfo else "Unknown"

    if not bot.database.getUser(userId) and bot.database.addUser(
            userId, userName):
        logger.info("checkUser - new user {}".format(userName))

        result['added'] = True

        if bot.messenger == 'discord':
            result['response'] = messages.welcome(bot.messenger)

    if bot.messenger == 'telegram':
        result['response'] = messages.welcome(bot.messenger)

    return result
示例#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 = 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)))