paritybot

A fun IRC bot.
git clone https://git.sr.ht/~jbauer/paritybot
Log | Files | Refs | README | LICENSE

commit bed9688c38d9c0106c770efe6a5f04e4ecf1b2ca
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Sat, 21 Nov 2020 02:26:07 -0500

Initial commit

Diffstat:
Abot.py | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 107 insertions(+), 0 deletions(-)

diff --git a/bot.py b/bot.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +# For basic functionality +import socket +import ssl + +# For fun and features +import random +import requests +from datetime import datetime +from pytz import timezone + +VERSION = "v0.4.0" +AUTHOR = "Jake Bauer" +server = "irc.paritybit.ca" +port = 6697 +channels = ["#test"] +botnick = "testbot" +quotesfile = "quotes.txt" + +s = None +ircsock = None + +def send_msg(msg): + ircsock.sendall(msg.encode('utf-8')) + + +def get_channel(ircmsg): + return ircmsg.split(' ')[2] + + +def init(): + global s + global ircsock + # Set up the socket + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((server, port)) + ircsock = ssl.wrap_socket(s) + + # Perform the initial connection + message = "USER " + " " + botnick + " 0 * " + botnick +"\n" + send_msg(message) + message = "NICK "+ botnick +"\n" + send_msg(message) + + # Join the configured channels + for channel in channels: + message = "JOIN "+ channel +"\n" + send_msg(message) + +def main(): + while 1: + ircmsg = ircsock.recv(2048).decode('utf-8') + ircmsg = ircmsg.strip('\n\r') + print(ircmsg) + + if ircmsg.find(",tbhelp") != -1: + message = "PRIVMSG "+ get_channel(ircmsg) +" :Available commands: ,tbhelp ,tbversion ,tbwhereami ,tbquote ,tbtime ,tbdiceroll ,tbweather\n" + send_msg(message) + + elif ircmsg.find(":,tbwhereami") != -1: + message = "PRIVMSG "+ get_channel(ircmsg) +" :You are currently in " + get_channel(ircmsg) + ".\n" + send_msg(message) + + elif ircmsg.find(",tbquote") != -1: + with open(quotesfile, "r") as f: + lines = f.read().splitlines() + selectedLine = random.choice(lines) + message = "PRIVMSG " + get_channel(ircmsg) + " :" + selectedLine + "\n" + send_msg(message) + + elif ircmsg.find(",tbversion") != -1: + message = "PRIVMSG " + get_channel(ircmsg) + " :" + botnick + "version " + VERSION + " by " + AUTHOR + ".\n" + send_msg(message) + + elif ircmsg.find(",tbtime") != -1: + try: + message = "PRIVMSG " + get_channel(ircmsg) + " :" + datetime.now(timezone(ircmsg.split(' ')[4])).strftime('%Y-%m-%d %H:%M:%S') + "\n" + except Exception as e: + message = "PRIVMSG " + get_channel(ircmsg) + " :Please specify a valid timezone (e.g. ,tbtime America/Toronto).\n" + send_msg(message) + + elif ircmsg.find(",tbdiceroll") != -1: + try: + message = "PRIVMSG " + get_channel(ircmsg) + " :" + str(random.randint(1, int(ircmsg.split(' ')[4]))) + "\n" + except Exception as e: + message = "PRIVMSG " + get_channel(ircmsg) + " :Please specify a maximum number (e.g. ,tbdiceroll 20).\n" + send_msg(message) + + elif ircmsg.find(",tbweather") != -1: + try: + city = ircmsg.split(' ')[4] + url = "https://wttr.in/" + city + "?format=3" + response = requests.get(url) + message = "PRIVMSG " + get_channel(ircmsg) + " :" + response.text.strip('\n\r') + "\n" + except Exception as e: + print(e) + message = "PRIVMSG " + get_channel(ircmsg) + " :Please specify a city (e.g. ,tbweather Toronto).\n" + send_msg(message) + + elif ircmsg.find("PING ") != -1: + print("Responding to PING.") + message = "PONG :" + botnick + "\n" + send_msg(message) + +init() +main()