DeepBlue/bot.py

113 lines
3.2 KiB
Python
Raw Normal View History

2020-01-06 23:29:42 +01:00
import discord
2020-01-07 02:41:08 +01:00
import os
import json
2020-01-06 23:29:42 +01:00
from discord.ext import commands
2020-01-08 02:08:07 +01:00
from util import logging, config
2020-01-07 02:41:08 +01:00
# Bot URL https://discordapp.com/api/oauth2/authorize?client_id=657709911337074698&permissions=314432&scope=bot
2020-01-08 02:26:36 +01:00
# The config must be loaded before everything else
2020-01-08 02:08:07 +01:00
total = 0
failed = 0
2020-01-07 18:04:00 +01:00
2020-01-08 02:08:07 +01:00
logging.info("Starting up...\n")
logging.info("--- Loading configs ---\n")
if not config.load("config.json"):
failed += 1
total += 1
logging.info("Finished loading configs.")
logging.info(f"Total {total}, Failed {failed}\n")
client = commands.Bot(command_prefix=config.settings["prefix"], case_insensitive=True)
2020-01-07 18:04:00 +01:00
2020-01-07 02:41:08 +01:00
2020-01-08 21:18:17 +01:00
@client.command(name="load", description="Loads a cog", usage="load <Cog>")
2020-01-07 02:41:08 +01:00
@commands.is_owner()
2020-01-08 02:26:36 +01:00
async def load(ctx : commands.Context, extension : str):
2020-01-07 04:07:39 +01:00
try:
client.load_extension(f"cogs.{extension}")
await ctx.message.add_reaction("👍")
except:
await ctx.message.add_reaction("👎")
2020-01-07 02:41:08 +01:00
2020-01-08 21:18:17 +01:00
@client.command(name="unload", description="Unoads a cog", usage="unload <Cog>")
2020-01-07 02:41:08 +01:00
@commands.is_owner()
2020-01-08 02:26:36 +01:00
async def unload(ctx : commands.Context, extension : str):
2020-01-07 04:07:39 +01:00
try:
client.unload_extension(f"cogs.{extension}")
await ctx.message.add_reaction("👍")
except:
await ctx.message.add_reaction("👎")
2020-01-07 02:41:08 +01:00
2020-01-08 21:18:17 +01:00
@client.command(name="reload", description="Reoads a cog", usage="reload <Cog>")
2020-01-07 04:07:39 +01:00
@commands.is_owner()
2020-01-08 02:26:36 +01:00
async def reload(ctx : commands.Context, extension : str):
2020-01-07 04:07:39 +01:00
try:
client.reload_extension(f"cogs.{extension}")
await ctx.message.add_reaction("👍")
except:
await ctx.message.add_reaction("👎")
2020-01-06 23:29:42 +01:00
@client.event
async def on_ready():
2020-01-08 02:26:36 +01:00
# Try to load cogs
2020-01-08 02:08:07 +01:00
logging.info("--- Loading cogs ---\n")
2020-01-07 02:41:08 +01:00
total = 0
failed = 0
# Load all cogs on startup
2020-01-08 21:18:17 +01:00
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
total += 1
cog = f"cogs.{filename[:-3]}"
try:
client.load_extension(cog)
logging.info(f"Trying {cog}.....Success!")
except Exception as e:
logging.info(f"Trying {cog}.....Failed!")
logging.error(str(e))
failed += 1
2020-01-07 02:41:08 +01:00
# Load "fun" cogs
for filename in os.listdir("./cogs/fun"):
if filename.endswith(".py"):
total += 1
cog = f"cogs.fun.{filename[:-3]}"
try:
client.load_extension(cog)
logging.info(f"Trying {cog}.....Success!")
2020-01-08 02:08:07 +01:00
except Exception as e:
logging.info(f"Trying {cog}.....Failed!")
logging.error(str(e))
2020-01-07 02:41:08 +01:00
failed += 1
# Load "api" cogs
for filename in os.listdir("./cogs/api"):
if filename.endswith(".py"):
total += 1
cog = f"cogs.api.{filename[:-3]}"
try:
client.load_extension(cog)
logging.info(f"Trying {cog}.....Success!")
2020-01-08 02:08:07 +01:00
except Exception as e:
logging.info(f"Trying {cog}.....Failed!")
logging.error(str(e))
2020-01-07 02:41:08 +01:00
failed += 1
logging.info("Finished loading cogs.")
logging.info(f"Total {total}, Failed {failed}\n")
logging. info("Deep Blue finished loading.")
2020-01-06 23:29:42 +01:00
2020-01-07 18:04:00 +01:00
2020-01-08 02:08:07 +01:00
key = config.settings["client_key"]
2020-01-07 02:41:08 +01:00
client.run(key)