Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling split messages #237

Open
Dusty-Meg opened this issue Dec 15, 2024 · 1 comment
Open

Handling split messages #237

Dusty-Meg opened this issue Dec 15, 2024 · 1 comment

Comments

@Dusty-Meg
Copy link

Had a bot working for years now, generally working really well.
Occassionally I get long private messages, which get split into 2 on_privmsg calls.

Is there anyway of joining these together?

Thought about having a buffer which holds the messages, but can see how this could end badly, so wondering if there is anything in the irc package that could assist with this.

@ljluestc
Copy link

import irc.bot
import irc.strings
from irc.client import Event, ServerConnection

class MessageBufferBot(irc.bot.SingleServerIRCBot):
    def __init__(self, server, port, nickname, channel):
        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel
        self.message_buffer = {}

    def on_welcome(self, connection: ServerConnection, event: Event):
        connection.join(self.channel)

    def on_privmsg(self, connection: ServerConnection, event: Event):
        nick = event.source.nick
        message = event.arguments[0]

        # Initialize buffer if it doesn't exist
        if nick not in self.message_buffer:
            self.message_buffer[nick] = ""

        # Append the new message to the buffer
        self.message_buffer[nick] += message

        # Check if the message is complete (e.g., ends with a newline or specific marker)
        if self.is_message_complete(self.message_buffer[nick]):
            # Process the complete message
            self.process_complete_message(nick, self.message_buffer[nick])
            # Clear the buffer for this user
            self.message_buffer[nick] = ""

    def is_message_complete(self, message: str) -> bool:
        # Define your logic to determine if the message is complete
        # For example, check if the message ends with a newline or a specific marker
        return message.endswith('\n') or message.endswith('\r\n')

    def process_complete_message(self, nick: str, message: str):
        # Process the complete message here
        print(f"Complete message from {nick}: {message}")
        # Example: Echo the message back to the user
        self.connection.privmsg(nick, f"You said: {message}")

if __name__ == "__main__":
    server = "irc.example.com"
    port = 6667
    nickname = "YourBotNick"
    channel = "#yourchannel"

    bot = MessageBufferBot(server, port, nickname, channel)
    bot.start()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants