diff options
author | stolenvw <stolenvw@hotmail.com> | 2021-04-10 15:28:44 -0400 |
---|---|---|
committer | stolenvw <stolenvw@hotmail.com> | 2021-04-10 15:28:44 -0400 |
commit | 7c58f5a97abf9ac5e7353940b0ddc50a585d9066 (patch) | |
tree | 7c58c1b3a856822495f95701f2417b0ec8c9ab2b /code/utils | |
parent | 10003220489fe8493021e35f7243a4f63525f376 (diff) |
Major rewrite, Added optional commands for displaying V+ config settings
Diffstat (limited to 'code/utils')
-rw-r--r-- | code/utils/botsql.py | 58 |
1 files changed, 58 insertions, 0 deletions
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)) |