Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to SQLAlchemy for DB communication #139

Merged
merged 18 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ secrets.env
**/__pycache__
test-file.py
/src/cogs/TestCog.py
.vscode
.vscode
.idea
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apt install ffmpeg -y

# Install requirements first to take advantage of docker build layer caching
COPY ./src/requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip
Laura7089 marked this conversation as resolved.
Show resolved Hide resolved
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt

COPY ./src /code
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
restart: unless-stopped
volumes:
- "db_data:/var/lib/postgresql/data"
ports:
- "5432:5432"
Laura7089 marked this conversation as resolved.
Show resolved Hide resolved

bot:
build: .
Expand Down
13 changes: 7 additions & 6 deletions src/esportsbot/base_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .db_gateway import db_gateway
from esportsbot.db_gateway_v1 import DBGatewayActions
from esportsbot.models import Voicemaster_master, Voicemaster_slave, Guild_info


async def send_to_log_channel(self, guild_id, msg):
db_logging_call = db_gateway().get('guild_info', params={'guild_id': guild_id})
if db_logging_call and db_logging_call[0]['log_channel_id']:
await self.bot.get_channel(db_logging_call[0]['log_channel_id']).send(msg)
db_logging_call = DBGatewayActions().get(Guild_info, guild_id=guild_id)
if db_logging_call and db_logging_call.log_channel_id is not None:
await self.bot.get_channel(db_logging_call.log_channel_id).send(msg)


def role_id_from_mention(pre_clean_data: str) -> int:
Expand Down Expand Up @@ -48,10 +49,10 @@ def user_id_from_mention(pre_clean_data: str) -> int:


def get_whether_in_vm_master(guild_id, channel_id):
in_master = db_gateway().get('voicemaster_master', params={'guild_id': guild_id, 'channel_id': channel_id})
in_master = DBGatewayActions().get(Voicemaster_master, guild_id=guild_id, channel_id=channel_id)
return bool(in_master)


def get_whether_in_vm_slave(guild_id, channel_id):
in_slave = db_gateway().get('voicemaster_slave', params={'guild_id': guild_id, 'channel_id': channel_id})
in_slave = DBGatewayActions().get(Voicemaster_slave, guild_id=guild_id, channel_id=channel_id)
return bool(in_slave)
114 changes: 50 additions & 64 deletions src/esportsbot/bot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Dict, Any
from dotenv import load_dotenv
from . import lib
from .base_functions import get_whether_in_vm_master, get_whether_in_vm_slave
from .generate_schema import generate_schema
from .db_gateway import db_gateway
from esportsbot import lib
from esportsbot.base_functions import get_whether_in_vm_master, get_whether_in_vm_slave

from esportsbot.db_gateway_v1 import DBGatewayActions
from esportsbot.models import Guild_info, Voicemaster_slave, Pingable_roles

from discord.ext import commands
from discord.ext.commands import CommandNotFound, MissingRequiredArgument
from discord.ext.commands.context import Context
Expand All @@ -22,30 +23,12 @@

# EsportsBot client instance
client = lib.client.instance()
# TODO
client.remove_command('help')


def make_guild_init_data(guild: discord.Guild) -> Dict[str, Any]:
"""Construct default data for a guild database registration.

:param discord.Guild guild: The guild to be registered
:return: A dictionary with default guild attributes, including the guild ID
:rtype: Dict[str, Any]
"""
return {
'guild_id': guild.id,
'num_running_polls': 0,
'role_ping_cooldown_seconds': int(DEFAULT_ROLE_PING_COOLDOWN.total_seconds()),
"pingme_create_threshold": DEFAULT_PINGME_CREATE_THRESHOLD,
"pingme_create_poll_length_seconds": int(DEFAULT_PINGME_CREATE_POLL_LENGTH.total_seconds())
}


async def send_to_log_channel(guild_id, msg):
db_logging_call = db_gateway().get('guild_info', params={'guild_id': guild_id})
if db_logging_call and db_logging_call[0]['log_channel_id']:
await client.get_channel(db_logging_call[0]['log_channel_id']).send(msg)
db_logging_call = DBGatewayActions().get(Guild_info, guild_id=guild_id)
if db_logging_call and db_logging_call.log_channel_id is not None:
await client.get_channel(db_logging_call.log_channel_id).send(msg)


@client.event
Expand All @@ -63,21 +46,32 @@ async def on_ready():
@client.event
async def on_guild_join(guild):
print(f"Joined the guild: {guild.name}")
db_gateway().insert('guild_info', params=make_guild_init_data(guild))
DBGatewayActions().create(
Guild_info(
guild_id=guild.id,
num_running_polls=0,
role_ping_cooldown_seconds=int(DEFAULT_ROLE_PING_COOLDOWN.total_seconds()),
pingme_create_threshold=DEFAULT_PINGME_CREATE_THRESHOLD,
pingme_create_poll_length_seconds=int(DEFAULT_PINGME_CREATE_POLL_LENGTH.total_seconds())
)
)


@client.event
async def on_guild_remove(guild):
print(f"Left the guild: {guild.name}")
db_gateway().delete('guild_info', where_params={'guild_id': guild.id})
guild_from_db = DBGatewayActions().get(Guild_info, guild_id=guild.id)
if guild_from_db:
DBGatewayActions().delete(guild_from_db)
print(f"Left the guild: {guild.name}")


@client.event
async def on_member_join(member):
default_role_exists = db_gateway().get('guild_info', params={'guild_id': member.guild.id})
guild = DBGatewayActions().get(Guild_info, guild_id=member.guild.id)
default_role_exists = guild.default_role_id is not None

if default_role_exists[0]['default_role_id']:
default_role = member.guild.get_role(default_role_exists[0]['default_role_id'])
if default_role_exists:
default_role = member.guild.get_role(guild.default_role_id)
await member.add_roles(default_role)
await send_to_log_channel(
member.guild.id,
Expand All @@ -93,41 +87,27 @@ async def on_voice_state_update(member, before, after):
after_channel_id = after.channel.id if after.channel != None else False

if before_channel_id and get_whether_in_vm_slave(member.guild.id, before_channel_id):
vm_slave = DBGatewayActions().get(Voicemaster_slave, guild_id=member.guild.id, channel_id=before_channel_id)
# If you were in a slave VM VC
if not before.channel.members:
# Nobody else in VC
await before.channel.delete()
db_gateway().delete(
'voicemaster_slave',
where_params={
'guild_id': member.guild.id,
'channel_id': before_channel_id
}
)
DBGatewayActions().delete(vm_slave)
await send_to_log_channel(member.guild.id, f"{member.mention} has deleted a VM slave")
else:
# Still others in VC
await before.channel.edit(name=f"{before.channel.members[0].display_name}'s VC")
db_gateway().update(
'voicemaster_slave',
set_params={'owner_id': before.channel.members[0].id},
where_params={
'guild_id': member.guild.id,
'channel_id': before_channel_id
}
)
vm_slave.owner_id = before.channel.members[0].id
DBGatewayActions().update(vm_slave)
elif after_channel_id and get_whether_in_vm_master(member.guild.id, after_channel_id):
# Moved into a master VM VC
slave_channel_name = f"{member.display_name}'s VC"
new_slave_channel = await member.guild.create_voice_channel(slave_channel_name, category=after.channel.category)
db_gateway().insert(
'voicemaster_slave',
params={
'guild_id': member.guild.id,
'channel_id': new_slave_channel.id,
'owner_id': member.id,
'locked': False,
}
DBGatewayActions().create(
Voicemaster_slave(guild_id=member.guild.id,
channel_id=new_slave_channel.id,
owner_id=member.id,
locked=False)
)
await member.move_to(new_slave_channel)
await send_to_log_channel(member.guild.id, f"{member.mention} has created a VM slave")
Expand Down Expand Up @@ -262,7 +242,7 @@ async def on_message(message):
# Handle music channel messages
guild_id = message.guild.id
music_channel_in_db = client.MUSIC_CHANNELS.get(guild_id)
if music_channel_in_db == message.channel.id:
if music_channel_in_db:
# The message was in a music channel and a song should be found
music_cog_instance = client.cogs.get('MusicCog')
await music_cog_instance.on_message_handle(message)
Expand All @@ -284,11 +264,19 @@ async def on_message(message):
@client.command()
@commands.has_permissions(administrator=True)
async def initialsetup(ctx):
already_in_db = db_gateway().get('guild_info', params={'guild_id': ctx.author.guild.id})
already_in_db = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
if already_in_db:
await ctx.channel.send("This server is already set up")
else:
db_gateway().insert('guild_info', make_guild_init_data(ctx.guild))
DBGatewayActions().create(
Guild_info(
guild_id=ctx.author.guild.id,
num_running_polls=0,
role_ping_cooldown_seconds=int(DEFAULT_ROLE_PING_COOLDOWN.total_seconds()),
pingme_create_threshold=DEFAULT_PINGME_CREATE_THRESHOLD,
pingme_create_poll_length_seconds=int(DEFAULT_PINGME_CREATE_POLL_LENGTH.total_seconds())
)
)
await ctx.channel.send("This server has now been initialised")


Expand All @@ -298,9 +286,9 @@ async def on_guild_role_delete(role: discord.Role):

:param Role role: The role which was removed
"""
db = db_gateway()
if db.get("pingable_roles", {"role_id": role.id}):
db.delete("pingable_roles", {"role_id": role.id})
pingable_role = DBGatewayActions().get(Pingable_roles, role_id=role.id)
if pingable_role:
DBGatewayActions().delete(pingable_role)
logEmbed = discord.Embed()
logEmbed.set_author(icon_url=client.user.avatar_url_as(size=64), name="Admin Log")
logEmbed.set_footer(text=datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
Expand All @@ -311,11 +299,9 @@ async def on_guild_role_delete(role: discord.Role):


def launch():
load_dotenv()

TOKEN = os.getenv('DISCORD_TOKEN')

# Generate Database Schema
generate_schema()
client.update_music_channels()

client.load_extension('esportsbot.cogs.VoicemasterCog')
Expand Down
3 changes: 1 addition & 2 deletions src/esportsbot/cogs/AdminCog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import toml
from discord.ext import commands
from ..db_gateway import db_gateway
from ..base_functions import send_to_log_channel
from esportsbot.base_functions import send_to_log_channel


class AdminCog(commands.Cog):
Expand Down
33 changes: 15 additions & 18 deletions src/esportsbot/cogs/DefaultRoleCog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import toml
from discord.ext import commands
from ..db_gateway import db_gateway
from ..base_functions import role_id_from_mention
from ..base_functions import send_to_log_channel
from esportsbot.db_gateway_v1 import DBGatewayActions
from esportsbot.models import Guild_info
from esportsbot.base_functions import role_id_from_mention, send_to_log_channel


class DefaultRoleCog(commands.Cog):
Expand All @@ -15,11 +15,9 @@ def __init__(self, bot):
async def setdefaultrole(self, ctx, given_role_id=None):
cleaned_role_id = role_id_from_mention(given_role_id) if given_role_id else False
if cleaned_role_id:
db_gateway().update(
'guild_info',
set_params={'default_role_id': cleaned_role_id},
where_params={'guild_id': ctx.author.guild.id}
)
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
guild.default_role_id = cleaned_role_id
DBGatewayActions().update(guild)
await ctx.channel.send(self.STRINGS['default_role_set'].format(role_id=cleaned_role_id))
default_role = ctx.author.guild.get_role(cleaned_role_id)
await send_to_log_channel(
Expand All @@ -34,24 +32,23 @@ async def setdefaultrole(self, ctx, given_role_id=None):
@commands.command()
@commands.has_permissions(administrator=True)
async def getdefaultrole(self, ctx):
default_role_exists = db_gateway().get('guild_info', params={'guild_id': ctx.author.guild.id})
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
default_role_exists = guild.default_role_id is not None

if default_role_exists[0]['default_role_id']:
await ctx.channel.send(self.STRINGS['default_role_get'].format(role_id=default_role_exists[0]['default_role_id']))
if default_role_exists:
await ctx.channel.send(self.STRINGS['default_role_get'].format(role_id=guild.default_role_id))
else:
await ctx.channel.send(self.STRINGS['default_role_missing'])

@commands.command()
@commands.has_permissions(administrator=True)
async def removedefaultrole(self, ctx):
default_role_exists = db_gateway().get('guild_info', params={'guild_id': ctx.author.guild.id})
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
default_role_exists = guild.default_role_id is not None

if default_role_exists[0]['default_role_id']:
db_gateway().update(
'guild_info',
set_params={'default_role_id': 'NULL'},
where_params={'guild_id': ctx.author.guild.id}
)
if default_role_exists:
guild.default_role_id = None
DBGatewayActions().update(guild)
await ctx.channel.send(self.STRINGS['default_role_removed'])
await send_to_log_channel(
self,
Expand Down
Loading