diff --git a/.gitignore b/.gitignore index 0e26039..5d1d7d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ # Stores the Bot token TOKEN +# Databases +*.sqlite +*.db + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/db/build.sql b/db/build.sql deleted file mode 100644 index ffe3f2f..0000000 --- a/db/build.sql +++ /dev/null @@ -1,42 +0,0 @@ - -/* - Server Channels -*/ -CREATE TABLE IF NOT EXISTS 'server_channels' ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - channel_id INTEGER NOT NULL, - news_category_id INTEGER NOT NULL, - active INTEGER NOT NULL, - FOREIGN KEY (news_category_id) REFERENCES 'news_categories' (id) - ON DELETE CASCADE -); - - -/* - News Articles -*/ -CREATE TABLE IF NOT EXISTS 'news_articles' ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - url TEXT NOT NULL, - server_channel_id INTEGER NOT NULL, - FOREIGN KEY (server_channel_id) REFERENCES 'server_channels' (id) - ON DELETE CASCADE -); - - -/* - News Categories -*/ -CREATE TABLE IF NOT EXISTS 'news_categories' ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT UNIQUE NOT NULL -); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('all'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('world'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('uk'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('north_america'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('entertainment'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('business'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('tech'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('science'); -INSERT OR IGNORE INTO 'news_categories' (name) VALUES ('top_stories'); diff --git a/db/db.sqlite b/db/db.sqlite deleted file mode 100644 index c8dfd5f..0000000 Binary files a/db/db.sqlite and /dev/null differ diff --git a/src/db/db.py b/src/db/db.py index 292cefa..5a94065 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -7,10 +7,9 @@ import logging from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker -DB_PATH = "db/db.sqlite" -BUILD_PATH = "db/build.sql" -DATABASE_URL = "sqlite:///db/db.sqlite" -DATABASE_ASYNC_URL = "sqlite+aiosqlite:///db/db.sqlite" +DB_PATH = "sqlite.db" +DATABASE_URL = "sqlite:///sqlite.db" +DATABASE_ASYNC_URL = "sqlite+aiosqlite:///sqlite.db" log = logging.getLogger(__name__) diff --git a/src/extensions/news.py b/src/extensions/news.py index 13e3e1c..307c00c 100644 --- a/src/extensions/news.py +++ b/src/extensions/news.py @@ -43,6 +43,27 @@ class NewsStoryType: published_parsed: datetime +class ArticleView(discord.ui.View): + + def __init__(self, story: NewsStoryType): + super().__init__(timeout=None) + + self.story = story + self.add_item(discord.ui.Button( + label="Visit Article", + url=story.link, + style=discord.ButtonStyle.url + )) + + # @discord.ui.button(label="Read More") + async def read_more(self, inter: Interaction, button: discord.Button): + """ + + """ + + await inter.response.send_message(self.story.summary_detail) + + class NewsCog(commands.Cog): """ News cog. @@ -149,7 +170,8 @@ class NewsCog(commands.Cog): for story in stories: embed = await self.story_to_embed(story, category.name) - await inter.followup.send(embed=embed) + view = ArticleView(story) + await inter.followup.send(embed=embed, view=view) async def _get_or_fetch_channel(self, channel_id: int) -> discord.TextChannel: """ @@ -229,10 +251,11 @@ class NewsCog(commands.Cog): return story_embed = await self.story_to_embed(story, category.name) + story_view = ArticleView(story) for channel in server_channels: - await send_or_ignore(channel.channel_id, story_embed, story.link) + await send_or_ignore(channel.channel_id, story_embed, story_view, story.link) - async def send_or_ignore(channel_id: int, story_embed: discord.Embed, story_link: str): + async def send_or_ignore(channel_id: int, story_embed: discord.Embed, story_view: ArticleView, story_link: str): """ Send (or don't) the given `story_embed` to a discord channel with the matching `channel_id`. The embed will be sent if a matching `story_link` has not already been sent to the channel. @@ -246,7 +269,7 @@ class NewsCog(commands.Cog): await flag_url_as_sent(channel_id, story_link) channel = await self._get_or_fetch_channel(channel_id) - await channel.send(embed=story_embed) + await channel.send(embed=story_embed, view=story_view) async def flag_url_as_sent(channel_id: int, story_link: str): """