Add support for a configuration file.
This commit is contained in:
@@ -194,11 +194,7 @@ app.add_routes([
|
||||
|
||||
async def run():
|
||||
app["pool"] = await db.connect(
|
||||
os.environ["DATABASE_USER"],
|
||||
os.environ["DATABASE_PASS"],
|
||||
os.environ["DATABASE_NAME"],
|
||||
os.environ["DATABASE_HOST"],
|
||||
int(os.environ["DATABASE_PORT"])
|
||||
os.environ["DATABASE_URI"]
|
||||
)
|
||||
return app
|
||||
|
||||
|
@@ -1,4 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
# uvloop doesn't work on Windows, therefore an optional dependency
|
||||
@@ -7,5 +10,13 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from pluralkit import bot
|
||||
bot.run()
|
||||
with open(sys.argv[1] if len(sys.argv) > 1 else "pluralkit.conf") as f:
|
||||
config = json.load(f)
|
||||
|
||||
if "database_uri" not in config and "database_uri" not in os.environ:
|
||||
print("Config file must contain key 'database_uri', or the environment variable DATABASE_URI must be present.")
|
||||
elif "token" not in config and "token" not in os.environ:
|
||||
print("Config file must contain key 'token', or the environment variable TOKEN must be present.")
|
||||
else:
|
||||
from pluralkit import bot
|
||||
bot.run(os.environ.get("TOKEN", config["token"]), os.environ.get("DATABASE_URI", config["database_uri"]), int(config.get("log_channel", 0)))
|
@@ -13,36 +13,12 @@ from pluralkit.bot import commands, proxy, channel_logger, embeds
|
||||
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s")
|
||||
|
||||
|
||||
def connect_to_database() -> asyncpg.pool.Pool:
|
||||
username = os.environ["DATABASE_USER"]
|
||||
password = os.environ["DATABASE_PASS"]
|
||||
name = os.environ["DATABASE_NAME"]
|
||||
host = os.environ["DATABASE_HOST"]
|
||||
port = os.environ["DATABASE_PORT"]
|
||||
|
||||
if username is None or password is None or name is None or host is None or port is None:
|
||||
print(
|
||||
"Database credentials not specified. Please pass valid PostgreSQL database credentials in the DATABASE_[USER|PASS|NAME|HOST|PORT] environment variable.",
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
port = int(port)
|
||||
except ValueError:
|
||||
print("Please pass a valid integer as the DATABASE_PORT environment variable.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
return asyncio.get_event_loop().run_until_complete(db.connect(
|
||||
username=username,
|
||||
password=password,
|
||||
database=name,
|
||||
host=host,
|
||||
port=port
|
||||
))
|
||||
def connect_to_database(uri: str) -> asyncpg.pool.Pool:
|
||||
return asyncio.get_event_loop().run_until_complete(db.connect(uri))
|
||||
|
||||
|
||||
def run():
|
||||
pool = connect_to_database()
|
||||
def run(token: str, db_uri: str, log_channel_id: int):
|
||||
pool = connect_to_database(db_uri)
|
||||
|
||||
async def create_tables():
|
||||
async with pool.acquire() as conn:
|
||||
@@ -51,7 +27,6 @@ def run():
|
||||
asyncio.get_event_loop().run_until_complete(create_tables())
|
||||
|
||||
client = discord.Client()
|
||||
|
||||
logger = channel_logger.ChannelLogger(client)
|
||||
|
||||
@client.event
|
||||
@@ -98,11 +73,14 @@ def run():
|
||||
|
||||
@client.event
|
||||
async def on_error(event_name, *args, **kwargs):
|
||||
log_channel_id = os.environ.get("LOG_CHANNEL")
|
||||
# Print it to stderr
|
||||
logging.getLogger("pluralkit").exception("Exception while handling event {}".format(event_name))
|
||||
|
||||
# Then log it to the given log channel
|
||||
# TODO: replace this with Sentry or something
|
||||
if not log_channel_id:
|
||||
return
|
||||
|
||||
log_channel = client.get_channel(int(log_channel_id))
|
||||
log_channel = client.get_channel(log_channel_id)
|
||||
|
||||
# If this is a message event, we can attach additional information in an event
|
||||
# ie. username, channel, content, etc
|
||||
@@ -124,14 +102,4 @@ def run():
|
||||
if len(traceback.format_exc()) >= (2000 - len("```python\n```")):
|
||||
traceback_str = "```python\n...{}```".format(traceback.format_exc()[- (2000 - len("```python\n...```")):])
|
||||
await log_channel.send(content=traceback_str, embed=embed)
|
||||
|
||||
# Print it to stderr anyway, though
|
||||
logging.getLogger("pluralkit").exception("Exception while handling event {}".format(event_name))
|
||||
|
||||
bot_token = os.environ["TOKEN"]
|
||||
if not bot_token:
|
||||
print("No token specified. Please pass a valid Discord bot token in the TOKEN environment variable.",
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
client.run(bot_token)
|
||||
client.run(token)
|
||||
|
@@ -22,7 +22,7 @@ async def help_root(ctx: CommandContext):
|
||||
|
||||
|
||||
async def invite_link(ctx: CommandContext):
|
||||
client_id = os.environ["CLIENT_ID"]
|
||||
client_id = (await ctx.client.application_info()).id
|
||||
|
||||
permissions = discord.Permissions()
|
||||
|
||||
|
@@ -12,10 +12,10 @@ from pluralkit.system import System
|
||||
from pluralkit.member import Member
|
||||
|
||||
logger = logging.getLogger("pluralkit.db")
|
||||
async def connect(username, password, database, host, port):
|
||||
async def connect(uri):
|
||||
while True:
|
||||
try:
|
||||
return await asyncpg.create_pool(user=username, password=password, database=database, host=host, port=port)
|
||||
return await asyncpg.create_pool(uri)
|
||||
except (ConnectionError, asyncpg.exceptions.CannotConnectNowError):
|
||||
logger.exception("Failed to connect to database, retrying in 5 seconds...")
|
||||
time.sleep(5)
|
||||
|
Reference in New Issue
Block a user