REF: Made sure everything conforms to Flake8's standards
FIX: Fixed the Permission issues with logger FEAT: Changed how the XP system works. Now does a scaling curve with xp being 1-5
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from twitchAPI.twitch import Twitch
|
||||
from twitchAPI.helper import first
|
||||
import logging
|
||||
from twitchAPI.twitch import Twitch
|
||||
|
||||
import config
|
||||
import asyncio
|
||||
from modules.data.db import get_connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -13,8 +15,7 @@ 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.twitch = Twitch(config.TWITCH_CLIENT_ID, config.TWITCH_CLIENT_SECRET)
|
||||
self.bot.loop.create_task(self.authenticate_twitch())
|
||||
self.bot.loop.create_task(self.check_live_streams())
|
||||
self.add_commands()
|
||||
@ -26,15 +27,17 @@ class TwitchModule:
|
||||
while True:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute('''
|
||||
c.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS live_status (
|
||||
twitch_name TEXT PRIMARY KEY,
|
||||
is_live BOOLEAN
|
||||
)
|
||||
''')
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
c.execute('SELECT twitch_name, discord_channel_id FROM followed_channels')
|
||||
c.execute("SELECT twitch_name, discord_channel_id FROM followed_channels")
|
||||
followed_channels = c.fetchall()
|
||||
|
||||
for twitch_name, discord_channel_id in followed_channels:
|
||||
@ -47,7 +50,10 @@ class TwitchModule:
|
||||
streams = await first(self.twitch.get_streams(user_id=[user_id]))
|
||||
is_live = streams is not None
|
||||
|
||||
c.execute('SELECT is_live FROM live_status WHERE twitch_name = ?', (twitch_name,))
|
||||
c.execute(
|
||||
"SELECT is_live FROM live_status WHERE twitch_name = ?",
|
||||
(twitch_name,),
|
||||
)
|
||||
row = c.fetchone()
|
||||
was_live = row[0] if row else False
|
||||
|
||||
@ -61,18 +67,26 @@ class TwitchModule:
|
||||
f"**Game:** {streams.game_name}\n"
|
||||
f"**Viewers:** {streams.viewer_count}"
|
||||
),
|
||||
color=discord.Color.green()
|
||||
color=discord.Color.green(),
|
||||
)
|
||||
embed.set_thumbnail(
|
||||
url=streams.thumbnail_url.replace('{width}', '320').replace('{height}', '180')
|
||||
url=streams.thumbnail_url.replace(
|
||||
"{width}", "320"
|
||||
).replace("{height}", "180")
|
||||
)
|
||||
await channel.send(embed=embed)
|
||||
|
||||
c.execute('INSERT OR REPLACE INTO live_status (twitch_name, is_live) VALUES (?, ?)', (twitch_name, is_live))
|
||||
c.execute(
|
||||
"INSERT OR REPLACE INTO live_status (twitch_name, is_live) VALUES (?, ?)",
|
||||
(twitch_name, is_live),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking live status for {twitch_name}: {e}", exc_info=True)
|
||||
logger.error(
|
||||
f"Error checking live status for {twitch_name}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
conn.close()
|
||||
await asyncio.sleep(300) # Check every 5 minutes
|
||||
@ -80,15 +94,19 @@ class TwitchModule:
|
||||
def add_commands(self):
|
||||
@app_commands.command(
|
||||
name="follow_twitch",
|
||||
description="Follow a Twitch channel to get live alerts"
|
||||
description="Follow a Twitch channel to get live alerts",
|
||||
)
|
||||
async def follow_twitch(interaction: discord.Interaction, twitch_name: str, channel: discord.TextChannel = None):
|
||||
async def follow_twitch(
|
||||
interaction: discord.Interaction,
|
||||
twitch_name: str,
|
||||
channel: discord.TextChannel = None,
|
||||
):
|
||||
channel = channel or interaction.channel
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute(
|
||||
'INSERT OR REPLACE INTO followed_channels (twitch_name, discord_channel_id) VALUES (?, ?)',
|
||||
(twitch_name, channel.id)
|
||||
"INSERT OR REPLACE INTO followed_channels (twitch_name, discord_channel_id) VALUES (?, ?)",
|
||||
(twitch_name, channel.id),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@ -96,40 +114,34 @@ class TwitchModule:
|
||||
embed=discord.Embed(
|
||||
title="Followed Twitch Channel",
|
||||
description=f"Now following {twitch_name}. Alerts will be sent to {channel.mention}.",
|
||||
color=discord.Color.green()
|
||||
color=discord.Color.green(),
|
||||
)
|
||||
)
|
||||
logger.info(f"Now following {twitch_name} for alerts in {channel.name}")
|
||||
|
||||
@app_commands.command(
|
||||
name="unfollow_twitch",
|
||||
description="Unfollow a Twitch channel"
|
||||
name="unfollow_twitch", description="Unfollow a Twitch channel"
|
||||
)
|
||||
async def unfollow_twitch(interaction: discord.Interaction, twitch_name: str):
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute(
|
||||
'DELETE FROM followed_channels WHERE twitch_name = ?',
|
||||
(twitch_name,)
|
||||
)
|
||||
c.execute(
|
||||
'DELETE FROM live_status WHERE twitch_name = ?',
|
||||
(twitch_name,)
|
||||
"DELETE FROM followed_channels WHERE twitch_name = ?", (twitch_name,)
|
||||
)
|
||||
c.execute("DELETE FROM live_status WHERE twitch_name = ?", (twitch_name,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
await interaction.response.send_message(
|
||||
embed=discord.Embed(
|
||||
title="Unfollowed Twitch Channel",
|
||||
description=f"No longer following {twitch_name}.",
|
||||
color=discord.Color.red()
|
||||
color=discord.Color.red(),
|
||||
)
|
||||
)
|
||||
logger.info(f"No longer following {twitch_name}")
|
||||
|
||||
@app_commands.command(
|
||||
name="twitch_live",
|
||||
description="Check if a Twitch streamer is live"
|
||||
name="twitch_live", description="Check if a Twitch streamer is live"
|
||||
)
|
||||
async def twitch_live(interaction: discord.Interaction, streamer: str):
|
||||
await interaction.response.defer()
|
||||
@ -141,7 +153,7 @@ class TwitchModule:
|
||||
embed=discord.Embed(
|
||||
title="Twitch Live Check",
|
||||
description=f"Streamer {streamer} not found.",
|
||||
color=discord.Color.red()
|
||||
color=discord.Color.red(),
|
||||
)
|
||||
)
|
||||
logger.info(f"Streamer {streamer} not found.")
|
||||
@ -158,10 +170,12 @@ class TwitchModule:
|
||||
f"**Game:** {streams.game_name}\n"
|
||||
f"**Viewers:** {streams.viewer_count}"
|
||||
),
|
||||
color=discord.Color.green()
|
||||
color=discord.Color.green(),
|
||||
)
|
||||
embed.set_thumbnail(
|
||||
url=streams.thumbnail_url.replace('{width}', '320').replace('{height}', '180')
|
||||
url=streams.thumbnail_url.replace("{width}", "320").replace(
|
||||
"{height}", "180"
|
||||
)
|
||||
)
|
||||
await interaction.followup.send(embed=embed)
|
||||
logger.info(f"Streamer {streamer} is live.")
|
||||
@ -170,7 +184,7 @@ class TwitchModule:
|
||||
embed=discord.Embed(
|
||||
title=f"{streamer} is not live",
|
||||
description=f"{streamer} is currently offline.",
|
||||
color=discord.Color.red()
|
||||
color=discord.Color.red(),
|
||||
)
|
||||
)
|
||||
logger.info(f"Streamer {streamer} is offline.")
|
||||
|
@ -1,10 +1,12 @@
|
||||
# modules/social/youtube_module.py
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from googleapiclient.discovery import build
|
||||
import logging
|
||||
|
||||
import config
|
||||
import asyncio
|
||||
from modules.data.db import get_connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -13,7 +15,7 @@ logger = logging.getLogger(__name__)
|
||||
class YouTubeModule:
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.youtube = build('youtube', 'v3', developerKey=config.YOUTUBE_API_KEY)
|
||||
self.youtube = build("youtube", "v3", developerKey=config.YOUTUBE_API_KEY)
|
||||
self.bot.loop.create_task(self.check_youtube_channels())
|
||||
self.add_commands()
|
||||
|
||||
@ -21,34 +23,38 @@ class YouTubeModule:
|
||||
while True:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT youtube_channel_id, discord_channel_id FROM followed_youtube_channels')
|
||||
c.execute(
|
||||
"SELECT youtube_channel_id, discord_channel_id FROM followed_youtube_channels"
|
||||
)
|
||||
followed_channels = c.fetchall()
|
||||
conn.close()
|
||||
|
||||
for youtube_channel_id, discord_channel_id in followed_channels:
|
||||
try:
|
||||
request = self.youtube.channels().list(
|
||||
part='contentDetails',
|
||||
id=youtube_channel_id
|
||||
part="contentDetails", id=youtube_channel_id
|
||||
)
|
||||
response = request.execute()
|
||||
if 'items' in response and len(response['items']) > 0:
|
||||
uploads_playlist_id = response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
|
||||
|
||||
if "items" in response and len(response["items"]) > 0:
|
||||
uploads_playlist_id = response["items"][0]["contentDetails"][
|
||||
"relatedPlaylists"
|
||||
]["uploads"]
|
||||
|
||||
request = self.youtube.playlistItems().list(
|
||||
part='snippet',
|
||||
playlistId=uploads_playlist_id,
|
||||
maxResults=1
|
||||
part="snippet", playlistId=uploads_playlist_id, maxResults=1
|
||||
)
|
||||
response = request.execute()
|
||||
if 'items' in response and len(response['items']) > 0:
|
||||
latest_video = response['items'][0]['snippet']
|
||||
video_id = latest_video['resourceId']['videoId']
|
||||
title = latest_video['title']
|
||||
publish_time = latest_video['publishedAt']
|
||||
thumbnail_url = latest_video['thumbnails']['high']['url']
|
||||
if "items" in response and len(response["items"]) > 0:
|
||||
latest_video = response["items"][0]["snippet"]
|
||||
video_id = latest_video["resourceId"]["videoId"]
|
||||
title = latest_video["title"]
|
||||
latest_video["publishedAt"]
|
||||
thumbnail_url = latest_video["thumbnails"]["high"]["url"]
|
||||
|
||||
c.execute('SELECT last_video_id FROM youtube_status WHERE youtube_channel_id = ?', (youtube_channel_id,))
|
||||
c.execute(
|
||||
"SELECT last_video_id FROM youtube_status WHERE youtube_channel_id = ?",
|
||||
(youtube_channel_id,),
|
||||
)
|
||||
row = c.fetchone()
|
||||
last_video_id = row[0] if row else None
|
||||
|
||||
@ -58,15 +64,21 @@ class YouTubeModule:
|
||||
embed = discord.Embed(
|
||||
title=f"New Video from {youtube_channel_id}",
|
||||
description=f"{title}\n[Watch now](https://www.youtube.com/watch?v={video_id})",
|
||||
color=discord.Color.green()
|
||||
color=discord.Color.green(),
|
||||
)
|
||||
embed.set_thumbnail(url=thumbnail_url)
|
||||
await channel.send(embed=embed)
|
||||
|
||||
c.execute('INSERT OR REPLACE INTO youtube_status (youtube_channel_id, last_video_id) VALUES (?, ?)', (youtube_channel_id, video_id))
|
||||
c.execute(
|
||||
"INSERT OR REPLACE INTO youtube_status (youtube_channel_id, last_video_id) VALUES (?, ?)",
|
||||
(youtube_channel_id, video_id),
|
||||
)
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking YouTube channel {youtube_channel_id}: {e}", exc_info=True)
|
||||
logger.error(
|
||||
f"Error checking YouTube channel {youtube_channel_id}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
conn.close()
|
||||
await asyncio.sleep(300) # Check every 5 minutes
|
||||
@ -74,15 +86,19 @@ class YouTubeModule:
|
||||
def add_commands(self):
|
||||
@app_commands.command(
|
||||
name="follow_youtube",
|
||||
description="Follow a YouTube channel to get video updates"
|
||||
description="Follow a YouTube channel to get video updates",
|
||||
)
|
||||
async def follow_youtube(interaction: discord.Interaction, youtube_channel_id: str, channel: discord.TextChannel = None):
|
||||
async def follow_youtube(
|
||||
interaction: discord.Interaction,
|
||||
youtube_channel_id: str,
|
||||
channel: discord.TextChannel = None,
|
||||
):
|
||||
channel = channel or interaction.channel
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute(
|
||||
'INSERT OR REPLACE INTO followed_youtube_channels (youtube_channel_id, discord_channel_id) VALUES (?, ?)',
|
||||
(youtube_channel_id, channel.id)
|
||||
"INSERT OR REPLACE INTO followed_youtube_channels (youtube_channel_id, discord_channel_id) VALUES (?, ?)",
|
||||
(youtube_channel_id, channel.id),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@ -90,25 +106,28 @@ class YouTubeModule:
|
||||
embed=discord.Embed(
|
||||
title="Followed YouTube Channel",
|
||||
description=f"Now following {youtube_channel_id}. Alerts will be sent to {channel.mention}.",
|
||||
color=discord.Color.green()
|
||||
color=discord.Color.green(),
|
||||
)
|
||||
)
|
||||
logger.info(f"Now following {youtube_channel_id} for video updates in {channel.name}")
|
||||
logger.info(
|
||||
f"Now following {youtube_channel_id} for video updates in {channel.name}"
|
||||
)
|
||||
|
||||
@app_commands.command(
|
||||
name="unfollow_youtube",
|
||||
description="Unfollow a YouTube channel"
|
||||
name="unfollow_youtube", description="Unfollow a YouTube channel"
|
||||
)
|
||||
async def unfollow_youtube(interaction: discord.Interaction, youtube_channel_id: str):
|
||||
async def unfollow_youtube(
|
||||
interaction: discord.Interaction, youtube_channel_id: str
|
||||
):
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute(
|
||||
'DELETE FROM followed_youtube_channels WHERE youtube_channel_id = ?',
|
||||
(youtube_channel_id,)
|
||||
"DELETE FROM followed_youtube_channels WHERE youtube_channel_id = ?",
|
||||
(youtube_channel_id,),
|
||||
)
|
||||
c.execute(
|
||||
'DELETE FROM youtube_status WHERE youtube_channel_id = ?',
|
||||
(youtube_channel_id,)
|
||||
"DELETE FROM youtube_status WHERE youtube_channel_id = ?",
|
||||
(youtube_channel_id,),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@ -116,7 +135,7 @@ class YouTubeModule:
|
||||
embed=discord.Embed(
|
||||
title="Unfollowed YouTube Channel",
|
||||
description=f"No longer following {youtube_channel_id}.",
|
||||
color=discord.Color.red()
|
||||
color=discord.Color.red(),
|
||||
)
|
||||
)
|
||||
logger.info(f"No longer following {youtube_channel_id}")
|
||||
|
Reference in New Issue
Block a user