Пример #1
0
async def on_message(message: Message):
    # If the message is by a bot thats not irc then ignore it
    if message.author.bot and message.author.id != CONFIG['UWCS_DISCORD_BRIDGE_BOT_ID']:
        return

    user = db_session.query(User).filter(User.user_uid == message.author.id).first()
    if not user:
        user = User(user_uid=message.author.id, username=str(message.author))
        db_session.add(user)
    else:
        user.last_seen = message.created_at
    # Commit the session so the user is available now
    db_session.commit()

    # Only log messages that were in a public channel
    if isinstance(message.channel, GuildChannel):
        # Log the message to the database
        logged_message = LoggedMessage(message_uid=message.id, message_content=message.clean_content, author=user.id,
                                       created_at=message.created_at, channel_name=message.channel.name)
        db_session.add(logged_message)
        db_session.commit()

        # Get all specified command prefixes for the bot
        command_prefixes = bot.command_prefix(bot, message)
        # Only process karma if the message was not a command (ie did not start with a command prefix)
        if True not in [message.content.startswith(prefix) for prefix in command_prefixes]:
            reply = process_karma(message, logged_message.id, db_session, CONFIG['KARMA_TIMEOUT'])
            if reply:
                await message.channel.send(reply)

    await bot.process_commands(message)
Пример #2
0
    def outgoing(self, message):
        '''
        This will be called when messages go out.
        '''
        
        msg = LoggedMessage.create_from_message(message)
        msg.direction = LoggedMessage.DIRECTION_OUTGOING

        # If an _outgoing_ message has a logger_id it is actually the logger_id
        # of the incoming message (because Message.respond does a copy.copy
        # on the message object). This is actually very convenient because
        # it allows us to match an outgoing message to the incoming message
        # that solicited it (assuming Message.respond) was used.
        if hasattr(message, 'logger_id'):
            try:
                orig_msg = LoggedMessage.incoming.get(pk=message.logger_id)
            except LoggedMessage.DoesNotExist:
                # Really no reason for this to ever happen, but if it does
                # we'll just silently continue, but we won't be able
                # to set the response_to field of this LoggedMessage
                pass
            else:
                # Set the response_to foreign key of this logged outgoing
                # message to the incoming message that it was copied from.
                msg.response_to = orig_msg

        msg.save()

        # Watermark the message object with the LoggedMessage pk.
        message.logger_id = msg.pk

        # Print message if debug
        self.debug(msg)
Пример #3
0
    def outgoing(self, message):
        '''
        This will be called when messages go out.
        '''

        msg = LoggedMessage.create_from_message(message)
        msg.direction = LoggedMessage.DIRECTION_OUTGOING

        # If an _outgoing_ message has a logger_id it is actually the logger_id
        # of the incoming message (because Message.respond does a copy.copy
        # on the message object). This is actually very convenient because
        # it allows us to match an outgoing message to the incoming message
        # that solicited it (assuming Message.respond) was used.
        if hasattr(message, 'logger_id'):
            try:
                orig_msg = LoggedMessage.incoming.get(pk=message.logger_id)
            except LoggedMessage.DoesNotExist:
                # Really no reason for this to ever happen, but if it does
                # we'll just silently continue, but we won't be able
                # to set the response_to field of this LoggedMessage
                pass
            else:
                # Set the response_to foreign key of this logged outgoing
                # message to the incoming message that it was copied from.
                msg.response_to = orig_msg

        msg.save()

        # Watermark the message object with the LoggedMessage pk.
        message.logger_id = msg.pk

        # Print message if debug
        self.debug(msg)
Пример #4
0
async def on_message(message: Message):
    # If the message is by a bot thats not irc then ignore it
    if message.author.bot and message.author.id != CONFIG[
            "UWCS_DISCORD_BRIDGE_BOT_ID"]:
        return

    user = db_session.query(User).filter(
        User.user_uid == message.author.id).first()
    if not user:
        user = User(user_uid=message.author.id, username=str(message.author))
        db_session.add(user)
    else:
        user.last_seen = message.created_at
    # Commit the session so the user is available now
    try:
        db_session.commit()
    except (ScalarListException, SQLAlchemyError):
        db_session.rollback()
        # Something very wrong, but not way to reliably recover so abort
        return

    # Only log messages that were in a public channel
    if isinstance(message.channel, GuildChannel):
        # Log the message to the database
        logged_message = LoggedMessage(
            message_uid=message.id,
            message_content=message.clean_content,
            author=user.id,
            created_at=message.created_at,
            channel_name=message.channel.name,
        )
        db_session.add(logged_message)
        try:
            db_session.commit()
        except (ScalarListException, SQLAlchemyError):
            db_session.rollback()
            return

        # Get all specified command prefixes for the bot
        command_prefixes = bot.command_prefix(bot, message)
        # Only process karma if the message was not a command (ie did not start with a command prefix)
        if True not in [
                message.content.startswith(prefix)
                for prefix in command_prefixes
        ]:
            reply = process_karma(message, logged_message.id, db_session,
                                  CONFIG["KARMA_TIMEOUT"])
            if reply:
                await message.channel.send(reply)

    # allow irc users to use commands by altering content to remove the nick before sending for command processing
    # note that clean_content is *not* altered and everything relies on this fact for it to work without having to go back and lookup the message in the db
    # if message.content.startswith("**<"): <-- FOR TESTING
    if message.author.id == CONFIG["UWCS_DISCORD_BRIDGE_BOT_ID"]:
        # Search for first "> " and strip the message from there (Since irc nicks cant have <, > in them
        idx = message.content.find(">** ")
        idx += 4
        message.content = message.content[idx:]

    await bot.process_commands(message)
Пример #5
0
    async def on_message(self, message: Message):
        # If the message is by a bot that's not irc then ignore it
        if message.author.bot and not user_is_irc_bot(message):
            return

        user = (db_session.query(User).filter(
            User.user_uid == message.author.id).one_or_none())
        if not user:
            user = User(user_uid=message.author.id,
                        username=str(message.author))
            db_session.add(user)
        else:
            user.last_seen = message.created_at
        # Commit the session so the user is available now
        try:
            db_session.commit()
        except (ScalarListException, SQLAlchemyError) as e:
            db_session.rollback()
            logging.error(e)
            # Something very wrong, but not way to reliably recover so abort
            return

        # Only log messages that were in a public channel
        if isinstance(message.channel, GuildChannel):
            # Log the message to the database
            logged_message = LoggedMessage(
                message_uid=message.id,
                message_content=message.clean_content,
                author=user.id,
                created_at=message.created_at,
                channel_name=message.channel.name,
            )
            db_session.add(logged_message)
            try:
                db_session.commit()
            except (ScalarListException, SQLAlchemyError) as e:
                db_session.rollback()
                logging.error(e)
                return

            # KARMA

            # Get all specified command prefixes for the bot
            command_prefixes = self.bot.command_prefix(self.bot, message)
            # Only process karma if the message was not a command (ie did not start with a command prefix)
            if not any(
                    message.content.startswith(prefix)
                    for prefix in command_prefixes):
                reply = process_karma(message, logged_message.id, db_session,
                                      CONFIG.KARMA_TIMEOUT)
                if reply:
                    await message.channel.send(reply)
Пример #6
0
    def handle(self, message):
        '''
        This will be called when messages come in. We don't return return
        anything, so as far as rapidsms is concerned, we haven't handled it
        so it just keeps passing it along to the latter apps.
        '''
        msg = LoggedMessage.create_from_message(message)
        msg.direction = LoggedMessage.DIRECTION_INCOMING
        msg.save()

        # Watermark the message object with the LoggedMessage pk.
        message.logger_id = msg.pk

        # Print message if debug
        self.debug(msg)
Пример #7
0
    def handle(self, message):
        '''
        This will be called when messages come in. We don't return return
        anything, so as far as rapidsms is concerned, we haven't handled it
        so it just keeps passing it along to the latter apps.
        '''
        msg = LoggedMessage.create_from_message(message)
        msg.direction = LoggedMessage.DIRECTION_INCOMING
        msg.save()

        # Watermark the message object with the LoggedMessage pk.
        message.logger_id = msg.pk

        # Print message if debug
        self.debug(msg)