Skip to content

Instantly share code, notes, and snippets.

@dece
Last active May 27, 2021 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dece/61271ecc765568567e0d0b450b82cebe to your computer and use it in GitHub Desktop.
Save dece/61271ecc765568567e0d0b450b82cebe to your computer and use it in GitHub Desktop.
Announce to every joining users that you moved to greener pastures (you need the "irc" Python3 package to run it). Beware, I got a server klined after using it.
import argparse
try:
import irc.client
except ImportError:
exit("You need the IRC package to run this bot: pip3 install irc")
def main():
argparser = argparse.ArgumentParser(description="\"pick up that chan, leetizen\"")
argparser.add_argument(
"old_channel",
help="Channel to join and monitor",
)
argparser.add_argument(
"nick",
help="Bot name"
)
argparser.add_argument(
"new_channel",
nargs="?",
help=(
"Channel name on the new server to advertise"
" (defaults to old channel name)"
),
)
argparser.add_argument(
"--new-server",
default="irc.libera.chat",
help="New server hostname to advertise (defaults to irc.libera.chat)"
)
argparser.add_argument(
"--new-port",
type=int,
default=6697,
help="New server port to advertise (defaults to 6697)"
)
args = argparser.parse_args()
bot = Leebot(args.old_channel, args.nick, args.new_channel or args.old_channel,
args.new_server, args.new_port)
bot.run()
class Leebot(irc.client.SimpleIRCClient):
MSG = (
"Hi {target}! This channel has moved to {ns} (port {np}), under the "
"name {nn}"
)
def __init__(self, old_chan, nick, new_chan, new_server, new_port):
super().__init__()
self.old_chan = old_chan
self.nick = nick
self.new_chan = new_chan
self.new_server = new_server
self.new_port = new_port
def run(self):
self.connect("irc.freenode.net", 6667, self.nick)
try:
self.start()
except KeyboardInterrupt:
print("Bye!")
def on_welcome(self, connection, event):
connection.join(self.old_chan)
def on_join(self, connection, event):
joined = event.source.nick
if joined != self.nick:
self.announce(joined, event.target)
def announce(self, target, channel):
message = Leebot.MSG.format(
target=target,
ns=self.new_server,
np=self.new_port,
nn=self.new_chan
)
self.connection.privmsg(channel, message)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment