database and articleview changes

This commit is contained in:
Corban-Lee 2023-07-11 10:52:43 +01:00
parent 7b86ae0434
commit 27d3077f14
5 changed files with 34 additions and 50 deletions

4
.gitignore vendored
View File

@ -1,6 +1,10 @@
# Stores the Bot token
TOKEN
# Databases
*.sqlite
*.db
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

View File

@ -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');

Binary file not shown.

View File

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

View File

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