Moved DB Connection to main.py

DB Connection is now created within the same event loop as client
pull/8/head
sgoudham 4 years ago
parent e1befcb730
commit ca66d108cf

@ -2,21 +2,24 @@ import datetime
import string import string
from typing import Optional from typing import Optional
import aiomysql
import discord import discord
from decouple import config from decouple import config
from discord import Embed from discord import Embed
from discord.ext import commands, tasks from discord.ext import commands, tasks
from discord.ext.commands import when_mentioned_or, is_owner, guild_only, has_permissions from discord.ext.commands import when_mentioned_or, is_owner, guild_only, has_permissions
import db
import settings import settings
from cogs.help import HelpPaginator from cogs.help import HelpPaginator
from db import connection
from settings import blank_space, enso_embedmod_colours, enso_guild_ID, enso_newpeople_ID, get_prefix_for_guild, \ from settings import blank_space, enso_embedmod_colours, enso_guild_ID, enso_newpeople_ID, get_prefix_for_guild, \
storage_prefix_for_guild, cache, del_cache storage_prefix_for_guild, cache, del_cache
counter = 0 counter = 0
# Get password/host from .env
password = config('DB_PASS')
host = config('DB_HOST')
# Getting the Bot token from Environment Variables # Getting the Bot token from Environment Variables
API_TOKEN = config('DISCORD_TOKEN') API_TOKEN = config('DISCORD_TOKEN')
@ -41,6 +44,21 @@ client = commands.Bot( # Create a new bot
version=get_version) # Version number of Ensō~Chan version=get_version) # Version number of Ensō~Chan
client.remove_command("help") # Remove default help command client.remove_command("help") # Remove default help command
# Setting up connection using pool/aiomysql
async def create_connection():
client.db = await aiomysql.create_pool(
host=host,
port=3306,
user="hamothy",
password=password,
db='enso',
loop=client.loop)
# Make sure the connection is setup before the bot is ready
client.loop.run_until_complete(create_connection())
if __name__ == '__main__': if __name__ == '__main__':
for ext in settings.extensions(): for ext in settings.extensions():
client.load_extension(ext) client.load_extension(ext)
@ -158,7 +176,7 @@ async def reload_db(ctx):
"""Reloads the database by inserting/updating all the records""" """Reloads the database by inserting/updating all the records"""
# Setup pool # Setup pool
pool = await connection(db.loop) pool = client.db
# Setup up pool connection and cursor # Setup up pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:
@ -209,7 +227,7 @@ async def on_guild_join(guild):
cache(str(guild.id), channel=None, prefix="~") cache(str(guild.id), channel=None, prefix="~")
# Setup pool # Setup pool
pool = await connection(db.loop) pool = client.db
# Setup up pool connection and cursor # Setup up pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:
@ -246,7 +264,7 @@ async def on_guild_remove(guild):
del_cache(str(guild.id)) del_cache(str(guild.id))
# Setup pool # Setup pool
pool = await connection(db.loop) pool = client.db
# Setup pool connection and cursor # Setup pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:
@ -284,7 +302,7 @@ async def on_member_join(member):
guild = member.guild guild = member.guild
# Setup pool # Setup pool
pool = await connection(db.loop) pool = client.db
# Setup pool connection and cursor # Setup pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:
@ -368,7 +386,7 @@ async def on_member_remove(member):
role_ids = ", ".join([str(r.id) for r in member.roles]) role_ids = ", ".join([str(r.id) for r in member.roles])
# Setup pool # Setup pool
pool = await connection(db.loop) pool = client.db
# Setup pool connection and cursor # Setup pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:

Loading…
Cancel
Save