You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
minecraft-discord-bridge/minecraft_discord_bridge/database.py

80 lines
2.6 KiB

#
# Copyright (c) 2018 Tristan Gosselin-Hane.
#
# This file is part of minecraft-discord-bridge
# (see https://github.com/starcraft66/minecraft-discord-bridge).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime, timedelta, timezone
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from .database_session import Base
class DiscordChannel(Base):
__tablename__ = 'discord_channels'
id = Column(Integer, primary_key=True)
channel_id = Column(Integer)
def __init__(self, channel_id):
self.channel_id = channel_id
class AccountLinkToken(Base):
__tablename__ = 'account_link_tokens'
id = Column(Integer, primary_key=True)
discord_account = relationship("DiscordAccount", back_populates="link_token")
token = Column(String)
expiry = Column(DateTime)
def __init__(self, discord_id, token):
self.discord_id = discord_id
self.token = token
now = datetime.now(timezone.utc)
# Token expires an hour from now
then = now + timedelta(hours=1)
self.expiry = then
class MinecraftAccount(Base):
__tablename__ = 'minecraft_accounts'
id = Column(Integer, primary_key=True)
minecraft_uuid = Column(String)
discord_account = relationship("DiscordAccount", back_populates="minecraft_account")
def __init__(self, minecraft_uuid, discord_id):
self.minecraft_uuid = minecraft_uuid
self.discord_account_id = discord_id
class DiscordAccount(Base):
__tablename__ = 'discord_accounts'
id = Column(Integer, primary_key=True)
discord_id = Column(Integer)
link_token_id = Column(Integer, ForeignKey('account_link_tokens.id'))
minecraft_account_id = Column(Integer, ForeignKey('minecraft_accounts.id'))
link_token = relationship(
"AccountLinkToken", uselist=False, foreign_keys=[link_token_id], back_populates="discord_account")
minecraft_account = relationship(
"MinecraftAccount", uselist=False, foreign_keys=[minecraft_account_id], back_populates="discord_account")
def __init__(self, discord_id):
self.discord_id = discord_id