FIX: Fixed Twitch

FEAT: Added Tiktok, This new module should auto-embed tiktoks to have them be viewable
This commit is contained in:
Dan 2024-07-10 12:10:30 -04:00
parent 5219d5a6c8
commit f7ffd77942
4 changed files with 69 additions and 1 deletions

View File

@ -26,6 +26,7 @@ config = {
'terms_privacy': {'enabled': True},
'knucklebones': {'enabled': True},
'wordle': {'enabled': True},
'profiles': {'enabled': True}
'profiles': {'enabled': True},
'tiktok': {'enabled': True}
}
}

View File

@ -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()

47
modules/social/tiktok.py Normal file
View File

@ -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

View File

@ -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"