Migrated config and commands to ConfigParser
This commit is contained in:
parent
f792f3b862
commit
e1ff594f79
|
@ -2,7 +2,7 @@
|
||||||
*.kate-swp
|
*.kate-swp
|
||||||
*.log
|
*.log
|
||||||
data/
|
data/
|
||||||
config/owncast.json
|
config.ini
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
{}
|
|
|
@ -1,10 +1,10 @@
|
||||||
import json
|
from configparser import ConfigParser
|
||||||
import sys
|
import sys
|
||||||
from ameliabot.logger import ColorizedArgsFormatter, BraceFormatStyleFormatter
|
from ameliabot.logger import ColorizedArgsFormatter, BraceFormatStyleFormatter
|
||||||
from ameliabot.logger import logging
|
from ameliabot.logger import logging
|
||||||
|
|
||||||
|
|
||||||
__version__ = "0.0.1"
|
__version__ = "0.1.0"
|
||||||
|
|
||||||
# Set up logging
|
# Set up logging
|
||||||
root_logger = logging.getLogger()
|
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))
|
file_handler.setFormatter(BraceFormatStyleFormatter(file_format))
|
||||||
root_logger.addHandler(file_handler)
|
root_logger.addHandler(file_handler)
|
||||||
|
|
||||||
# Get basic owncast bot config
|
# Load bot config
|
||||||
|
config = ConfigParser()
|
||||||
try:
|
try:
|
||||||
with open("config/owncast.json", "r") as f:
|
config.read("config.ini")
|
||||||
config = json.load(f)
|
logging.info("Configuration loaded")
|
||||||
logging.info("Configuration loaded")
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
logging.error("Configuration file not found.")
|
config["DEFAULT"] = {"BotName": "ameliabot", "BotOwner": "Some Name"}
|
||||||
logging.error("Please see README.md for more information.")
|
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
|
raise SystemExit
|
||||||
|
|
||||||
with open("commands.json", "r") as f:
|
cmd_file = ConfigParser()
|
||||||
commands = json.load(f)
|
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")
|
logging.info("Commands loaded")
|
||||||
|
|
||||||
with open("alias.json", "r") as f:
|
aliases = cmd_file["aliases"]
|
||||||
aliases = json.load(f)
|
|
||||||
logging.info("Aliases loaded")
|
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
|
||||||
|
|
|
@ -6,16 +6,16 @@ from ameliabot.logger import logging
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
# Make quote system and dependencies optional
|
# Make quote system and dependencies optional
|
||||||
if config["quote_enabled"]:
|
if config["quotes"]["Enabled"]:
|
||||||
from ameliabot.quote import Quote
|
from ameliabot.quote import Quote
|
||||||
quote = Quote()
|
quote = Quote()
|
||||||
|
|
||||||
# prepare the header for the bot posts
|
# prepare the header for the bot posts
|
||||||
headers = CaseInsensitiveDict()
|
headers = CaseInsensitiveDict()
|
||||||
headers["Content-Type"] = "application/json"
|
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():
|
def parse_webhook():
|
||||||
|
@ -79,7 +79,7 @@ def process_chat(data):
|
||||||
# This is so bad, it runs everything every command :pikajoy:
|
# This is so bad, it runs everything every command :pikajoy:
|
||||||
placeholders = {
|
placeholders = {
|
||||||
"sender": sender,
|
"sender": sender,
|
||||||
"streamer": config["streamer_name"],
|
"streamer": config["DEFAULT"]["BotOwner"],
|
||||||
"target": first_parameter,
|
"target": first_parameter,
|
||||||
"random": str(random.randrange(1, 100, 1)),
|
"random": str(random.randrange(1, 100, 1)),
|
||||||
"commands": ", ".join(list(commands.keys())),
|
"commands": ", ".join(list(commands.keys())),
|
||||||
|
|
5
bot.py
5
bot.py
|
@ -4,11 +4,12 @@ from ameliabot import owncast, __version__
|
||||||
from ameliabot.logger import logging
|
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
|
# the url of the Owncast API for bot posts
|
||||||
owncast_url = "{}/api/integrations/chat/send".format(
|
owncast_url = "{}/api/integrations/chat/send".format(
|
||||||
owncast.config["owncast_server"])
|
owncast.config["owncast"]["ServerURL"])
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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}"
|
|
||||||
}
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
[DEFAULT]
|
||||||
|
BotName = ameliabot
|
||||||
|
BotOwner = Some Name
|
||||||
|
|
||||||
|
[owncast]
|
||||||
|
ServerURL = https://yourowncloud.server
|
||||||
|
AccessToken = Someaccesstoken23-209523dfsd
|
||||||
|
|
||||||
|
[quotes]
|
||||||
|
Enabled = False
|
|
@ -1,7 +0,0 @@
|
||||||
{
|
|
||||||
"owncast_server": "localhost",
|
|
||||||
"access_token": "",
|
|
||||||
"bot_name": "ameliabot",
|
|
||||||
"streamer_name": "owncast streamer",
|
|
||||||
"quote_enabled": false,
|
|
||||||
}
|
|
Loading…
Reference in New Issue