Migrated config and commands to ConfigParser

This commit is contained in:
Siina Mashek 2022-04-24 04:47:08 +03:00
parent f792f3b862
commit e1ff594f79
9 changed files with 66 additions and 39 deletions

2
.gitignore vendored
View file

@ -2,7 +2,7 @@
*.kate-swp
*.log
data/
config/owncast.json
config.ini
# ---> Python
# Byte-compiled / optimized / DLL files

View file

@ -1 +0,0 @@
{}

View file

@ -1,10 +1,10 @@
import json
from configparser import ConfigParser
import sys
from ameliabot.logger import ColorizedArgsFormatter, BraceFormatStyleFormatter
from ameliabot.logger import logging
__version__ = "0.0.1"
__version__ = "0.1.0"
# Set up logging
root_logger = logging.getLogger()
@ -25,20 +25,40 @@ file_format = "%(asctime)s - %(name)s (%(lineno)s) - %(levelname)-8s - %(threadN
file_handler.setFormatter(BraceFormatStyleFormatter(file_format))
root_logger.addHandler(file_handler)
# Get basic owncast bot config
# Load bot config
config = ConfigParser()
try:
with open("config/owncast.json", "r") as f:
config = json.load(f)
config.read("config.ini")
logging.info("Configuration loaded")
except FileNotFoundError:
logging.error("Configuration file not found.")
logging.error("Please see README.md for more information.")
config["DEFAULT"] = {"BotName": "ameliabot", "BotOwner": "Some Name"}
config["owncast"] = {
"ServerURL": "https://yourowncloud.server",
"AccessToken": "Someaccesstoken23-209523dfsd"}
with open("config.ini", "w") as f:
config.write(f)
logging.error("Configuration file not found so minimal one generated.")
logging.error("Please edit config.ini and try again.")
logging.error("See config.example.ini for full options")
raise SystemExit
with open("commands.json", "r") as f:
commands = json.load(f)
cmd_file = ConfigParser()
try:
cmd_file.read("commands.ini")
commands = cmd_file["commands"]
if config["quotes"]["Enabled"]:
# Perhaps configparser handles this and I haven't figured it out
for cmd in cmd_file["commands.quote"]:
commands[cmd] = cmd_file["commands.quote"][cmd]
logging.info("Commands loaded")
with open("alias.json", "r") as f:
aliases = json.load(f)
aliases = cmd_file["aliases"]
logging.info("Aliases loaded")
except FileNotFoundError:
cmd_file["commands"] = {
"!version": "I am running ameliabot {bot_version}"}
cmd_file["aliases"] = {"!bot": "!version"}
with open("commands.ini", "w") as f:
cmd_file.write(f)
logging.error("Missing commands.ini! Generated basic file.")
raise SystemExit

View file

@ -6,16 +6,16 @@ from ameliabot.logger import logging
from flask import request
# Make quote system and dependencies optional
if config["quote_enabled"]:
if config["quotes"]["Enabled"]:
from ameliabot.quote import Quote
quote = Quote()
# prepare the header for the bot posts
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer {}".format(config["access_token"])
headers["Authorization"] = "Bearer {}".format(config["owncast"]["AccessToken"])
post_url = "%s/api/integrations/chat/send" % config["owncast_server"]
post_url = "%s/api/integrations/chat/send" % config["owncast"]["ServerURL"]
def parse_webhook():
@ -79,7 +79,7 @@ def process_chat(data):
# This is so bad, it runs everything every command :pikajoy:
placeholders = {
"sender": sender,
"streamer": config["streamer_name"],
"streamer": config["DEFAULT"]["BotOwner"],
"target": first_parameter,
"random": str(random.randrange(1, 100, 1)),
"commands": ", ".join(list(commands.keys())),

5
bot.py
View file

@ -4,11 +4,12 @@ from ameliabot import owncast, __version__
from ameliabot.logger import logging
logging.info("Loaded %s v%s" % (owncast.config["bot_name"], __version__))
logging.info("Loaded %s, running ameliabot v%s)" % (
owncast.config["DEFAULT"]["BotName"], __version__))
# the url of the Owncast API for bot posts
owncast_url = "{}/api/integrations/chat/send".format(
owncast.config["owncast_server"])
owncast.config["owncast"]["ServerURL"])
app = Flask(__name__)

16
commands.ini Normal file
View file

@ -0,0 +1,16 @@
[commands]
!help = Commands {command}
!backseat = **No backseating** unless {botowner} specifically asks for help
!hydrate = [HYDRATE] {sender} wants {botowner} to take a drink!
!stretch = [STRETCH] {sender} reminds {botowner} to stretch!
!save = {sender} is reminding {botowner} to _**SAVE NOW**_!
!slap = *slaps {target} with a large trout!*
!version = I am running ameliabot {bot_version}
[commands.quote]
!quote = {quote}
!addquote = {quote_parameters}
[aliases]
help = commands
bot = version

View file

@ -1,12 +0,0 @@
{
"!help": "Commands: {commands}",
"!backseat": "**No backseating** unless I specifically ask for help.",
"!hydrate": "[HYDRATE] {sender} wants {streamer} to take a drink!",
"!stretch": "[STRETCH] {sender} reminds {streamer} to stretch dat body.",
"!save": "SAVE! **NOW!!!**",
"!love": "There is **{random}% love** between {sender} and {target} <3",
"!slap": "*slaps {target} with a large trout!*",
"!bot": "I am currently running **ameliabot {bot_version}**",
"!quote": "{quote}",
"!addquote": "{quote_parameters}"
}

10
config.example.ini Normal file
View file

@ -0,0 +1,10 @@
[DEFAULT]
BotName = ameliabot
BotOwner = Some Name
[owncast]
ServerURL = https://yourowncloud.server
AccessToken = Someaccesstoken23-209523dfsd
[quotes]
Enabled = False

View file

@ -1,7 +0,0 @@
{
"owncast_server": "localhost",
"access_token": "",
"bot_name": "ameliabot",
"streamer_name": "owncast streamer",
"quote_enabled": false,
}