|
| 1 | +import disnake |
| 2 | +import datetime as dt |
| 3 | +from disnake.ext import commands, tasks |
| 4 | + |
| 5 | +from utils.bot import OGIROID |
| 6 | + |
| 7 | + |
| 8 | +class Lewis(commands.Cog, name="Lewis"): |
| 9 | + def __init__(self, bot: OGIROID): |
| 10 | + self.bot = bot |
| 11 | + self.upload_check.start() |
| 12 | + self.youtube_channel_id = "UCWI-ohtRu8eEeDj93hmUsUQ" |
| 13 | + self.youtube_channel_url = f"https://www.youtube.com/channel/{self.youtube_channel_id}" |
| 14 | + self.youtube_api_key = self.bot.config.tokens.yt_api_key |
| 15 | + self.youtube_api_url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={self.youtube_channel_id}&maxResults=1&order=date&type=video&key={self.youtube_api_key}" |
| 16 | + |
| 17 | + def cog_unload(self): |
| 18 | + self.upload_check.cancel() |
| 19 | + |
| 20 | + @tasks.loop(minutes=30) |
| 21 | + async def upload_check(self): |
| 22 | + if self.youtube_api_key is None: |
| 23 | + print("No YouTube API key found, skipping upload check") |
| 24 | + return |
| 25 | + check_time = dt.datetime.utcnow() |
| 26 | + channel = self.bot.get_channel(self.bot.config.channels.uploads) |
| 27 | + if channel is None: |
| 28 | + return |
| 29 | + |
| 30 | + response = await self.bot.session.get(self.youtube_api_url) |
| 31 | + response = await response.json() |
| 32 | + |
| 33 | + video_id = response["items"][0]["id"]["videoId"] |
| 34 | + video_url = f"https://www.youtube.com/watch?v={video_id}" |
| 35 | + # Credits to Simon for the idea and part of the code |
| 36 | + |
| 37 | + video_release_time = response["items"][0]["snippet"]["publishedAt"] |
| 38 | + year = video_release_time.split("-")[0] |
| 39 | + month = video_release_time.split("-")[1] |
| 40 | + day = video_release_time.split("-")[2].split("T")[0] |
| 41 | + hour = video_release_time.split("T")[1].split(":")[0] |
| 42 | + minute = video_release_time.split("T")[1].split(":")[1] |
| 43 | + second = video_release_time.split("T")[1].split(":")[2].split("Z")[0] |
| 44 | + time = dt.datetime(int(year), int(month), int(day), int(hour), int(minute), int(second)) |
| 45 | + if check_time - time < dt.timedelta(minutes=30): |
| 46 | + return await channel.send(f"Hey, Lewis posted a new video! <@&{self.bot.config.roles.yt_announcements}>\n{video_url}") |
| 47 | + else: |
| 48 | + return |
| 49 | + |
| 50 | + |
| 51 | +def setup(bot): |
| 52 | + bot.add_cog(Lewis(bot)) |
0 commit comments