Clean up database sessions properly

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent e90ae8c45f
commit 452e9c89bb
  1. 1
      database.py
  2. 8
      webhook-bridge.py

@ -1,7 +1,6 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey from sqlalchemy import Column, String, Integer, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from database_session import Base from database_session import Base

@ -363,6 +363,7 @@ def main():
msg = "Please connect your minecraft account to `{}.{}:{}` in order to link it to this bridge!"\ msg = "Please connect your minecraft account to `{}.{}:{}` in order to link it to this bridge!"\
.format(new_token, config.auth_dns, config.auth_port) .format(new_token, config.auth_dns, config.auth_port)
session.close() session.close()
del session
await send_channel.send(msg) await send_channel.send(msg)
except discord.errors.Forbidden: except discord.errors.Forbidden:
if isinstance(message.author, discord.abc.User): if isinstance(message.author, discord.abc.User):
@ -483,6 +484,7 @@ def main():
if discord_user.minecraft_account: if discord_user.minecraft_account:
minecraft_uuid = discord_user.minecraft_account.minecraft_uuid minecraft_uuid = discord_user.minecraft_account.minecraft_uuid
session.close() session.close()
del session
minecraft_username = mc_uuid_to_username(minecraft_uuid) minecraft_username = mc_uuid_to_username(minecraft_uuid)
# Max chat message length: 256, bot username does not count towards this # Max chat message length: 256, bot username does not count towards this
@ -506,6 +508,7 @@ def main():
session = database_session.get_session() session = database_session.get_session()
channels = session.query(DiscordChannel).all() channels = session.query(DiscordChannel).all()
session.close() session.close()
del session
for channel in channels: for channel in channels:
webhooks = await discord_bot.get_channel(channel.channel_id).webhooks() webhooks = await discord_bot.get_channel(channel.channel_id).webhooks()
@ -529,6 +532,11 @@ def main():
msg = "Unable to send chat message: there is no Minecraft account linked to this discord account," \ msg = "Unable to send chat message: there is no Minecraft account linked to this discord account," \
"please run `mc!register`." "please run `mc!register`."
await send_channel.send(msg) await send_channel.send(msg)
session.close()
del session
else:
session.close()
del session
discord_bot.run(config.discord_token) discord_bot.run(config.discord_token)

Loading…
Cancel
Save