2018-07-11 22:47:44 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logging.getLogger("discord").setLevel(logging.INFO)
|
|
|
|
logging.getLogger("websockets").setLevel(logging.INFO)
|
|
|
|
|
|
|
|
logger = logging.getLogger("pluralkit.bot")
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
client = discord.Client()
|
|
|
|
|
2018-07-11 22:49:02 +00:00
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
@client.event
|
|
|
|
async def on_error(evt, *args, **kwargs):
|
2018-07-11 22:49:02 +00:00
|
|
|
logger.exception(
|
|
|
|
"Error while handling event {} with arguments {}:".format(evt, args))
|
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
|
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
|
|
|
# Print status info
|
|
|
|
logger.info("Connected to Discord.")
|
2018-07-11 22:49:02 +00:00
|
|
|
logger.info("Account: {}#{}".format(
|
|
|
|
client.user.name, client.user.discriminator))
|
2018-07-11 22:47:44 +00:00
|
|
|
logger.info("User ID: {}".format(client.user.id))
|
|
|
|
|
2018-07-11 22:49:02 +00:00
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
@client.event
|
|
|
|
async def on_message(message):
|
|
|
|
# Ignore bot messages
|
|
|
|
if message.author.bot:
|
|
|
|
return
|
2018-07-11 22:49:02 +00:00
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
# Split into args. shlex sucks so we don't bother with quotes
|
|
|
|
args = message.content.split(" ")
|
|
|
|
|
|
|
|
from pluralkit import proxy, utils
|
|
|
|
|
|
|
|
# Find and execute command in map
|
|
|
|
if len(args) > 0 and args[0] in utils.command_map:
|
|
|
|
subcommand_map = utils.command_map[args[0]]
|
|
|
|
|
|
|
|
if len(args) >= 2 and args[1] in subcommand_map:
|
|
|
|
async with client.pool.acquire() as conn:
|
|
|
|
await subcommand_map[args[1]][0](conn, message, args[2:])
|
|
|
|
elif None in subcommand_map:
|
|
|
|
async with client.pool.acquire() as conn:
|
|
|
|
await subcommand_map[None][0](conn, message, args[1:])
|
|
|
|
elif len(args) >= 2:
|
|
|
|
embed = discord.Embed()
|
|
|
|
embed.colour = discord.Colour.dark_red()
|
|
|
|
embed.description = "Subcommand \"{}\" not found.".format(args[1])
|
|
|
|
await client.send_message(message.channel, embed=embed)
|
|
|
|
else:
|
|
|
|
# Try doing proxy parsing
|
|
|
|
async with client.pool.acquire() as conn:
|
|
|
|
await proxy.handle_proxying(conn, message)
|
|
|
|
|
2018-07-11 22:49:02 +00:00
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
@client.event
|
|
|
|
async def on_reaction_add(reaction, user):
|
|
|
|
from pluralkit import proxy
|
|
|
|
|
|
|
|
# Pass reactions to proxy system
|
|
|
|
async with client.pool.acquire() as conn:
|
|
|
|
await proxy.handle_reaction(conn, reaction, user)
|
|
|
|
|
2018-07-11 22:49:02 +00:00
|
|
|
|
2018-07-11 22:47:44 +00:00
|
|
|
async def run():
|
|
|
|
from pluralkit import db
|
|
|
|
try:
|
|
|
|
logger.info("Connecting to database...")
|
|
|
|
pool = await db.connect()
|
|
|
|
|
|
|
|
logger.info("Attempting to create tables...")
|
|
|
|
async with pool.acquire() as conn:
|
|
|
|
await db.create_tables(conn)
|
|
|
|
|
|
|
|
logger.info("Connecting to InfluxDB...")
|
|
|
|
|
|
|
|
client.pool = pool
|
|
|
|
logger.info("Connecting to Discord...")
|
|
|
|
await client.start(os.environ["TOKEN"])
|
|
|
|
finally:
|
|
|
|
logger.info("Logging out from Discord...")
|
2018-07-11 22:49:02 +00:00
|
|
|
await client.logout()
|