FEAT: Added Twitch Module (Just Checks if a user is live or not right now)

REF: Removed unneeded database generation files
DOC: Added Twitch module to the Config and main.py
REF: Changed 'currency' to 'Kibble'
This commit is contained in:
Dan 2024-06-21 18:38:15 -04:00
parent c78912783a
commit 81f4981646
6 changed files with 93 additions and 50 deletions

View File

@ -19,4 +19,5 @@ ENABLED_MODULES = [
"modules.media.spotify_module",
"modules.user.birthday_module",
"modules.money.currency_module",
"modules.social.twitch_module",
]

View File

@ -15,7 +15,7 @@ class Selena(discord.Client):
guild = discord.Object(id=config.DISCORD_GUILD_ID)
await self.load_extensions()
self.tree.copy_global_to(guild=guild)
await self.tree.sync()
await self.tree.sync(guild=guild)
async def load_extension(self, name):
module = __import__(name, fromlist=["setup"])

View File

@ -1,18 +0,0 @@
import sqlite3
def initialize_db():
conn = sqlite3.connect('currency.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS currency (
user_id TEXT PRIMARY KEY,
balance INTEGER
)
''')
conn.commit()
conn.close()
def get_connection():
return sqlite3.connect('currency.db')

View File

@ -20,9 +20,9 @@ class CurrencyModule:
def add_commands(self):
@app_commands.command(
name="earn_currency", description="Earn currency"
name="earn_kibble", description="Earn Kibble"
)
async def earn_currency(interaction: discord.Interaction):
async def earn_kibble(interaction: discord.Interaction):
await interaction.response.defer()
try:
conn = get_connection()
@ -37,13 +37,13 @@ class CurrencyModule:
if datetime.now() - last_earned < CurrencyModule.COOLDOWN_PERIOD:
await interaction.followup.send(
embed=discord.Embed(
title="Earn Currency",
title="Earn Kibble",
description="You are on cooldown. Please try again later.",
color=discord.Color.red()
)
)
conn.close()
logger.info(f"User {user_id} attempted to earn currency but is on cooldown.")
logger.info(f"User {user_id} attempted to earn Kibble but is on cooldown.")
return
amount = random.choices([random.randint(1, 10), 100], [0.99, 0.01])[0]
@ -57,18 +57,18 @@ class CurrencyModule:
conn.close()
await interaction.followup.send(
embed=discord.Embed(
title="Earn Currency",
description=f"You have earned {amount} currency. Your new balance is {new_balance}.",
title="Earn Kibble",
description=f"You have earned {amount} Kibble. Your new balance is {new_balance}.",
color=discord.Color.green()
)
)
logger.info(f"User {user_id} earned {amount} currency. New balance: {new_balance}")
logger.info(f"User {user_id} earned {amount} Kibble. New balance: {new_balance}")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in earn_currency command: {e}")
logger.error(f"Error in earn_kibble command: {e}")
@app_commands.command(
name="check_balance", description="Check your currency balance"
name="check_balance", description="Check your Kibble balance"
)
async def check_balance(interaction: discord.Interaction):
await interaction.response.defer()
@ -82,7 +82,7 @@ class CurrencyModule:
await interaction.followup.send(
embed=discord.Embed(
title="Check Balance",
description=f"Your current balance is {result[0]} currency.",
description=f"Your current balance is {result[0]} Kibble.",
color=discord.Color.green()
)
)
@ -91,16 +91,16 @@ class CurrencyModule:
await interaction.followup.send(
embed=discord.Embed(
title="Check Balance",
description="You have no currency.",
description="You have no Kibble.",
color=discord.Color.red()
)
)
logger.info(f"User {interaction.user.id} has no currency.")
logger.info(f"User {interaction.user.id} has no Kibble.")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in check_balance command: {e}")
self.bot.tree.add_command(earn_currency)
self.bot.tree.add_command(earn_kibble)
self.bot.tree.add_command(check_balance)

View File

@ -0,0 +1,78 @@
import discord
from discord import app_commands
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
import logging
import config
logger = logging.getLogger(__name__)
class TwitchModule:
def __init__(self, bot):
self.bot = bot
self.twitch = Twitch(config.TWITCH_CLIENT_ID,
config.TWITCH_CLIENT_SECRET)
self.bot.loop.create_task(self.authenticate_twitch())
self.add_commands()
async def authenticate_twitch(self):
await self.twitch.authenticate_app([])
def add_commands(self):
@app_commands.command(
name="twitch_live",
description="Check if a Twitch streamer is live"
)
async def twitch_live(interaction: discord.Interaction, streamer: str):
await interaction.response.defer()
try:
logger.debug(f"Fetching user info for streamer: {streamer}")
user_info = await first(self.twitch.get_users(logins=[streamer]))
if not user_info:
await interaction.followup.send(
embed=discord.Embed(
title="Twitch Live Check",
description=f"Streamer {streamer} not found.",
color=discord.Color.red()
)
)
logger.info(f"Streamer {streamer} not found.")
return
user_id = user_info.id
logger.debug(f"Fetching stream info for user ID: {user_id}")
streams = await first(self.twitch.get_streams(user_id=[user_id]))
if streams:
embed = discord.Embed(
title=f"{streamer} is Live!",
description=(
f"**Title:** {streams.title}\n"
f"**Game:** {streams.game_name}\n"
f"**Viewers:** {streams.viewer_count}"
),
color=discord.Color.green()
)
embed.set_thumbnail(
url=streams.thumbnail_url.replace('{width}', '320').replace('{height}', '180')
)
await interaction.followup.send(embed=embed)
logger.info(f"Streamer {streamer} is live.")
else:
await interaction.followup.send(
embed=discord.Embed(
title=f"{streamer} is not live",
description=f"{streamer} is currently offline.",
color=discord.Color.red()
)
)
logger.info(f"Streamer {streamer} is offline.")
except Exception as e:
logger.error(f"Error in twitch_live command: {e}", exc_info=True)
await interaction.followup.send(f"An error occurred: {e}")
self.bot.tree.add_command(twitch_live)
async def setup(bot):
TwitchModule(bot)

View File

@ -1,18 +0,0 @@
import sqlite3
def initialize_db():
conn = sqlite3.connect('birthdays.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS birthdays (
user_id TEXT PRIMARY KEY,
birthday TEXT
)
''')
conn.commit()
conn.close()
def get_connection():
return sqlite3.connect('birthdays.db')