mirror of
https://github.com/Redume/StarBoard.git
synced 2025-04-07 04:05:28 +01:00
refactor: organized the code into folders
This commit is contained in:
parent
6bba40b745
commit
26a1d87140
3 changed files with 70 additions and 72 deletions
18
events/join_chat.py
Normal file
18
events/join_chat.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from aiogram import types, Router
|
||||||
|
from aiogram.filters import ChatMemberUpdatedFilter, IS_MEMBER
|
||||||
|
|
||||||
|
from database.database import pg_con
|
||||||
|
|
||||||
|
router = Router()
|
||||||
|
|
||||||
|
|
||||||
|
@router.my_chat_member(ChatMemberUpdatedFilter(IS_MEMBER))
|
||||||
|
async def join_chat(event: types.Message):
|
||||||
|
if event.chat.type not in {'group', 'supergroup'}:
|
||||||
|
return
|
||||||
|
|
||||||
|
conn = await pg_con()
|
||||||
|
data = await conn.fetch('SELECT emoji_list FROM chat WHERE chat_id = $1', event.chat.id)
|
||||||
|
|
||||||
|
if len(data) == 0:
|
||||||
|
await conn.execute('INSERT INTO chat (chat_id) VALUES ($1)', event.chat.id)
|
48
events/reactions.py
Normal file
48
events/reactions.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from aiogram import types, Router
|
||||||
|
from database.database import pg_con
|
||||||
|
|
||||||
|
router = Router()
|
||||||
|
|
||||||
|
|
||||||
|
async def update_reaction_count(conn, chat_id, message_id, delta):
|
||||||
|
data_message = await conn.fetchrow(
|
||||||
|
'SELECT reaction_count FROM message WHERE chat_id = $1 AND message_id = $2',
|
||||||
|
chat_id, message_id
|
||||||
|
)
|
||||||
|
|
||||||
|
if data_message is None and delta > 0:
|
||||||
|
await conn.execute(
|
||||||
|
'INSERT INTO message (chat_id, message_id, reaction_count) VALUES ($1, $2, $3)',
|
||||||
|
chat_id, message_id, delta
|
||||||
|
)
|
||||||
|
elif data_message:
|
||||||
|
await conn.execute(
|
||||||
|
'UPDATE message SET reaction_count = reaction_count + $3 WHERE chat_id = $1 AND message_id = $2',
|
||||||
|
chat_id, message_id, delta
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.message_reaction()
|
||||||
|
async def register_message_reaction(event: types.MessageReactionUpdated):
|
||||||
|
if event.chat.type not in {'group', 'supergroup'}:
|
||||||
|
return
|
||||||
|
|
||||||
|
conn = await pg_con()
|
||||||
|
|
||||||
|
data_reaction = await conn.fetchval('SELECT emoji_list FROM chat WHERE chat_id = $1', event.chat.id)
|
||||||
|
if not data_reaction:
|
||||||
|
return
|
||||||
|
|
||||||
|
valid_emojis = set(data_reaction)
|
||||||
|
|
||||||
|
if event.new_reaction:
|
||||||
|
for reaction in event.new_reaction:
|
||||||
|
emoji = reaction.model_dump()['emoji']
|
||||||
|
if emoji in valid_emojis:
|
||||||
|
await update_reaction_count(conn, event.chat.id, event.message_id, 1)
|
||||||
|
|
||||||
|
if event.old_reaction:
|
||||||
|
for reaction in event.old_reaction:
|
||||||
|
emoji = reaction.model_dump()['emoji']
|
||||||
|
if emoji in valid_emojis:
|
||||||
|
await update_reaction_count(conn, event.chat.id, event.message_id, -1)
|
76
main.py
76
main.py
|
@ -1,85 +1,17 @@
|
||||||
from aiogram import Bot, Dispatcher, types
|
from aiogram import Bot, Dispatcher
|
||||||
from aiogram.filters import ChatMemberUpdatedFilter, IS_MEMBER
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from database.database import pg_con
|
from events import join_chat, reactions
|
||||||
|
|
||||||
config = yaml.safe_load(open('config.yaml'))
|
config = yaml.safe_load(open('config.yaml'))
|
||||||
|
|
||||||
dp = Dispatcher()
|
dp = Dispatcher()
|
||||||
bot = Bot(config['token'])
|
bot = Bot(config['token'])
|
||||||
|
|
||||||
|
dp.include_router(router=reactions.router)
|
||||||
@dp.message_reaction()
|
dp.include_router(router=join_chat.router)
|
||||||
async def message_reaction_handler(event: types.MessageReactionUpdated):
|
|
||||||
if event.chat.type != 'group' and event.chat.type != 'supergroup':
|
|
||||||
return
|
|
||||||
|
|
||||||
conn = await pg_con()
|
|
||||||
|
|
||||||
if event.new_reaction is not None and len(event.new_reaction) > 0:
|
|
||||||
|
|
||||||
for n in range(len(event.new_reaction)):
|
|
||||||
data_reaction = await conn.fetchrow('SELECT emoji_list FROM chat WHERE chat_id = $1', event.chat.id)
|
|
||||||
|
|
||||||
|
|
||||||
if data_reaction[0][n] == event.new_reaction[n].model_dump()['emoji']:
|
|
||||||
data_message = await conn.fetchrow(
|
|
||||||
'SELECT reaction_count FROM message '
|
|
||||||
'WHERE chat_id = $1 AND message_id = $2',
|
|
||||||
event.chat.id,
|
|
||||||
event.message_id
|
|
||||||
)
|
|
||||||
|
|
||||||
if data_message is None:
|
|
||||||
await conn.execute(
|
|
||||||
'INSERT INTO message (chat_id, message_id, reaction_count) VALUES ($1, $2, $3)',
|
|
||||||
event.chat.id,
|
|
||||||
event.message_id,
|
|
||||||
1
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await conn.execute('UPDATE message SET reaction_count = reaction_count + 1 '
|
|
||||||
'WHERE chat_id = $1 AND message_id = $2',
|
|
||||||
event.chat.id,
|
|
||||||
event.message_id,
|
|
||||||
)
|
|
||||||
if event.old_reaction is not None and len(event.old_reaction) > 0:
|
|
||||||
|
|
||||||
for n in range(len(event.old_reaction)):
|
|
||||||
data_reaction = await conn.fetchrow('SELECT emoji_list FROM chat WHERE chat_id = $1', event.chat.id)
|
|
||||||
|
|
||||||
if data_reaction[0][n] == event.old_reaction[n].model_dump()['emoji']:
|
|
||||||
data_message = await conn.fetchrow(
|
|
||||||
'SELECT reaction_count FROM message '
|
|
||||||
'WHERE chat_id = $1 AND message_id = $2',
|
|
||||||
event.chat.id,
|
|
||||||
event.message_id
|
|
||||||
)
|
|
||||||
|
|
||||||
if data_message is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
else:
|
|
||||||
await conn.execute('UPDATE message SET reaction_count = reaction_count - 1 '
|
|
||||||
'WHERE chat_id = $1 AND message_id = $2',
|
|
||||||
event.chat.id,
|
|
||||||
event.message_id
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dp.my_chat_member(ChatMemberUpdatedFilter(IS_MEMBER))
|
|
||||||
async def event_channel(channel: types.Message):
|
|
||||||
if channel.chat.type != 'group' and channel.chat.type != 'supergroup':
|
|
||||||
return
|
|
||||||
|
|
||||||
conn = await pg_con()
|
|
||||||
data = await conn.fetch('SELECT emoji_list FROM chat WHERE chat_id = $1', channel.chat.id)
|
|
||||||
|
|
||||||
if len(data) == 0:
|
|
||||||
await conn.execute('INSERT INTO chat (chat_id) VALUES ($1)', channel.chat.id)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Reference in a new issue