Skip to content

Commit

Permalink
services: Remove prefix and move into folder
Browse files Browse the repository at this point in the history
The SQL migration is modified instead of added upon as it is a
backwards-compatible change (ie. servers.py can just ignore the
command_prefix field if it exists in the db).
  • Loading branch information
chanbakjsd committed Feb 4, 2021
1 parent fea6093 commit c83cbc4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
3 changes: 1 addition & 2 deletions sooch/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def migrate(self):
"""
CREATE TABLE IF NOT EXISTS `server`(
`discord_id` BIGINT PRIMARY KEY,
`name` VARCHAR(256),
`command_prefix` VARCHAR(32)
`name` VARCHAR(256)
);
"""
]
Expand Down
10 changes: 5 additions & 5 deletions sooch/listeners.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Implement listeners for all the events SoochBot cares about."""
import logging
from typing import Optional

import discord

from sooch.servers import Server, Servers
from sooch.services.servers import Server, Servers


logger = logging.getLogger("sooch")
servers: Optional[Servers] = None


async def on_guild_join(servers: Servers, guild: discord.Guild):
async def on_guild_join(guild: discord.Guild):
"""Handle guild joins, adding it to the database as necessary."""
logger.info(
"Joined guild %s, checking if we've been here before", guild.name)
Expand All @@ -22,11 +24,10 @@ async def on_guild_join(servers: Servers, guild: discord.Guild):
await servers.add_server(Server(
guild.id,
guild.name,
"s!"
))


async def on_ready(client: discord.Client, servers: Servers):
async def on_ready(client: discord.Client):
"""Setup the bot with latest info from the Discord server."""
logger.info("Connected to Discord.")
for guild in client.guilds:
Expand All @@ -40,5 +41,4 @@ async def on_ready(client: discord.Client, servers: Servers):
await servers.add_server(Server(
guild.id,
guild.name,
"s!"
))
13 changes: 4 additions & 9 deletions sooch/servers.py → sooch/services/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ async def get_server(self, discord_id: int) -> "Server":
"""Return the server info associated with the ID requested"""
cursor = self.database.connection.cursor()
cursor.execute(
("SELECT `name`, `command_prefix` FROM `server` "
"WHERE `discord_id` = ?"),
"SELECT `name` FROM `server` WHERE `discord_id` = ?",
(discord_id,)
)
row = cursor.fetchone()
Expand All @@ -26,18 +25,15 @@ async def get_server(self, discord_id: int) -> "Server":

return Server(
discord_id,
row[0],
row[1]
row[0]
)

async def add_server(self, server: "Server"):
"""Add the provided server into the database"""
cursor = self.database.connection.cursor()
cursor.execute(
("INSERT INTO `server`"
"(`discord_id`, `name`, `command_prefix`)"
"VALUES(?, ?, ?)"),
(server.discord_id, server.name, server.command_prefix)
("INSERT INTO `server` (`discord_id`, `name`) VALUES(?, ?)"),
(server.discord_id, server.name)
)
self.database.connection.commit()

Expand All @@ -47,4 +43,3 @@ class Server:
"""Represent an instance of a guild in Discord."""
discord_id: int
name: str
command_prefix: str = "s!"
12 changes: 8 additions & 4 deletions sooch/sooch_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from sooch import listeners, message, path
from sooch.database import Database
from sooch.servers import Servers
from sooch.services.servers import Servers


class SoochBot(discord.Client):
Expand Down Expand Up @@ -42,9 +42,13 @@ def __init__(self):
discord_logger.setLevel(DEBUG)
discord_logger.addHandler(stream_handler)
discord_logger.addHandler(file_handler)

# Read from config.
self.config = self.load_config()
self.database = Database(self.config)
self.servers = Servers(self.database)

# Initialize all the other modules.
listeners.servers = Servers(self.database)

super().__init__()

Expand Down Expand Up @@ -84,11 +88,11 @@ def start_sooching(self):

async def on_ready(self):
"""Prepare the SoochBot to serve commands when it connects."""
await listeners.on_ready(self, self.servers)
await listeners.on_ready(self)

async def on_guild_join(self, guild):
"""Register the guild that just joined if necessary."""
await listeners.on_guild_join(self.servers, guild)
await listeners.on_guild_join(guild)

async def on_message(self, msg: discord.Message):
"""Handle messages that are command and ignore others."""
Expand Down

0 comments on commit c83cbc4

Please sign in to comment.