def getUserVoteCount(vid: int, choice: int = None, uid: int = None) -> Union[int, list]: if choice is None: if uid is None: return db.executeFAll( "SELECT O.OptionNumb, COALESCE(T.Count, 0) AS Count FROM Options O LEFT JOIN (" " SELECT O2.OptionNumb AS Numb, COUNT(*) AS Count " " FROM Options O2 JOIN UserVote UV ON (UV.VoteID = O2.VoteID AND UV.Choice = O2.OptionNumb) " " WHERE O2.VoteID = %s GROUP BY O2.OptionNumb" ") T ON O.OptionNumb = T.Numb WHERE O.VoteID = %s ORDER BY Count DESC;", vid, vid) else: vs = db.executeF1( "SELECT COUNT(*) FROM UserVote WHERE VoteID = %s AND UserID = %s;", vid, uid) else: if uid is None: vs = db.executeF1( "SELECT COUNT(*) FROM UserVote WHERE VoteID = %s AND Choice = %s;", vid, choice) else: vs = db.executeF1( "SELECT COUNT(*) FROM UserVote WHERE VoteID = %s AND UserID = %s AND Choice = %s;", vid, uid, choice ) # Of questionable usefulness, but present for completeness if vs: return vs[0] else: return 0
def addVote(creator: discord.User, question: str, options: list[str], limit: int, guild: discord.Guild, channel: discord.TextChannel, stage: int = 0, type: int = 1, num_win: int = 1, title_pre: str = "Poll") -> tuple[int, str]: # noinspection SyntaxError vid = db.executeF1( "INSERT INTO Votes (CreatorID, Question, VoteLimit, GuildID, ChannelID, PollStage, Type, NumWinners) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING VoteID;", creator.id, question, limit, guild.id, channel.id, stage, type, num_win)[0] title = f"{title_pre} `{vid}`: " + question db.execute("UPDATE Votes SET Question = %s WHERE VoteID = %s", title, vid) db.multiexec( "INSERT INTO Options (VoteID, OptionNumb, Prompt) VALUES (%s, %s, %s);", ((vid, i, p) for i, p in enumerate(options))) return vid, title
def getMsgVote(mid: int): return db.executeF1( "SELECT M.VoteID, Part, Type, VoteLimit, PollStage " "FROM VoteMessages M JOIN Votes V USING (VoteID) WHERE MessageID = %s;", mid)
def getVote(vid: int): return db.executeF1( "SELECT CreatorID, Question, GuildID, ChannelID, Type, NumWinners " "FROM Votes WHERE VoteID = %s;", vid)
def getVoterCount(vid: int): return db.executeF1( "SELECT COUNT(DISTINCT UserID) FROM Votes V JOIN UserVote UV USING (VoteID) WHERE VoteID = %s", vid)[0]
def getPrefix(gid: int): prefix = db.executeF1( "SELECT COALESCE(Prefix, '+') FROM Prefix WHERE GuildID = %s;", gid) if not prefix: return "+" return prefix[0]