From 7c58f5a97abf9ac5e7353940b0ddc50a585d9066 Mon Sep 17 00:00:00 2001 From: stolenvw Date: Sat, 10 Apr 2021 15:28:44 -0400 Subject: Major rewrite, Added optional commands for displaying V+ config settings --- code/utils/botsql.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 code/utils/botsql.py (limited to 'code/utils') diff --git a/code/utils/botsql.py b/code/utils/botsql.py new file mode 100644 index 0000000..5df39c6 --- /dev/null +++ b/code/utils/botsql.py @@ -0,0 +1,58 @@ +import mysql.connector, config, discord, asyncio +from discord.ext import commands +from colorama import Fore, Style, init +from config import SQL_HOST as MYhost +from config import SQL_PORT as MYport +from config import SQL_USER as MYuser +from config import SQL_PASS as MYpass +from config import SQL_DATABASE as MYbase + +# Connect to MYSQL + +class BotSQL(commands.Cog): + def __init__(self, bot): + self.bot = bot + + async def mydbconnect(self): + global mydb + mydb = mysql.connector.connect( + host=MYhost, + user=MYuser, + password=MYpass, + database=MYbase, + port=MYport, + ) + bugchan = self.bot.get_channel(config.BUGCHANNEL_ID) + try: + if mydb.is_connected(): + db_Info = mydb.get_server_info() + print(Fore.GREEN + "Connected to MySQL database... MySQL Server version ", db_Info + Style.RESET_ALL) + if config.USEDEBUGCHAN == True: + buginfo = discord.Embed(title=":white_check_mark: **INFO** :white_check_mark:", description="Connected to MySQL database... MySQL Server version " + db_Info, color=0x7EFF00) + buginfo.set_author(name=config.SERVER_NAME) + await bugchan.send(embed=buginfo) + except mysql.connector.Error as err: + print(Fore.RED + err + 'From MySQL database' + Style.RESET_ALL) + if config.USEDEBUGCHAN == True: + bugerror = discord.Embed(title=":sos: **ERROR** :sos:", description="{} From MySQL database".format(err), color=0xFF001E) + bugerror.set_author(name=config.SERVER_NAME) + await bugchan.send(embed=bugerror) + + async def get_cursor(self): + try: + mydb.ping(reconnect=True, attempts=3, delay=5) + except mysql.connector.Error as err: + await mydbconnect() + print(Fore.RED + "Connection to MySQL database went away... Reconnecting " + Style.RESET_ALL) + if config.USEDEBUGCHAN == True: + bugchan = bot.get_channel(dbchanID) + bugerror = discord.Embed(title=":sos: **ERROR** :sos:", description="Connection to MySQL database went away... Reconnecting", color=0xFF001E) + bugerror.set_author(name=config.SERVER_NAME) + await bugchan.send(embed=bugerror) + return mydb.cursor() + + async def botmydb(self): + mydb.commit() + +def setup(bot): + bot.add_cog(BotSQL(bot)) -- cgit v1.2.3-70-g09d2