Skip to content

Commit

Permalink
- Discord token is changed
Browse files Browse the repository at this point in the history
- A few useful functions
- Work with DB is refactored
- Removed non-stable domain fixers
- Removed non-necessary tree command `status`
  • Loading branch information
soksanichenko committed Jun 7, 2024
1 parent 0019bf8 commit b556d36
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 132 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ repos:
name: PyLint
entry: pylint
language: python
exclude: ".*/alembic/versions/.*"
args:
- '--disable'
- 'R0903' # too-few-public-methods
Expand Down
16 changes: 8 additions & 8 deletions ansible/inventories/zelgray.work/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
discord_token: !vault |
$ANSIBLE_VAULT;1.1;AES256
32636261616562653835323234346535346232363163343362343634343963313261643931386131
3563643830323865373134616661626266366535353836390a356334636132653439373639633161
64373733613134396431376262373962343833616530623639303232333565366265316230303538
3331373630646636360a316239396539626134633365356165636330633231393438663166393463
66343366636238653464383935653564636233373730613961323230333463636236353730386532
35303865636437363765643162396261643136653661626664373031633738663134643863623239
64633837313536393661393066393135383565363364633630356565376532353833616336643462
31313436303837303836
30393738393866646637633661636434333830616532326365646334356363363236363962316338
3934383530376230393437313030646533353461366533330a303361613266636136626361656161
31353333343239316263303536656339353465323166303561366134323135636332386466613637
3763666466633036330a343337663235333938623531346265316262613164636664323461666435
35353231316130623138343439653762353735323763343761306561663064636231393234393835
36663633363234313230323138363934343664356263653732373730633735663561336236383565
64653561666666353665303732303537386262663965643236616464643638386336393739306235
32393732383534633531
db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
37373933613933303839356661356563373461333963316637613935386466643164363162336331
Expand Down
25 changes: 23 additions & 2 deletions sources/lib/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,26 @@ async def get_command(
:param command_name: A name of command
:return: object of Command
"""
commands = await commands_tree.fetch_commands()
return next(iter(filter(lambda c: c.name == command_name, commands)))
cmds = await commands_tree.fetch_commands()
return next(iter(filter(lambda c: c.name == command_name, cmds)))


def get_user_status(
user_id: int,
interaction: discord.Interaction,
) -> discord.Status:
"""Get the status of a user"""
return interaction.guild.get_member(user_id).status


def get_user_activity(
user_id: int,
interaction: discord.Interaction,
) -> discord.Activity:
"""Get the activity of a user"""
return interaction.guild.get_member(user_id).activity


def check_is_guild_owner(interaction: discord.Interaction) -> bool:
"""Check an interaction's user is a guild owner"""
return interaction.guild.owner == interaction.user
54 changes: 54 additions & 0 deletions sources/lib/db/alembic/versions/7b0c01070796_domain_fixers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""domain_fixers
Revision ID: 7b0c01070796
Revises:
Create Date: 2024-06-08 00:20:08.625435
"""

from typing import Sequence, Union
from sqlalchemy.engine.reflection import Inspector

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '7b0c01070796'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
tables = inspector.get_table_names()
# ### commands auto generated by Alembic - please adjust! ###
dropped_tables = (
'bot_options',
'domain_fixers',
)
for dropped_table in dropped_tables:
if dropped_table in tables:
op.drop_table(dropped_table)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
'domain_fixers',
sa.Column('original', sa.TEXT(), autoincrement=False, nullable=False),
sa.Column('fixer', sa.TEXT(), autoincrement=False, nullable=False),
sa.Column('enabled', sa.BOOLEAN(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('original', name='domain_fixers_pkey'),
)
op.create_table(
'bot_options',
sa.Column('id', sa.BIGINT(), autoincrement=True, nullable=False),
sa.Column('name', sa.TEXT(), autoincrement=False, nullable=False),
sa.Column('value', sa.TEXT(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('id', name='bot_options_pkey'),
)
# ### end Alembic commands ###
File renamed without changes.
95 changes: 95 additions & 0 deletions sources/lib/db/crud/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Base operations with any DB table"""

from typing import (
Any,
Type,
)

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from sources.lib.db.models import Base


async def get_db_entity(
db_session: AsyncSession,
table_class: Type[Base],
**kwargs,
):
"""Get a DB entity by the params"""
user = select(table_class).filter_by(**kwargs)
return (await db_session.scalars(user)).one_or_none()


async def create_db_entity(db_session: AsyncSession, table_class: Type[Base], **kwargs):
"""Add a new DB entity"""
db_entity = table_class(
**kwargs,
)
db_session.add(db_entity)
await db_session.commit()


async def update_db_entity(
db_session: AsyncSession,
db_entity: Type[Base],
**kwargs,
):
"""Update an existing DB entity"""
for key, value in kwargs.items():
setattr(db_entity, key, value)
db_session.add(db_entity)
await db_session.commit()


async def delete_db_entity(
db_session: AsyncSession,
db_entity: Type[Base],
):
"""Delete an existing DB entity"""
await db_session.delete(db_entity)


async def delete_db_entity_if_exists(
db_session: AsyncSession,
table_class: Type[Base],
**kwargs,
):
"""Delete a DB entity if it exists"""
db_entity = await get_db_entity(
db_session=db_session,
table_class=table_class,
**kwargs,
)
if db_entity is not None:
await delete_db_entity(
db_session=db_session,
db_entity=db_entity,
)


async def update_db_entity_or_create(
db_session: AsyncSession,
table_class: Type[Base],
filters: dict[str, Any],
updates: dict[str, Any],
):
"""Update a DB entity if it exists or create if it doesn't exist"""
db_entity = await get_db_entity(
db_session=db_session,
table_class=table_class,
**filters,
)
if db_entity is None:
filters.update(updates)
await create_db_entity(
db_session=db_session,
table_class=table_class,
**filters,
)
else:
await update_db_entity(
db_session=db_session,
db_entity=db_entity,
**updates,
)
30 changes: 0 additions & 30 deletions sources/lib/db/operations/guilds.py

This file was deleted.

57 changes: 0 additions & 57 deletions sources/lib/db/operations/users.py

This file was deleted.

2 changes: 0 additions & 2 deletions sources/lib/on_message/domains_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def fix_urls(message: discord.Message) -> str:
"""
domains = {
"reddit.com": "rxddit",
"tiktok.com": "vxtiktok",
"x.com": "fixupx",
"twitter.com": "fxtwitter",
"instagram.com": "ddinstagram",
}

msg_content_lines = message.content.split()
Expand Down
Loading

0 comments on commit b556d36

Please sign in to comment.