mirror of
https://github.com/Redume/StarBoard.git
synced 2025-04-07 04:05:28 +01:00
feat: Added creation of chat in the database and registration of the number of reactions
This commit is contained in:
parent
799727a405
commit
dfc7a17e27
2 changed files with 113 additions and 0 deletions
27
database/database.py
Normal file
27
database/database.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
config = yaml.safe_load(open("config.yaml", "r"))
|
||||||
|
|
||||||
|
async def pg_con():
|
||||||
|
try:
|
||||||
|
con = await asyncpg.connect(
|
||||||
|
user=config['database']["user"],
|
||||||
|
password=config['database']["password"],
|
||||||
|
database=config['database']["database"],
|
||||||
|
host=config['database']["host"],
|
||||||
|
port=5432
|
||||||
|
)
|
||||||
|
|
||||||
|
await con.set_type_codec(
|
||||||
|
'json',
|
||||||
|
encoder=json.dumps,
|
||||||
|
decoder=json.loads,
|
||||||
|
schema='pg_catalog'
|
||||||
|
)
|
||||||
|
|
||||||
|
return con
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
86
main.py
Normal file
86
main.py
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
from aiogram import Bot, Dispatcher, types
|
||||||
|
from aiogram.filters import ChatMemberUpdatedFilter, IS_MEMBER
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from database.database import pg_con
|
||||||
|
|
||||||
|
config = yaml.safe_load(open('config.yaml'))
|
||||||
|
|
||||||
|
dp = Dispatcher()
|
||||||
|
bot = Bot(config['token'])
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_reaction()
|
||||||
|
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__':
|
||||||
|
asyncio.run(dp.start_polling(bot))
|
Loading…
Add table
Reference in a new issue