1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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 NameError as er:
print(Fore.RED, er, "Reconnecting MySQL database" + Style.RESET_ALL)
if config.USEDEBUGCHAN == True:
bugchan = self.bot.get_channel(config.BUGCHANNEL_ID)
bugerror = discord.Embed(title=":sos: **ERROR** :sos:", description= "{} Reconnection MySQL database".format(er), color=0xFF001E)
bugerror.set_author(name=config.SERVER_NAME)
await bugchan.send(embed=bugerror)
await self.mydbconnect()
except mysql.connector.Error as err:
await self.mydbconnect()
print(Fore.RED + "Connection to MySQL database went away... Reconnecting " + Style.RESET_ALL)
if config.USEDEBUGCHAN == True:
bugchan = self.bot.get_channel(config.BUGCHANNEL_ID)
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()
async def get_mydb(self):
try:
mydb.ping(reconnect=True, attempts=3, delay=5)
except NameError as er:
print(Fore.RED, er, "Reconnecting MySQL database" + Style.RESET_ALL)
if config.USEDEBUGCHAN == True:
bugchan = self.bot.get_channel(config.BUGCHANNEL_ID)
bugerror = discord.Embed(title=":sos: **ERROR** :sos:", description= "{} Reconnection MySQL database".format(er), color=0xFF001E)
bugerror.set_author(name=config.SERVER_NAME)
await bugchan.send(embed=bugerror)
await self.mydbconnect()
except mysql.connector.Error as err:
await self.mydbconnect()
print(Fore.RED + "Connection to MySQL database went away... Reconnecting " + Style.RESET_ALL)
if config.USEDEBUGCHAN == True:
bugchan = self.bot.get_channel(config.BUGCHANNEL_ID)
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
def setup(bot):
bot.add_cog(BotSQL(bot))
|