diff --git a/config.py b/config.py index c4e9d21..dfb1bbe 100644 --- a/config.py +++ b/config.py @@ -26,6 +26,7 @@ config = { 'terms_privacy': {'enabled': True}, 'knucklebones': {'enabled': True}, 'wordle': {'enabled': True}, - 'profiles': {'enabled': True} + 'profiles': {'enabled': True}, + 'tiktok': {'enabled': True} } } diff --git a/main.py b/main.py index b8b47fe..aa81789 100644 --- a/main.py +++ b/main.py @@ -107,6 +107,12 @@ class Selena(discord.Client): self.profiles = profiles # Properly set the profiles attribute logging.info("Profiles module loaded") + if config['modules']['tiktok']['enabled']: + from modules.social.tiktok import TikTok + tiktok = TikTok(self) + tiktok.setup(self.tree) + logging.info("TikTok module loaded") + bot = Selena() diff --git a/modules/social/tiktok.py b/modules/social/tiktok.py new file mode 100644 index 0000000..0f51503 --- /dev/null +++ b/modules/social/tiktok.py @@ -0,0 +1,47 @@ +import discord +from discord import app_commands +import logging +import re + + +class TikTok: + def __init__(self, bot): + self.bot = bot + self.logger = logging.getLogger('TikTok') + self.logger.setLevel(logging.DEBUG) + handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w') + handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')) + self.logger.addHandler(handler) + + async def on_message(self, message): + if message.author.bot: + return + + tiktok_url = self.extract_tiktok_url(message.content) + if tiktok_url: + embed = self.create_tiktok_embed(tiktok_url) + await message.channel.send(embed=embed) + await message.delete() + await message.author.send("Your TikTok video has been properly embedded.") + + def extract_tiktok_url(self, content): + tiktok_regex = re.compile( + r'(https?://(?:www\.)?tiktok\.com/[^ ]+|https?://vm\.tiktok\.com/[^ ]+)') + match = tiktok_regex.search(content) + return match.group(0) if match else None + + def create_tiktok_embed(self, url): + embed = discord.Embed(title="TikTok Video", description="Here is the TikTok video:", url=url) + embed.set_author(name="TikTok", icon_url="https://upload.wikimedia.org/wikipedia/en/a/a9/TikTok_logo.svg") + embed.add_field(name="Link", value=f"[Watch on TikTok]({url})") + embed.set_footer(text="TikTok Embed Module") + return embed + + def setup(self, tree: app_commands.CommandTree): + pass + + +def setup(bot): + tiktok = TikTok(bot) + bot.add_listener(tiktok.on_message, "on_message") + bot.tiktok_module = tiktok diff --git a/modules/social/twitch.py b/modules/social/twitch.py index c4255d7..e4df141 100644 --- a/modules/social/twitch.py +++ b/modules/social/twitch.py @@ -21,6 +21,20 @@ class Twitch: self.token = None self.token_expiry = None self.channel_alerts = {} + self.ensure_table_exists() + + def ensure_table_exists(self): + conn = sqlite3.connect(self.db_path) + cursor = conn.cursor() + cursor.execute(""" + CREATE TABLE IF NOT EXISTS twitch_channels ( + channel_name TEXT PRIMARY KEY, + alert_channel_id TEXT + ); + """) + conn.commit() + conn.close() + self.logger.info('Twitch channels table ensured in database') async def get_token(self): url = "https://id.twitch.tv/oauth2/token"