python-xmpp-bot

A simple python-based XMPP bot.
git clone https://git.sr.ht/~jbauer/python-xmpp-bot
Log | Files | Refs | README | LICENSE

commit f880208a3e96795668d1b5aa07b24935024177b7
parent 6fc6746cfc67d98a120d59eb5d058a67702ff6e8
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Sun, 29 Nov 2020 04:39:48 -0500

Split bot.py into run.py, bot.py

bot.py now contains the class and class method definitions for the bot,
run.py now just handles starting the bot.

run.py has also been cleaned up and now has a main() function.

Diffstat:
MREADME.md | 3---
Mbot.py | 44--------------------------------------------
Arun.py | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 47 deletions(-)

diff --git a/README.md b/README.md @@ -57,6 +57,3 @@ Here is a list of features I plan to add: * Expand bot documentation: * Comment class functions. * Add to README a detailed overview of the commands available. -* Separate bot class into its own `.py` files. Class is getting quite large, can - split into Bot class and Commands class where Bot inherits from Commands. - This separates the files so they're not as unwieldy. diff --git a/bot.py b/bot.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 - # Copyright (C) 2020 Jake Bauer <jbauer@paritybit.ca> # # This program is free software: you can redistribute it and/or modify @@ -15,34 +13,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. -# For basic functionality import xmpp import sys -import signal import json import re - -# For fun and features import random import requests from datetime import datetime from pytz import timezone -# Used for restarting the bot -restarting = False -invoker = "" - -def signal_handler(sig, frame): - try: - sys.stderr.write("\nSIGINT caught, exiting...\n") - bot.announce("Bot shutting down.") - bot.jabber.disconnect() - sys.exit(0) - except: - sys.exit(0) - -signal.signal(signal.SIGINT, signal_handler) - class Bot: def __init__(self): self.jabber = None @@ -290,26 +269,3 @@ class Bot: message = "User was not an admin or no JID given." self.send_message(recipient, message, typ) - -def start_bot(): - global bot - bot = Bot() - if not bot.xmpp_connect(): - sys.stderr.write("Could not connect to server.\n") - sys.exit(1) - bot.announce("Bot online!") - - -start_bot() -while True: - if restarting: - bot.jabber.disconnect() - start_bot() - bot.send_message(invoker, "Bot restarted!", typ="chat") - restarting = False - try: - if not bot.jabber.isConnected(): - bot.jabber.reconnectAndReauth() - bot.jabber.Process(1) - except Exception as e: - sys.stderr.write(str(e) + "\n") diff --git a/run.py b/run.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 Jake Bauer <jbauer@paritybit.ca> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +import sys +import signal +from bot import Bot + +def signal_handler(sig, frame): + try: + sys.stderr.write("\nSIGINT caught, exiting...\n") + bot.announce("Bot shutting down.") + bot.jabber.disconnect() + sys.exit(0) + except: + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + + +def start_bot(): + bot = Bot() + if not bot.xmpp_connect(): + sys.stderr.write("Could not connect to server.\n") + sys.exit(1) + bot.announce("Bot online!") + return bot + + +def main(): + restarting = False # Used for restarting the bot + invoker = "" # Used for restarting the bot + + bot = start_bot() + + while True: + if restarting: + bot.jabber.disconnect() + start_bot() + bot.send_message(invoker, "Bot restarted!", typ="chat") + restarting = False + try: + if not bot.jabber.isConnected(): + bot.jabber.reconnectAndReauth() + bot.jabber.Process(1) + except Exception as e: + sys.stderr.write(str(e) + "\n") + + +if __name__ == '__main__': + main()