def on_message(self, conn, event): source = irc.client.NickMask(event.source) nick = source.nick.lower() tags = dict((i['key'], i['value']) for i in event.tags) metadata = { 'usercolor': tags.get('color'), 'emotes': tags.get('emotes'), 'display-name': tags.get('display-name') or nick, 'specialuser': set(), } if int(tags.get('subscriber', 0)): metadata['specialuser'].add('subscriber') if int(tags.get('turbo', 0)): metadata['specialuser'].add('turbo') if tags.get('user-type'): metadata['specialuser'].add(tags.get('user-type')) if self.is_mod(event): metadata['specialuser'].add('mod') log.debug("Message metadata: %r", metadata) chatlog.log_chat(event, metadata) if not hasattr(conn.privmsg, "is_throttled"): conn.privmsg = utils.twitch_throttle()(conn.privmsg) if not hasattr(conn.privmsg, "is_logged"): conn.privmsg = self.log_outgoing(conn.privmsg) source = irc.client.NickMask(event.source) # If the message was sent to a channel, respond in the channel # If it was sent via PM, respond via PM if irc.client.is_channel(event.target): respond_to = event.target else: respond_to = source.nick if (nick == config['notifyuser']): self.on_notification(conn, event, respond_to) elif self.check_spam(conn, event, event.arguments[0]): return else: self.check_subscriber(conn, nick, metadata) if self.access == "mod" and not self.is_mod(event): return if self.access == "sub" and not self.is_mod( event) and not self.is_sub(event): return command_match = self.re_botcommand.match(event.arguments[0]) if command_match: command = command_match.group(command_match.lastindex) log.info("Command from %s: %s " % (source.nick, command)) proc, end = self.command_groups[command_match.lastindex] params = command_match.groups()[command_match.lastindex:end] proc(self, conn, event, respond_to, *params)
def on_message(self, conn, event): source = irc.client.NickMask(event.source) nick = source.nick.lower() tags = dict((i["key"], i["value"]) for i in event.tags) metadata = { "usercolor": tags.get("color"), "emotes": tags.get("emotes"), "display-name": tags.get("display-name") or nick, "specialuser": set(), } if int(tags.get("subscriber", 0)): metadata["specialuser"].add("subscriber") if int(tags.get("turbo", 0)): metadata["specialuser"].add("turbo") if tags.get("user-type"): metadata["specialuser"].add(tags.get("user-type")) if self.is_mod(event): metadata["specialuser"].add("mod") log.debug("Message metadata: %r", metadata) chatlog.log_chat(event, metadata) if not hasattr(conn.privmsg, "is_throttled"): conn.privmsg = utils.twitch_throttle()(conn.privmsg) if not hasattr(conn.privmsg, "is_logged"): conn.privmsg = self.log_outgoing(conn.privmsg) source = irc.client.NickMask(event.source) # If the message was sent to a channel, respond in the channel # If it was sent via PM, respond via PM if irc.client.is_channel(event.target): respond_to = event.target else: respond_to = source.nick if nick == config["notifyuser"]: self.on_notification(conn, event, respond_to) elif self.check_spam(conn, event, event.arguments[0]): return else: self.check_subscriber(conn, nick, metadata) if self.access == "mod" and not self.is_mod(event): return if self.access == "sub" and not self.is_mod(event) and not self.is_sub(event): return command_match = self.re_botcommand.match(event.arguments[0]) if command_match: command = command_match.group(command_match.lastindex) log.info("Command from %s: %s " % (source.nick, command)) proc, end = self.command_groups[command_match.lastindex] params = command_match.groups()[command_match.lastindex : end] proc(self, conn, event, respond_to, *params)
def check_privmsg_wrapper(self, conn): """ Install a wrapper around privmsg that handles: * Throttle messages sent so we don't get banned by Twitch * Turn private messages into Twitch whispers * Log public messages in the chat log """ if hasattr(conn.privmsg, "is_wrapped"): return original_privmsg = utils.twitch_throttle()(conn.privmsg) @functools.wraps(original_privmsg) def new_privmsg(target, text): if irc.client.is_channel(target): username = config["username"] chatlog.log_chat(irc.client.Event("pubmsg", username, target, [text]), SELF_METADATA) original_privmsg(target, text) elif self.whisperconn: self.whisperconn.whisper(target, text) else: log.debug("Not sending private message to %s: %s", target, text) new_privmsg.is_wrapped = True conn.privmsg = new_privmsg