diff --git a/.gitignore b/.gitignore index 4a2e9dd..a14d6d0 100644 --- a/.gitignore +++ b/.gitignore @@ -154,6 +154,3 @@ cython_debug/ # PyCharm .idea/ - -# Debugging files -debug.py diff --git a/alembic/versions/632b69cdadde_add_missing_defaults.py b/alembic/versions/632b69cdadde_add_missing_defaults.py deleted file mode 100644 index 0f326a7..0000000 --- a/alembic/versions/632b69cdadde_add_missing_defaults.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Add missing defaults - -Revision ID: 632b69cdadde -Revises: 8c4ad0a1d699 -Create Date: 2022-07-03 16:29:07.387011 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '632b69cdadde' -down_revision = '8c4ad0a1d699' -branch_labels = None -depends_on = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### diff --git a/alembic/versions/8c4ad0a1d699_move_dinks_over_to_bank_add_invested_.py b/alembic/versions/8c4ad0a1d699_move_dinks_over_to_bank_add_invested_.py deleted file mode 100644 index ad56f5e..0000000 --- a/alembic/versions/8c4ad0a1d699_move_dinks_over_to_bank_add_invested_.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Move dinks over to Bank & add invested amount - -Revision ID: 8c4ad0a1d699 -Revises: 0d03c226d881 -Create Date: 2022-07-03 16:27:11.330746 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '8c4ad0a1d699' -down_revision = '0d03c226d881' -branch_labels = None -depends_on = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('bank', schema=None) as batch_op: - batch_op.add_column(sa.Column('invested', sa.BigInteger(), server_default='0', nullable=False)) - - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('bank', schema=None) as batch_op: - batch_op.drop_column('invested') - - # ### end Alembic commands ### diff --git a/database/crud/currency.py b/database/crud/currency.py index 3ef4de0..9fe21e0 100644 --- a/database/crud/currency.py +++ b/database/crud/currency.py @@ -5,7 +5,6 @@ from sqlalchemy.ext.asyncio import AsyncSession from database.crud import users from database.exceptions import currency as exceptions from database.models import Bank -from database.utils.math.currency import rob_upgrade_price, interest_upgrade_price, capacity_upgrade_price NIGHTLY_AMOUNT = 420 @@ -42,57 +41,3 @@ async def claim_nightly(session: AsyncSession, user_id: int): session.add(bank) session.add(nightly_data) await session.commit() - - -async def upgrade_capacity(database_session: AsyncSession, user_id: int) -> int: - """Upgrade capacity level""" - bank = await get_bank(database_session, user_id) - upgrade_price = capacity_upgrade_price(bank.capacity_level) - - # Can't afford this upgrade - if upgrade_price > bank.dinks: - raise exceptions.NotEnoughDinks - - bank.dinks -= upgrade_price - bank.capacity_level += 1 - - database_session.add(bank) - await database_session.commit() - - return bank.capacity_level - - -async def upgrade_interest(database_session: AsyncSession, user_id: int) -> int: - """Upgrade interest level""" - bank = await get_bank(database_session, user_id) - upgrade_price = interest_upgrade_price(bank.interest_level) - - # Can't afford this upgrade - if upgrade_price > bank.dinks: - raise exceptions.NotEnoughDinks - - bank.dinks -= upgrade_price - bank.interest_level += 1 - - database_session.add(bank) - await database_session.commit() - - return bank.interest_level - - -async def upgrade_rob(database_session: AsyncSession, user_id: int) -> int: - """Upgrade rob level""" - bank = await get_bank(database_session, user_id) - upgrade_price = rob_upgrade_price(bank.rob_level) - - # Can't afford this upgrade - if upgrade_price > bank.dinks: - raise exceptions.NotEnoughDinks - - bank.dinks -= upgrade_price - bank.rob_level += 1 - - database_session.add(bank) - await database_session.commit() - - return bank.rob_level diff --git a/database/exceptions/currency.py b/database/exceptions/currency.py index 5710c62..6eab7a1 100644 --- a/database/exceptions/currency.py +++ b/database/exceptions/currency.py @@ -1,6 +1,2 @@ class DoubleNightly(Exception): """Exception raised when claiming nightlies multiple times per day""" - - -class NotEnoughDinks(Exception): - """Exception raised when trying to do something you don't have the Dinks for""" diff --git a/database/models.py b/database/models.py index ef2e439..0bc6370 100644 --- a/database/models.py +++ b/database/models.py @@ -17,17 +17,16 @@ class Bank(Base): bank_id: int = Column(Integer, primary_key=True) user_id: int = Column(BigInteger, ForeignKey("users.user_id")) - dinks: int = Column(BigInteger, server_default="0", nullable=False) - invested: int = Column(BigInteger, server_default="0", nullable=False) + dinks: int = Column(BigInteger, default=0, nullable=False) # Interest rate - interest_level: int = Column(Integer, server_default="1", nullable=False) + interest_level: int = Column(Integer, default=1, nullable=False) # Maximum amount that can be stored in the bank - capacity_level: int = Column(Integer, server_default="1", nullable=False) + capacity_level: int = Column(Integer, default=1, nullable=False) # Maximum amount that can be robbed - rob_level: int = Column(Integer, server_default="1", nullable=False) + rob_level: int = Column(Integer, default=1, nullable=False) user: User = relationship("User", uselist=False, back_populates="bank", lazy="selectin") @@ -68,7 +67,7 @@ class NightlyData(Base): nightly_id: int = Column(Integer, primary_key=True) user_id: int = Column(BigInteger, ForeignKey("users.user_id")) last_nightly: Optional[datetime] = Column(DateTime(timezone=True), nullable=True) - count: int = Column(Integer, server_default="0", nullable=False) + count: int = Column(Integer, default=0, nullable=False) user: User = relationship("User", back_populates="nightly_data", uselist=False, lazy="selectin") @@ -82,7 +81,7 @@ class UforaCourse(Base): name: str = Column(Text, nullable=False, unique=True) code: str = Column(Text, nullable=False, unique=True) year: int = Column(Integer, nullable=False) - log_announcements: bool = Column(Boolean, server_default="0", nullable=False) + log_announcements: bool = Column(Boolean, default=False, nullable=False) announcements: list[UforaAnnouncement] = relationship( "UforaAnnouncement", back_populates="course", cascade="all, delete-orphan", lazy="selectin" diff --git a/database/utils/__init__.py b/database/utils/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/database/utils/math/__init__.py b/database/utils/math/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/database/utils/math/currency.py b/database/utils/math/currency.py deleted file mode 100644 index c6d36d0..0000000 --- a/database/utils/math/currency.py +++ /dev/null @@ -1,25 +0,0 @@ -import math - - -def interest_upgrade_price(level: int) -> int: - """Calculate the price to upgrade your interest level""" - base_cost = 600 - growth_rate = 1.8 - - return math.floor(base_cost * (growth_rate**level)) - - -def capacity_upgrade_price(level: int) -> int: - """Calculate the price to upgrade your capacity level""" - base_cost = 800 - growth_rate = 1.6 - - return math.floor(base_cost * (growth_rate**level)) - - -def rob_upgrade_price(level: int) -> int: - """Calculate the price to upgrade your rob level""" - base_cost = 950 - growth_rate = 1.9 - - return math.floor(base_cost * (growth_rate**level)) diff --git a/didier/cogs/currency.py b/didier/cogs/currency.py index d4910ee..606a910 100644 --- a/didier/cogs/currency.py +++ b/didier/cogs/currency.py @@ -4,11 +4,10 @@ import discord from discord.ext import commands from database.crud import currency as crud -from database.exceptions.currency import DoubleNightly, NotEnoughDinks +from database.exceptions.currency import DoubleNightly from didier import Didier from didier.utils.discord.checks import is_owner from didier.utils.discord.converters import abbreviated_number -from database.utils.math.currency import capacity_upgrade_price, interest_upgrade_price, rob_upgrade_price from didier.utils.types.string import pluralize @@ -35,75 +34,11 @@ class Currency(commands.Cog): mention_author=False, ) - @commands.group(name="bank", aliases=["B"], case_insensitive=True, invoke_without_command=True) + @commands.hybrid_group(name="bank", case_insensitive=True, invoke_without_command=True) async def bank(self, ctx: commands.Context): """Show your Didier Bank information""" async with self.client.db_session as session: - bank = await crud.get_bank(session, ctx.author.id) - - embed = discord.Embed(colour=discord.Colour.blue()) - embed.set_author(name=f"Bank van {ctx.author.display_name}") - embed.set_thumbnail(url=ctx.author.avatar.url) - - embed.add_field(name="Interest level", value=bank.interest_level) - embed.add_field(name="Capaciteit level", value=bank.capacity_level) - embed.add_field(name="Momenteel geïnvesteerd", value=bank.invested, inline=False) - - await ctx.reply(embed=embed, mention_author=False) - - @bank.group(name="Upgrade", aliases=["U", "Upgrades"], case_insensitive=True, invoke_without_command=True) - async def bank_upgrades(self, ctx: commands.Context): - """List the upgrades you can buy & their prices""" - async with self.client.db_session as session: - bank = await crud.get_bank(session, ctx.author.id) - - embed = discord.Embed(colour=discord.Colour.blue()) - embed.set_author(name="Bank upgrades") - - embed.add_field( - name=f"Interest ({bank.interest_level})", value=str(interest_upgrade_price(bank.interest_level)) - ) - embed.add_field( - name=f"Capaciteit ({bank.capacity_level})", value=str(capacity_upgrade_price(bank.capacity_level)) - ) - embed.add_field(name=f"Rob ({bank.rob_level})", value=str(rob_upgrade_price(bank.rob_level))) - - embed.set_footer(text="Didier Bank Upgrade [Categorie]") - - await ctx.reply(embed=embed, mention_author=False) - - @bank_upgrades.command(name="Capacity", aliases=["C"]) - async def bank_upgrade_capacity(self, ctx: commands.Context): - """Upgrade the capacity level of your bank""" - async with self.client.db_session as session: - try: - await crud.upgrade_capacity(session, ctx.author.id) - await ctx.message.add_reaction("⏫") - except NotEnoughDinks: - await ctx.reply("Je hebt niet genoeg Didier Dinks om dit te doen.", mention_author=False) - await self.client.reject_message(ctx.message) - - @bank_upgrades.command(name="Interest", aliases=["I"]) - async def bank_upgrade_interest(self, ctx: commands.Context): - """Upgrade the interest level of your bank""" - async with self.client.db_session as session: - try: - await crud.upgrade_interest(session, ctx.author.id) - await ctx.message.add_reaction("⏫") - except NotEnoughDinks: - await ctx.reply("Je hebt niet genoeg Didier Dinks om dit te doen.", mention_author=False) - await self.client.reject_message(ctx.message) - - @bank_upgrades.command(name="Rob", aliases=["R"]) - async def bank_upgrade_rob(self, ctx: commands.Context): - """Upgrade the rob level of your bank""" - async with self.client.db_session as session: - try: - await crud.upgrade_rob(session, ctx.author.id) - await ctx.message.add_reaction("⏫") - except NotEnoughDinks: - await ctx.reply("Je hebt niet genoeg Didier Dinks om dit te doen.", mention_author=False) - await self.client.reject_message(ctx.message) + await crud.get_bank(session, ctx.author.id) @commands.hybrid_command(name="dinks") async def dinks(self, ctx: commands.Context): diff --git a/didier/cogs/tasks.py b/didier/cogs/tasks.py index 285945c..c37b8b8 100644 --- a/didier/cogs/tasks.py +++ b/didier/cogs/tasks.py @@ -29,9 +29,9 @@ class Tasks(commands.Cog): if settings.UFORA_RSS_TOKEN is None or settings.UFORA_ANNOUNCEMENTS_CHANNEL is None: return - async with self.client.db_session as db_session: + async with self.client.db_session as session: announcements_channel = self.client.get_channel(settings.UFORA_ANNOUNCEMENTS_CHANNEL) - announcements = await fetch_ufora_announcements(self.client.http_session, db_session) + announcements = await fetch_ufora_announcements(session) for announcement in announcements: await announcements_channel.send(embed=announcement.to_embed()) diff --git a/didier/data/embeds/ufora/announcements.py b/didier/data/embeds/ufora/announcements.py index 138837e..52d5849 100644 --- a/didier/data/embeds/ufora/announcements.py +++ b/didier/data/embeds/ufora/announcements.py @@ -3,11 +3,9 @@ from dataclasses import dataclass, field from datetime import datetime from typing import Optional -import async_timeout import discord import feedparser import pytz -from aiohttp import ClientSession from markdownify import markdownify as md from sqlalchemy.ext.asyncio import AsyncSession @@ -118,9 +116,7 @@ def parse_ids(url: str) -> Optional[tuple[int, int]]: return int(spl[0]), int(spl[1]) -async def fetch_ufora_announcements( - http_session: ClientSession, database_session: AsyncSession -) -> list[UforaNotification]: +async def fetch_ufora_announcements(session: AsyncSession) -> list[UforaNotification]: """Fetch all new announcements""" notifications: list[UforaNotification] = [] @@ -128,7 +124,7 @@ async def fetch_ufora_announcements( if settings.UFORA_RSS_TOKEN is None: return notifications - courses = await crud.get_courses_with_announcements(database_session) + courses = await crud.get_courses_with_announcements(session) for course in courses: course_announcement_ids = list(map(lambda announcement: announcement.announcement_id, course.announcements)) @@ -138,9 +134,7 @@ async def fetch_ufora_announcements( ) # Get the updated feed - with async_timeout.timeout(10): - async with http_session.get(course_url) as response: - feed = feedparser.parse(await response.text()) + feed = feedparser.parse(course_url) # Remove old notifications fresh_feed: list[dict] = [] @@ -167,6 +161,6 @@ async def fetch_ufora_announcements( notifications.append(notification) # Create new db entry - await crud.create_new_announcement(database_session, notification_id, course, notification.published_dt) + await crud.create_new_announcement(session, notification_id, course, notification.published_dt) return notifications diff --git a/didier/didier.py b/didier/didier.py index ff26e7a..1138b02 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -1,7 +1,6 @@ import os import discord -from aiohttp import ClientSession from discord.ext import commands from sqlalchemy.ext.asyncio import AsyncSession @@ -15,7 +14,6 @@ class Didier(commands.Bot): """DIDIER <3""" initial_extensions: tuple[str, ...] = () - http_session: ClientSession def __init__(self): activity = discord.Activity(type=discord.ActivityType.playing, name=settings.DISCORD_STATUS_MESSAGE) @@ -45,9 +43,6 @@ class Didier(commands.Bot): await self._load_initial_extensions() await self._load_directory_extensions("didier/cogs") - # Create aiohttp session - self.http_session = ClientSession() - async def _load_initial_extensions(self): """Load all extensions that should be loaded before the others""" for extension in self.initial_extensions: diff --git a/pyproject.toml b/pyproject.toml index a81c771..c97a7dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,8 +30,7 @@ disable = [ "missing-module-docstring", "too-few-public-methods", "too-many-arguments", - "too-many-instance-attributes", - "too-many-locals" + "too-many-instance-attributes" ] [tool.pylint.format]