feat: Add vote functionality.

This commit is contained in:
Lemon4ksan
2025-01-22 19:30:37 +03:00
parent 18ff925236
commit 859d60de35
8 changed files with 278 additions and 152 deletions

View File

@@ -2,7 +2,7 @@ from .base import BaseGuildsDatabase, BaseUsersDatabase
from .extensions import VoiceGuildsDatabase
from .user import User, ExplicitUser
from .guild import Guild, ExplicitGuild
from .guild import Guild, ExplicitGuild, MessageVotes
__all__ = [
'BaseGuildsDatabase',
@@ -12,4 +12,5 @@ __all__ = [
'ExplicitUser',
'Guild',
'ExplicitGuild',
'MessageVotes'
]

View File

@@ -5,8 +5,8 @@ from typing import cast
from pymongo import MongoClient
from pymongo.collection import Collection
from MusicBot.database.user import User, ExplicitUser
from MusicBot.database.guild import Guild, ExplicitGuild
from .user import User, ExplicitUser
from .guild import Guild, ExplicitGuild, MessageVotes
client: MongoClient = MongoClient("mongodb://localhost:27017/")
users: Collection[ExplicitUser] = client.YandexMusicBot.users
@@ -91,14 +91,14 @@ class BaseGuildsDatabase:
is_stopped=True,
allow_explicit=True,
always_allow_menu=False,
vote_add=True,
vote_next_track=True,
vote_add_track=True,
vote_add_album=True,
vote_add_artist=True,
vote_add_playlist=True,
shuffle=False,
repeat=False
repeat=False,
votes={}
))
def update(self, gid: int, data: Guild) -> None:
@@ -135,14 +135,14 @@ class BaseGuildsDatabase:
is_stopped=True,
allow_explicit=True,
always_allow_menu=False,
vote_add=True,
vote_next_track=True,
vote_add_track=True,
vote_add_album=True,
vote_add_artist=True,
vote_add_playlist=True,
shuffle=False,
repeat=False
repeat=False,
votes={}
)
for field, default_value in fields.items():
if field not in existing_fields and field != '_id':
@@ -151,3 +151,14 @@ class BaseGuildsDatabase:
return guild
def update_vote(self, gid: int, mid: int, data: MessageVotes) -> None:
"""Update vote for a message in a guild.
Args:
gid (int): Guild id.
mid (int): Message id.
vote (bool): Vote value.
"""
guild = self.get_guild(gid)
guild['votes'][str(mid)] = data
guilds.update_one({'_id': gid}, {"$set": {'votes': guild['votes']}})

View File

@@ -1,4 +1,11 @@
from typing import TypedDict, Any
from typing import TypedDict, Literal, Any
class MessageVotes(TypedDict):
positive_votes: list[int]
negative_votes: list[int]
total_members: int
action: Literal['next', 'add_track', 'add_album', 'add_artist', 'add_playlist']
vote_content: dict[str, Any] | list[dict[str, Any]] | None
class Guild(TypedDict, total=False):
next_tracks: list[dict[str, Any]]
@@ -8,7 +15,6 @@ class Guild(TypedDict, total=False):
is_stopped: bool
allow_explicit: bool
always_allow_menu: bool
vote_add: bool
vote_next_track: bool
vote_add_track: bool
vote_add_album: bool
@@ -16,6 +22,7 @@ class Guild(TypedDict, total=False):
vote_add_playlist: bool
shuffle: bool
repeat: bool
votes: dict[str, MessageVotes]
class ExplicitGuild(TypedDict):
_id: int
@@ -26,11 +33,11 @@ class ExplicitGuild(TypedDict):
is_stopped: bool # Prevents the `after` callback of play_track
allow_explicit: bool
always_allow_menu: bool
vote_add: bool
vote_next_track: bool
vote_add_track: bool
vote_add_album: bool
vote_add_artist: bool
vote_add_playlist: bool
shuffle: bool
repeat: bool
repeat: bool
votes: dict[str, MessageVotes]