Properly closed MySQL connections
This commit is contained in:
parent
e95638f2cb
commit
28b47d2a13
3
bot.py
3
bot.py
|
@ -29,8 +29,7 @@ async def on_ready():
|
|||
destination = discord.utils.find(lambda m: m.id == result, bot.get_all_channels())
|
||||
await bot.send_message(destination, "I have just finished restarting!")
|
||||
cursor.execute('update restart_server set channel_id=0 where id=1')
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
|
||||
|
||||
@bot.event
|
||||
|
|
|
@ -49,8 +49,7 @@ class Core:
|
|||
result = cursor.fetchall()
|
||||
if {'channel_id': '{}'.format(ctx.message.channel.id)} in result:
|
||||
url += ",+explicit&filter_id=95938"
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
|
||||
# url should now be in the form of url?q=search+terms
|
||||
# Next part processes the json format, and saves the data in useful lists/dictionaries
|
||||
|
|
|
@ -52,8 +52,7 @@ def updateBattleRecords(winner, loser):
|
|||
sql = "insert into battle_records (id,record) values ('{0}','0-1')".format(loser.id)
|
||||
cursor.execute(sql)
|
||||
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
|
||||
|
||||
class Interaction:
|
||||
|
@ -163,8 +162,7 @@ class Interaction:
|
|||
cursor.execute(sql)
|
||||
fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!"
|
||||
await self.bot.say(fmt.format(booper, boopee, amount))
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
|
||||
|
||||
def setup(bot):
|
||||
|
|
|
@ -21,8 +21,7 @@ class Mod:
|
|||
except pymysql.IntegrityError:
|
||||
await self.bot.say("This channel is already registered as 'nsfw'!")
|
||||
return
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
await self.bot.say("This channel has just been registered as 'nsfw'! Have fun you naughties ;)")
|
||||
|
||||
@commands.command(pass_context=True)
|
||||
|
@ -36,8 +35,7 @@ class Mod:
|
|||
except pymysql.IntegrityError:
|
||||
await self.bot.say("This channel is not registered as a ''nsfw' channel!")
|
||||
return
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
await self.bot.say("This channel has just been unregistered as a nsfw channel")
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
|
|
@ -23,8 +23,7 @@ class Owner:
|
|||
cursor.execute('use {0}'.format(config.db_default))
|
||||
sql = "update restart_server set channel_id={0} where id=1".format(ctx.message.channel.id)
|
||||
cursor.execute(sql)
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
await self.bot.say("Restarting; see you in the next life {0}!".format(ctx.message.author.mention))
|
||||
python = sys.executable
|
||||
os.execl(python, python, *sys.argv)
|
||||
|
|
|
@ -22,8 +22,7 @@ class Stats:
|
|||
member = find(lambda m: m.id == result.get('id'), self.bot.get_all_members())
|
||||
await self.bot.say("{0} you have booped {1} the most amount of times, coming in at {2} times".format(
|
||||
ctx.message.author.mention, member.mention, result.get('amount')))
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
except Exception as e:
|
||||
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
|
||||
await self.bot.say(fmt.format(type(e).__name__, e))
|
||||
|
@ -46,8 +45,7 @@ class Stats:
|
|||
amount = r['amount']
|
||||
if member in members:
|
||||
output += "\n{0.name}: {1} times".format(member,amount)
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
await self.bot.say("```{}```".format(output))
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
@ -90,8 +88,7 @@ class Stats:
|
|||
count += 1
|
||||
for index in range(0, len(fmt)):
|
||||
fmt[index] = "{0}) {1}".format(index + 1, fmt[index])
|
||||
config.connection.commit()
|
||||
config.connection.close()
|
||||
config.closeConnection()
|
||||
if len(fmt) == 0:
|
||||
await self.bot.say("```No battling records found from any members in this server```")
|
||||
return
|
||||
|
|
|
@ -25,7 +25,12 @@ ownerCommands = global_config.get("ownerCommands", {})
|
|||
voiceCommands = global_config.get("voiceCommands", {})
|
||||
|
||||
def getCursor():
|
||||
global connection
|
||||
connection = pymysql.connect(host=global_config.get("db_host"), user=global_config.get("db_user"),
|
||||
password=global_config.get("db_user_pass"), charset='utf8mb4',
|
||||
cursorclass=pymysql.cursors.DictCursor)
|
||||
return connection.cursor()
|
||||
|
||||
def closeConnection():
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
|
Loading…
Reference in a new issue