Skip to content

Commit

Permalink
feat: switch to db to find truth bullets
Browse files Browse the repository at this point in the history
Hopefully solves #179.
  • Loading branch information
AstreaTSS committed Feb 12, 2024
1 parent 71adfb8 commit 93526aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
20 changes: 20 additions & 0 deletions common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing

import interactions as ipy
from tortoise import connections
from tortoise import fields
from tortoise.contrib.postgres.fields import ArrayField
from tortoise.models import Model
Expand Down Expand Up @@ -92,3 +93,22 @@ class Meta:
ult_detective_role: typing.Optional[int] = fields.BigIntField(null=True)
player_role: typing.Optional[int] = fields.BigIntField(null=True)
bullets_enabled: bool = fields.BooleanField(default=False) # type: ignore


async def find_truth_bullet(
channel_id: ipy.Snowflake_Type, content: str
) -> typing.Optional[TruthBullet]:
conn = connections.get("default")

result = await conn.execute_query(
f"SELECT {', '.join(TruthBullet._meta.fields)} FROM"
f" {TruthBullet.Meta.table} WHERE channel_id=$1 AND ((position(LOWER(name) in"
" LOWER($2)))>0 OR 0 < ANY(SELECT position(LOWER(UNNEST(aliases)) in"
" LOWER($2))));",
[int(channel_id), content],
)

try:
return None if result[0] <= 0 else TruthBullet(**dict(result[1][0]))
except (KeyError, IndexError, ValueError):
return None
16 changes: 3 additions & 13 deletions exts/bullet_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,9 @@ async def on_message(self, event: ipy.events.MessageCreate):
):
return

bullet_found: typing.Optional[models.TruthBullet] = None

channel_id = message.channel.id
content = message.content.lower()
async for bullet in models.TruthBullet.filter(
channel_id=channel_id, found=False
):
if bullet.name.lower() in content or any(
a.lower() in content for a in bullet.aliases
):
bullet_found = bullet
break

bullet_found = await models.find_truth_bullet(
message.channel.id, message.content
)
if not bullet_found:
return

Expand Down

0 comments on commit 93526aa

Please sign in to comment.