paritybot

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

commit 4c8e6eea4d32d12aab30feff1c1a3efeba467325
parent f27cfc5ff906ef92bd82aa984994b3410538c7a8
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Sat, 21 Nov 2020 02:39:50 -0500

Improvements to the bot, cleanups to the code

Diffstat:
Mbot.py | 19++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/bot.py b/bot.py @@ -25,7 +25,7 @@ import requests from datetime import datetime from pytz import timezone -VERSION = "v0.4.0" +VERSION = "v0.4.2" AUTHOR = "Jake Bauer" server = "irc.paritybit.ca" port = 6697 @@ -33,7 +33,6 @@ channels = ["#test"] botnick = "testbot" quotesfile = "quotes.txt" -s = None ircsock = None def send_msg(msg): @@ -45,7 +44,6 @@ def get_channel(ircmsg): def init(): - global s global ircsock # Set up the socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -69,47 +67,46 @@ def main(): ircmsg = ircmsg.strip('\n\r') print(ircmsg) - if ircmsg.find(",tbhelp") != -1: + 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: + 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: + 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: + elif ircmsg.find(":,tbversion ") != -1: message = "PRIVMSG " + get_channel(ircmsg) + " :" + botnick + "version " + VERSION + " by " + AUTHOR + ".\n" send_msg(message) - elif ircmsg.find(",tbtime") != -1: + 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: + 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: + 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)