async def postleaderboard(self, ctx, *args): """Shows posts with most karma on this server!""" currentguild = str(ctx.message.guild.id) User = Query() if not args: result = post.search(Query()['servers'] == currentguild) # "Result" is just the entire database. else: if ctx.message.mentions: valor = str(ctx.message.mentions[0].id) print(valor) result = post.search((User.servers == currentguild) & (User.username == valor)) else: result = post.search(Query()['servers'] == currentguild) # Defaults to every post. (?plb sfw, eg.) leaderboard = {} # Prepares an empty dictionary. j = 0 for x in result: # For each entry in the database: leaderboard[j] = [int(x.get("points")), str(x.get("content")), str(x.get("username")), str(x.get("embed")), str(x.get("servers")), str(x.get("stars")), str(x.get("nsfw")), str(x.get("richembed"))] # ...save the user's ID and its amount of points in a new Python database. j = j+1 leaderboard = sorted(leaderboard.items(), key = lambda x : x[1][0], reverse=True) page = 1 await printLeaderboard(page, leaderboard, self, ctx, ctx.message, ctx, args, False)
async def globalpostleaderboard(self, ctx, *args): """Check the toppest posts on every guild!""" if not args: result = post.all() # "Result" is just the entire database. else: if ctx.message.mentions: valor = str(ctx.message.mentions[0].id) print(valor) result = post.search(Query()['username'] == valor) else: result = post.all() # Defaults to every post. (?gplb nsfw, eg.) leaderboard = {} # Prepares an empty dictionary. j = 0 for x in result: # For each entry in the database: leaderboard[j] = [int(x.get("points")), str(x.get("content")), str(x.get("username")), str(x.get("embed")), str(x.get("servers")), str(x.get("stars")), str(x.get("nsfw")), str(x.get("richembed"))] # ...save the user's ID and its amount of points in a new Python database. j = j+1 leaderboard = sorted(leaderboard.items(), key = lambda x : x[1][0], reverse=True) page = 1 await printLeaderboard(page, leaderboard, self, ctx, ctx.message, ctx, args, True)
async def on_reaction_add(self, reaction, user): value = user.id User = Query() if ((user.id != reaction.message.author.id) or (debug == True)) and not user.bot: if isinstance(reaction.emoji, str): # ------------------------- # REACTION = :WASTEBASKET: # (leaderboards) # ------------------------- if reaction.emoji == '🗑️': await deleteMessages(reaction) # ------------------------- # REACTION = :ARROW_RIGHT: # (leaderboards) # ------------------------- if reaction.emoji == '➡️': result = dm.get(Query()['id'] == reaction.message.id) page = result.get('page') + 1 currentguild = str(reaction.message.guild.id) isGlobal = result.get('global') print(isGlobal) if (isGlobal == True): result = post.all() else: result = post.search(Query()['servers'] == currentguild) leaderboard = {} # Prepares an empty dictionary. j = 0 for x in result: # For each entry in the database: leaderboard[j] = [int(x.get("points")), str(x.get("content")), str(x.get("username")), str(x.get("embed")), str(x.get("servers")), str(x.get("stars")), str(x.get("nsfw")), str(x.get("richembed"))] # ...save the user's ID and its amount of points in a new Python database. j = j+1 leaderboard = sorted(leaderboard.items(), key = lambda x : x[1][0], reverse=True) await printLeaderboard(page, leaderboard, self, reaction.message.guild, reaction.message, reaction.message.channel, False, isGlobal) await deleteMessages(reaction) # ------------------------- # REACTION = :ARROW_LEFT: # (leaderboards) # ------------------------- if reaction.emoji == '⬅️': result = dm.get(Query()['id'] == reaction.message.id) page = result.get('page') - 1 currentguild = str(reaction.message.guild.id) isGlobal = result.get('global') if (isGlobal == True): result = post.all() else: result = post.search(Query()['servers'] == currentguild) leaderboard = {} # Prepares an empty dictionary. j = 0 for x in result: # For each entry in the database: leaderboard[j] = [int(x.get("points")), str(x.get("content")), str(x.get("username")), str(x.get("embed")), str(x.get("servers")), str(x.get("stars")), str(x.get("nsfw")), str(x.get("richembed"))] # ...save the user's ID and its amount of points in a new Python database. j = j+1 leaderboard = sorted(leaderboard.items(), key = lambda x : x[1][0], reverse=True) await printLeaderboard(page, leaderboard, self, reaction.message.guild, reaction.message, reaction.message.channel, False, isGlobal) await deleteMessages(reaction) # ------------------------- # REACTION = :BOMB: # (Delete User Data) # ------------------------- if reaction.emoji == "💣": if isinstance(reaction.message.channel, discord.channel.DMChannel): if (reaction.message.embeds[0].title == "Are you SURE?!"): checkM = loadingEmoji = self.client.get_emoji(660250625107296256) react = await reaction.message.add_reaction(checkM) priv.remove(where('username') == user.id) priv.clear_cache() post.remove(where('username') == str(user.id)) post.clear_cache() db.remove(where('username') == str(user.id)) db.clear_cache() botid = self.client.user await reaction.message.remove_reaction(checkM, botid) await reaction.message.channel.send("**Done.** Your Privacy Settings, all of your Comments and your personal data (such as servers and Karma) has been deleted.\nDo note that, by interacting with Reto functions, you may be re-introduced into the database.\n*Thank you for using Reto!*")
async def reactionAdded(bot, payload): if payload.guild_id is None: return User = Query() userid = payload.user_id # Misc. definitions. # Attempt a get_x command for brevity. If that doesn't work, use fetch_x (slow). user = bot.get_user(payload.user_id) if not user: user = await bot.fetch_user(payload.user_id) print("User not found. Trying to fetch it...") channel = bot.get_channel(payload.channel_id) if not channel: channel = await bot.fetch_channel(payload.channel_id) print("Channel not found. Trying to fetch it...") guild = bot.get_guild(payload.guild_id) if not guild: guild = await bot.fetch_guild(payload.guild_id) print("Guild not found. Trying to fetch it...") member = guild.get_member(payload.user_id) if not member: member = await guild.fetch_member(payload.user_id) print("Member not found. Trying to fetch it...") # no such thing as "get_message" message = await channel.fetch_message(payload.message_id) if ((userid != message.author.id) or (debug == True)) and not user.bot: if not isinstance(payload.emoji, str): # ------------------------- # REACTION = :10: # ------------------------- channel = message.channel is_nsfw = channel.is_nsfw() if payload.emoji.name == '10': if discord.utils.get(member.roles, name="Curator") is None: await message.remove_reaction(payload.emoji, user) else: channel = message.channel messageurl = "https://discordapp.com/channels/" + str( message.guild.id) + "/" + str( message.channel.id) + "/" + str(message.id) # Post the message in #best-of contenido = message.content autor = message.author.name foto = message.author.avatar_url if (len(message.attachments) > 0): imagen = message.attachments[0].url color = "" # If there's an embed, set the color of the new embed to it. (first come, first serve) if (message.embeds): embed = message.embeds[0].to_dict() if "color" in embed: color = embed['color'] if color: emberino = discord.Embed(description=contenido, color=color) else: emberino = discord.Embed(description=contenido) emberino.set_author(name=autor, url=messageurl, icon_url=foto) if (len(message.attachments) > 0): emberino.set_image(url=imagen) # Parsing embeds: if (message.embeds): for embed in message.embeds: embed = embed.to_dict() thisEmbed = emberino.to_dict() if (len(message.attachments) == 0) and not "image" in thisEmbed: if "thumbnail" in embed: emberino.set_image( url=embed['thumbnail']['url']) if "image" in embed: emberino.set_image( url=embed['image']['url']) if not "footer" in thisEmbed: if "footer" in embed: emberino.set_footer( text=embed['footer']['text']) else: footer = "" if "provider" in embed: footer = embed['provider']['name'] if "author" in embed and not "title" in embed: footer = embed['author']['name'] if footer: emberino.set_footer(text=footer) title = "" description = "" if "title" in embed: title = embed['title'] elif "author" in embed: title = embed['author']['name'] if "description" in embed: description = embed['description'] if title and description: emberino.add_field(name=title, value=description, inline=False) if "fields" in embed: for field in embed["fields"]: emberino.add_field(name=field['name'], value=field['value'], inline=False) # the difficult challenge of finding the channel to post to best.clear_cache() server = str(message.guild.id) channel = best.search(Query().serverid == server) valuetwo = str(message.id) postexists = post.search(Query().msgid == valuetwo) if postexists: postexists = int(postexists[0]['stars']) else: postexists = 0 if (postexists == 0): try: channel = channel[0][ 'channelid'] # channel id of best-of channel channel = discord.utils.get(message.guild.channels, id=channel) if channel == None: channel = discord.utils.get( message.guild.channels, name="best-of") if channel == None: # if the bot doesn't find a channel named best-of, the channnel has been deleted. create a new one! await message.guild.create_text_channel( 'best-of') channel = discord.utils.get( message.guild.channels, name="best-of") best.upsert( { 'serverid': server, 'channelid': channel.id, 'notification': "message" }, Query().serverid == server) channelformsg = message.channel await channelformsg.send( "The *Best Of* channel doesn't exist, if the bot has permissions it has been created." ) channel = best.search( Query().serverid == server) channel = channel[0]['channelid'] channel = discord.utils.get( message.guild.channels, id=channel) else: # if the bot does find a channel named best-of, the channel needs to be linked to the new db. # this is for legacy users (1.3.5 and below) best.upsert( { 'serverid': server, 'channelid': channel.id, 'notification': "message" }, Query().serverid == server) channel = best.search( Query().serverid == server) channel = channel[0]['channelid'] channel = discord.utils.get( message.guild.channels, id=channel) except IndexError: channel = discord.utils.get(message.guild.channels, name="best-of") if channel == None: # if the bot doesn't find a channel named best-of, the channnel has been deleted. create a new one! await message.guild.create_text_channel( 'best-of') channel = discord.utils.get( message.guild.channels, name="best-of") best.upsert( { 'serverid': server, 'channelid': channel.id, 'notification': "message" }, Query().serverid == server) channelformsg = message.channel await channelformsg.send( "The *Best Of* channel doesn't exist, if the bot has permissions it has been created." ) channel = best.search( Query().serverid == server) channel = channel[0]['channelid'] channel = discord.utils.get( message.guild.channels, id=channel) else: # if the bot does find a channel named best-of, the channel needs to be linked to the new db. # this is for legacy users (1.3.5 and below) best.upsert( { 'serverid': server, 'channelid': channel.id, 'notification': "message" }, Query().serverid == server) channel = best.search( Query().serverid == server) channel = channel[0]['channelid'] channel = discord.utils.get( message.guild.channels, id=channel) # Add user to the points table value = str(message.author.id) exists = db.count(Query().username == value) server = str(message.guild.id) if exists == 0: db.insert({ 'username': value, 'points': 10, 'servers': [server] }) else: User = Query() serverid = str(message.guild.id) existsserver = db.count( (User.servers.any([serverid])) & (User.username == value)) # no funciona if existsserver == 0: db.update(add('points', 10), where('username') == value) l = str(db.search((User.username == value))) if "servers" not in l: docs = db.search(User.username == value) for doc in docs: doc['servers'] = [str(server)] db.write_back(docs) else: db.update(add('servers', [server]), where('username') == value) else: db.update(add('points', 10), where('username') == value) # Finally, the bot sends the message to the Best-Of channel. if channel == None: channelformsg = message.channel await sendErrorEmbed( channelformsg, "The channel couldn't be sent to the Best Of channel, for some reason. Could you double-check it exists?" ) else: if (postexists == 0): await channel.send(embed=emberino) # Log post for post leaderboard priv.clear_cache() privSettings = priv.search( Query().username == message.author.id) if privSettings: privSettings = privSettings[0] username = str(message.author.id) notifmode = best.search(Query().serverid == server) notifmode = notifmode[0]['notification'] # ISO 8601! curdate = str(datetime.now()) if postexists == 0: if not privSettings or "mode" in privSettings and privSettings[ 'mode'] == False or not "mode" in privSettings: attachments = "" if (len(message.attachments) > 0): attachments = message.attachments[0].url if (message.embeds): richembeds = [None] * len(message.embeds) i = 0 for embed in message.embeds: richembeds[i] = embed.to_dict() i = i + 1 else: richembeds = "" post.insert({ 'msgid': valuetwo, 'username': username, 'points': 10, 'servers': server, 'content': message.content, 'embed': attachments, 'richembed': richembeds, 'voters': [user.id], 'stars': 1, 'nsfw': is_nsfw, 'timestamp': curdate }) else: print("Privacy Mode ENABLED!") else: post.update(add('points', 10), where('msgid') == valuetwo) post.update(add('voters', [user.id]), where('msgid') == valuetwo) post.update(add('stars', 1), where('msgid') == valuetwo) if (notifmode != "reaction") and (notifmode != "disabled"): channel = message.channel result = db.get(Query()['username'] == value) send = await channel.send( "Huzzah! **{}**'s post was so good it got starred more than once. They now have {} points. (+10)" .format(message.author.name, result.get('points'))) # Send a confirmation message channel = message.channel result = db.get(Query()['username'] == value) bestofname = best.search(Query().serverid == server) bestofname = bestofname[0]['channelid'] bestofname = discord.utils.get(message.guild.channels, id=bestofname) checkM = bot.get_emoji(660217963911184384) if notifmode == "reaction": react = await message.add_reaction(checkM) if (notifmode != "reaction") and ( notifmode != "disabled") and (postexists == 0): send = await channel.send( "Congrats, **{}**! Your post will be forever immortalized in the **#{}** channel. You now have {} points. (+10)" .format(message.author.name, bestofname.name, result.get('points'))) # Delete said message if notifmode == "reaction": await asyncio.sleep(1) botid = bot.user await message.remove_reaction(checkM, botid) if (notifmode != "reaction") and (notifmode != "disabled"): await asyncio.sleep(3) await send.delete() # ------------------------- # REACTION = :PLUS: # ------------------------- if payload.emoji.name == 'plus': botchannel = discord.utils.get(message.guild.text_channels, name="bot-commands") channel = message.channel # Add user to the points table if channel != botchannel: value = str(message.author.id) exists = db.count(Query().username == value) server = str(message.guild.id) if exists == 0: db.insert({ 'username': value, 'points': 1, 'servers': [server] }) # Send a confirmation message or reaction notifmode = best.search(Query().serverid == server) notifmode = notifmode[0]['notification'] checkM = bot.get_emoji(660217963911184384) if notifmode == "reaction": react = await message.add_reaction(checkM) if (notifmode != "reaction") and (notifmode != "disabled"): result = db.get(Query()['username'] == value) heart = await channel.send( "**Thanked!** {} now has {} thanks. (+1)". format(message.author.name, result.get('points'))) if notifmode == "reaction": await asyncio.sleep(1) botid = bot.user await message.remove_reaction(checkM, botid) if (notifmode != "reaction") and (notifmode != "disabled"): await asyncio.sleep(3) await heart.delete() else: User = Query() serverid = str(message.guild.id) existsserver = db.count( (User.servers.any([serverid])) & (User.username == value)) # no funciona print(str(existsserver)) if existsserver == 0: db.update(add('points', 1), where('username') == value) l = str(db.search((User.username == value))) print(l) if "servers" not in l: docs = db.search(User.username == value) for doc in docs: doc['servers'] = [str(server)] db.write_back(docs) else: db.update(add('servers', [server]), where('username') == value) else: db.update(add('points', 1), where('username') == value) # Log post for post leaderboard priv.clear_cache() privSettings = priv.search( Query().username == message.author.id) if privSettings: privSettings = privSettings[0] valuetwo = str(message.id) username = str(message.author.id) postexists = post.count(Query().msgid == valuetwo) # ISO 8601! curdate = str(datetime.now()) if postexists == 0: if not privSettings or "mode" in privSettings and privSettings[ 'mode'] == False or not "mode" in privSettings: attachments = "" if (len(message.attachments) > 0): attachments = message.attachments[0].url if (message.embeds): richembeds = [None] * len(message.embeds) i = 0 for embed in message.embeds: richembeds[i] = embed.to_dict() i = i + 1 else: richembeds = "" post.insert({ 'msgid': valuetwo, 'username': username, 'points': 1, 'servers': server, 'content': message.content, 'embed': attachments, 'richembed': richembeds, 'voters': [user.id], 'stars': 0, 'nsfw': is_nsfw, 'timestamp': curdate }) else: print("Privacy Mode ENABLED!") else: post.update(add('points', 1), where('msgid') == valuetwo) post.update(add('voters', [user.id]), where('msgid') == valuetwo) best.clear_cache() notifmode = best.search(Query().serverid == server) notifmode = notifmode[0]['notification'] checkM = bot.get_emoji(660217963911184384) if notifmode == "reaction": react = await message.add_reaction(checkM) if (notifmode != "reaction") and (notifmode != "disabled"): result = db.get(Query()['username'] == value) heart = await channel.send( "**Thanked!** {} now has {} thanks. (+1)". format(message.author.name, result.get('points'))) # Delete said message if notifmode == "reaction": await asyncio.sleep(1) botid = bot.user await message.remove_reaction(checkM, botid) if (notifmode != "reaction") and (notifmode != "disabled"): await asyncio.sleep(3) await heart.delete() else: print("in bot-commands cannot read")
async def getProfile(author, ctx, self): valor = str(author) searchvalor = str(author.id) result = db.get(Query()['username'] == searchvalor) # # CONSEGUIR VALOR EN LA GLB # if not isinstance(ctx.message.channel, discord.channel.DMChannel): server = str(ctx.message.guild.id) db.clear_cache() lbsult = db.all() # doesnt work leaderboard = {} # Prepares an empty dictionary. for x in lbsult: # For each entry in the database: leaderboard[x.get("username")] = int( x.get("points") ) # ...save the user's ID and its amount of points in a new Python database. leaderboard = sorted( leaderboard.items(), key=lambda x: x[1], reverse=True) # Sort this database by amount of points. s = "" leadervalue = 1 for key, value in leaderboard: # For each value in the new, sorted DB: if key == searchvalor: break else: leadervalue += 1 # # CONSEGUIR VALOR EN LA LLB # if not isinstance(ctx.message.channel, discord.channel.DMChannel): llbsult = db.search(Query().servers.all([server])) # doesnt work lleaderboard = {} # Prepares an empty dictionary. for x in llbsult: # For each entry in the database: lleaderboard[x.get("username")] = int( x.get("points") ) # ...save the user's ID and its amount of points in a new Python database. lleaderboard = sorted( lleaderboard.items(), key=lambda x: x[1], reverse=True) # Sort this database by amount of points. s = "" localvalue = 1 for key, value in lleaderboard: # For each value in the new, sorted DB: if key == searchvalor: break else: localvalue += 1 # # GLB BADGE # if result: if leadervalue == 1: leaderemblem = "🥇" elif leadervalue == 2: leaderemblem = "🥈" elif leadervalue == 3: leaderemblem = "🥉" elif leadervalue <= 10: leaderemblem = "🏅" else: leaderemblem = " " else: leaderemblem = " " # # REVISAR ESTATUS DE CURATOR # thisguild = ctx.message.guild newbie_artist = discord.utils.get(thisguild.roles, name="Newbie Artist") veteran_artist = discord.utils.get(thisguild.roles, name="Veteran Artist") senior_artist = discord.utils.get(thisguild.roles, name="Senior Artist") designer = discord.utils.get(thisguild.roles, name="Designer") veteran_designer = discord.utils.get(thisguild.roles, name="Veteran Designer") senior_designer = discord.utils.get(thisguild.roles, name="Senior Designer") artist = discord.utils.get(thisguild.roles, name="Artist") roleemblem = "None" if newbie_artist in author.roles: roleemblem = f"{newbie_artist.mention}" elif veteran_artist in author.roles: roleemblem = f"{veteran_artist.mention}" elif senior_artist in author.roles: roleemblem = f"{senior_artist.mention}" elif designer in author.roles: roleemblem = f"{designer.mention}" elif veteran_designer in author.roles: roleemblem = f"{veteran_designer.mention}" elif senior_designer in author.roles: roleemblem = f"{senior_designer.mention}" elif artist in author.roles: roleemblem = f"{artist.mention}" # # POINTS SENT # sentpoints = post.search(Query().voters.any([author.id])) # # STARS RECEIVED # starlist = post.search(Query().username == str(author.id)) starsrec = 0 for star in starlist: if 'stars' in star: starsrec += star['stars'] # # SUPPORT BADGES (WIP) # #checkM = self.client.get_emoji(741669341409312889) #print(str(checkM)) # # SEND THE EMBED # embed = discord.Embed(title=author.name + ' ' + leaderemblem) embed.set_thumbnail(url=author.avatar_url) if result: embed.add_field(name="Thanks Received", value=result.get('points'), inline=False) else: embed.add_field(name="Thanks Received", value='0', inline=False) if result: embed.add_field(name="Artist Rank", value=roleemblem, inline=False) embed.add_field(name="Leaderboard Rank", value=leadervalue, inline=False) embed.add_field(name="Thanks Given", value=len(sentpoints), inline=False) embed.add_field(name="Stars received", value=starsrec, inline=False) embed.colour = author.top_role.colour await ctx.send(embed=embed)
async def getProfile(author, ctx, self): valor = str(author) searchvalor = str(author.id) result = db.get(Query()['username'] == searchvalor) # # CONSEGUIR VALOR EN LA GLB # if not isinstance(ctx.message.channel, discord.channel.DMChannel): server = str(ctx.message.guild.id) db.clear_cache() lbsult = db.all() # doesnt work leaderboard = {} # Prepares an empty dictionary. for x in lbsult: # For each entry in the database: leaderboard[x.get("username")] = int( x.get("points") ) # ...save the user's ID and its amount of points in a new Python database. leaderboard = sorted( leaderboard.items(), key=lambda x: x[1], reverse=True) # Sort this database by amount of points. s = "" leadervalue = 1 for key, value in leaderboard: # For each value in the new, sorted DB: if key == searchvalor: break else: leadervalue += 1 # # CONSEGUIR VALOR EN LA LLB # if not isinstance(ctx.message.channel, discord.channel.DMChannel): llbsult = db.search(Query().servers.all([server])) # doesnt work lleaderboard = {} # Prepares an empty dictionary. for x in llbsult: # For each entry in the database: lleaderboard[x.get("username")] = int( x.get("points") ) # ...save the user's ID and its amount of points in a new Python database. lleaderboard = sorted( lleaderboard.items(), key=lambda x: x[1], reverse=True) # Sort this database by amount of points. s = "" localvalue = 1 for key, value in lleaderboard: # For each value in the new, sorted DB: if key == searchvalor: break else: localvalue += 1 # # GLB BADGE # if result: if leadervalue == 1: leaderemblem = "🥇" elif leadervalue == 2: leaderemblem = "🥈" elif leadervalue == 3: leaderemblem = "🥉" elif leadervalue <= 10: leaderemblem = "🏅" else: leaderemblem = " " else: leaderemblem = " " # # REVISAR ESTATUS DE CURATOR # curatoremblem = "" if not isinstance(ctx.message.channel, discord.channel.DMChannel): curatoremote = self.client.get_emoji(742136325628756000) role = discord.utils.get(ctx.guild.roles, name="Curator") if role in author.roles: curatoremblem = str(curatoremote) # # REVISAR ESTATUS DE BOTOWNER # botemblem = "" for x in botowner: if (int(author.id) == int(x)): botemblem = "👨💻" # # POINTS SENT # sentpoints = post.search(Query().voters.any([author.id])) # # STARS RECEIVED # starlist = post.search(Query().username == str(author.id)) starsrec = 0 for star in starlist: if 'stars' in star: starsrec += star['stars'] # # SUPPORT BADGES (WIP) # #checkM = self.client.get_emoji(741669341409312889) #print(str(checkM)) # # SEND THE EMBED # embed = discord.Embed(title=author.name + ' ' + leaderemblem + str(curatoremblem) + botemblem) embed.set_thumbnail(url=author.avatar_url) if result: embed.add_field(name="Karma", value=result.get('points'), inline=True) else: embed.add_field(name="Karma", value='0', inline=True) if result: embed.add_field(name="Global Rank", value=leadervalue, inline=True) if not isinstance(ctx.message.channel, discord.channel.DMChannel): embed.add_field(name="Local Rank", value=localvalue, inline=True) embed.add_field(name="Reacted posts", value=len(sentpoints), inline=True) embed.add_field(name="Stars received", value=starsrec, inline=True) await ctx.send(embed=embed)