summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Yu <andrew@andrewyu.org>2022-08-11 21:30:28 +0800
committerAndrew Yu <andrew@andrewyu.org>2022-08-11 21:30:28 +0800
commit341bb39a55bfbd015f9e6fd253b8f5ae19e9e880 (patch)
tree316989b8df38f08994c38e34dcabc87d84ff2f87
parentcbd0046251a3725e6eeede650730930e4448e50f (diff)
downloadirc-mod-bot-341bb39a55bfbd015f9e6fd253b8f5ae19e9e880.tar.gz
irc-mod-bot-341bb39a55bfbd015f9e6fd253b8f5ae19e9e880.zip
Changes in PRIVMSG handling
- A new reply() that auto-checks the target to differ from channels and private messages, and also appends the sender's nick to the front of the message and adds a colon (i.e. ``Andrew: Hi'') when replying in a channel. - Some changes in commands.
-rw-r--r--bot.py43
1 files changed, 25 insertions, 18 deletions
diff --git a/bot.py b/bot.py
index e5d5761..6feabae 100644
--- a/bot.py
+++ b/bot.py
@@ -35,6 +35,7 @@ import socket
import sys
import base64
import re
+import time
from config import SERVER, PORT, NICK, IDENT, GECOS, CHANNELS, PREFIX, LOGIN, PASSWORD, ADMINS
@@ -188,6 +189,18 @@ def send(s: socket.socket, *args: bytes) -> bytes:
print("<", line.decode("utf-8", "surrogateescape"))
return line
+def reply(s: socket.socket, msg: Message, text: bytes) -> bytes:
+ if msg.source is None:
+ raise TypeError("Cannot reply to a message that doesn't have a source")
+ if msg.cmd != b"PRIVMSG":
+ raise TypeError("reply() only accepts incoming PRIVMSG Message's as the msg argument")
+ if msg.args[0].startswith(b"#"):
+ text = (b"%s: " % msg.source.nick) + text
+ reply_to = msg.args[0]
+ else:
+ reply_to = msg.source.nick
+ return send(s, b"PRIVMSG", reply_to, text)
+
IRC_CALLBACK_FUNCTION_TYPE = Callable[
[socket.socket, State, User, dict[bytes, User], dict[bytes, Channel], Message], None
@@ -442,15 +455,13 @@ def handle_privmsg(
channel: Optional[Channel]
if target.startswith(b"#"):
channel = channels[target]
- reply_to = target
if not text.startswith(PREFIX):
return
text = text[len(PREFIX) :]
elif target == me.nick:
channel = None
- reply_to = msg.source.nick
if text.startswith(PREFIX):
- text = text[len(PREFIX) :]
+ text = text[len(PREFIX):]
else:
# A message addressed to a non-channel that isn't the bot itself? Weird!
return
@@ -459,36 +470,32 @@ def handle_privmsg(
chat_command[0] = chat_command[0].upper()
if chat_command[0] == b"DUMP":
- for send_buf in break_up(repr(channels).encode("utf-8"), 400):
- send(s, b"PRIVMSG", reply_to, send_buf)
+ print(repr(channels))
+ print(repr(users))
+ reply(s, msg, b"I dumped my channel and user database to standard output.")
return
elif chat_command[0] == b"OP":
if not channel:
- send(s, b"PRIVMSG", reply_to, b"%s: You may only do this in a channel." % msg.source.nick)
+ reply(s, msg, b"You may only do this in a channel.")
return
elif msg.source not in channel.users:
- send(s, b"PRIVMSG", reply_to, b"%s: You are not on the channel, and you're haxsending messages to it. This isn't nice." % msg.source.nick)
+ reply(s, msg, b"You are not on the channel, and you're haxsending messages to it. This isn't nice.")
return
priv = is_admin(msg.source)
if priv is False:
- send(s, b"PRIVMSG", reply_to, b"%s: Access denied." % msg.source.nick)
+ reply(s, msg, b"Access denied.")
return
elif priv is None:
- send(s, b"PRIVMSG", reply_to, b"%s: I don't know your ident or host, so I can't check your permissions. Something is wrong." % msg.source.nick)
+ reply(s, msg, b"I don't know your ident or host, so I can't check your permissions. Something is wrong.")
return
elif msg.source in channel.ops:
- send(s, b"PRIVMSG", reply_to, b"%s: You are already a channel operator." % msg.source.nick)
+ reply(s, msg, b"You are already a channel operator.")
return
else:
- send(s, b"MODE", reply_to, b"+o", msg.source.nick)
- send(s, b"PRIVMSG", reply_to, b"%s: I tried to make you a channel operator." % msg.source.nick)
+ send(s, b"MODE", channel.name, b"+o", msg.source.nick)
+ reply(s, msg, b"I tried to make you a channel operator.")
else:
- send(
- s,
- b"PRIVMSG",
- reply_to,
- b"%s: Unknown command: %s" % (msg.source.nick, chat_command[0]),
- )
+ reply(s, msg, b"Unknown command: %s" % chat_command[0])
def handle_incoming_message(