Closed ghost closed 10 years ago
Here's a terrible hacky, lazy quick 'fix':
if to == conn.nickname:
reply = self.brain.reply(text)
replylen = len(reply)
if replylen > 470:
conn.privmsg(event.target, "%s: %s" % (user, reply[:470]))
conn.privmsg(event.target, "%s: %s" % (user, reply[470:]))
else:
conn.privmsg(event.target, "%s: %s" % (user, reply))
Just breaks up reply into two chunks. Ugly and won't fix if you go way over. A better solution would be some kind of loop which I'll work on later. 470 seems to work for me. I'm not sure if the entire irc message needs to be less than 512 bytes including the actual "privmsg" part that sends the text to the channel... or just the actual string.
This at least prevents the crash. I'll work on something better later unless you or someone else has something in mind.
Yeah, that can be Unicode and also there's some IRC protocol stuff that takes a few bytes. 512 is the maximum size for the whole IRC message. I settled on 425 for a different bot.
There's decent whitespace-based wrapping in the standard library. Want to try something like this?
for line in textwrap.wrap(reply, 425):
conn.privmsg(event.target, "%s: %s" % (user, line))
looks good. added:
import textwrap
and made the conn.nickname if-statement read as follows:
if to == conn.nickname:
reply = self.brain.reply(text)
for line in textwrap.wrap(reply, 425):
conn.privmsg(event.target, "%s: %s" % (user, line))
seems to work great. better wrapping than what I was trying :)
Any way to split up messages for sending to IRC to avoid this error?