async def on_member_join(self, member: discord.Member): if member.guild.id == "725886999646437407": try: mee6API = API(member.guild.id) Xp = await mee6API.levels.get_user_xp(member.id) Level = await mee6API.levels.get_user_level(member.id) connection = self.auth() cursor = connection.cursor() mee6_api = API(member.guild.id) xp = await mee6_api.levels.get_user_xp(member.id) level = await mee6_api.levels.get_user_level(member.id) if xp == None: cursor.execute( f"INSERT INTO Levels(userId, guildId, LastValidTimestamp, level, XP, totalXP) VALUES" f"({member.id}, {member.guild.id}, {int(time.time())}, 0, 0, 0)" ) else: cursor.execute( f"INSERT INTO Levels(userId, guildId, LastValidTimestamp, level, XP, totalXP) VALUES" f"({member.id}, {member.guild.id}, {int(time.time())}, {level}, 0, {xp})" ) connection.commit() except Exception as ex: if config.debug: print( f"Can you like, stop writing awful code please?\n {ex}\n\n{traceback.format_exc()}" ) finally: if connection is not None and connection.is_connected(): connection.close() if cursor is not None: cursor.close()
async def on_message(self, message): if (message.author.bot): return # Find guild id guild_id = message.guild.id mee6API = API(guild_id) # Check user level user_id = message.author.id userLevel = await mee6API.levels.get_user_level(user_id) # Check roles json with open("roles.json", "r") as roleFile: data = json.load(roleFile) # Check config json with open("configuration.json", "r") as configFile: config = json.load(configFile) # Add role for x in data["roles"]: if (x["level"] == userLevel): if x["id"] not in [y.id for y in message.author.roles ]: # Check if user has not the role getrole = get(message.guild.roles, id=x["id"]) await message.author.add_roles(getrole) elif config["removePreviousRewards"] == True: if x["id"] in [y.id for y in message.author.roles]: getrole = get(message.guild.roles, id=x["id"]) await message.author.remove_roles(getrole)
async def get_user_rank(self, guild: str, user: str): if guild not in self.api_guild_cache: self.api_guild_cache[guild] = API(int(guild)) api = self.api_guild_cache[guild] return await api.levels.get_user_level(int(user))
async def leaderboard(self, ctx): waitMessage = await ctx.channel.send("Please wait during the update..." ) # Check roles json with open("roles.json", "r") as roleFile: data = json.load(roleFile) # Check config json with open("configuration.json", "r") as configFile: config = json.load(configFile) mee6API = API(ctx.guild.id) pageNumber = ceil(len(ctx.guild.members) / 100) for i in range(pageNumber): leaderboard_page = await mee6API.levels.get_leaderboard_page(i) for user in leaderboard_page["players"]: # If user in the guild if int(user["id"]) in [ guildMember.id for guildMember in ctx.guild.members ]: # Add role for x in data["roles"]: if x["level"] <= user["level"]: if x["id"] not in [ roleId for roleId in [ guildMember.roles for guildMember in ctx.guild.members if guildMember.id == int(user["id"]) ] ]: # Check if user has not the role getrole = get(ctx.guild.roles, id=x["id"]) member = [ guildMember for guildMember in ctx.guild.members if guildMember.id == int(user["id"]) ] await member[0].add_roles(getrole) elif config["removePreviousRewards"] == True: if x["id"] in [ roleId for roleId in [ guildMember.roles for guildMember in ctx.guild.members if guildMember.id == int(user["id"]) ] ]: # Check if user has the role getrole = get(ctx.guild.roles, id=x["id"]) member = [ guildMember for guildMember in ctx.guild.members if guildMember.id == int(user["id"]) ] await member[0].remove_roles(getrole) await waitMessage.delete() embed = discord.Embed(title=f"**LEADERBOARD :**", description=f"Everyone has been updated.", color=0x1eb823) # Green embed.set_footer(text="Bot Created by Darkempire#8245") await ctx.channel.send(embed=embed)
async def updateRoles(self): with open("configuration.json", "r") as configFile: config = json.load(configFile) for guildId in config["updateEachTime"]: guild = self.bot.get_guild(guildId) if guild: with open("roles.json", "r") as roleFile: data = json.load(roleFile) mee6API = API(guild.id) pageNumber = ceil(len(guild.members) / 100) for i in range(pageNumber): leaderboard_page = await mee6API.levels.get_leaderboard_page( i) for user in leaderboard_page["players"]: # If user in the guild if int(user["id"]) in [ guildMember.id for guildMember in guild.members ]: # Add role for x in data["roles"]: if x["level"] <= user["level"]: if x["id"] not in [ roleId for roleId in [ guildMember.roles for guildMember in guild.members if guildMember.id == int( user["id"]) ] ]: # Check if user has not the role getrole = get(guild.roles, id=x["id"]) member = [ guildMember for guildMember in guild.members if guildMember.id == int(user["id"]) ] await member[0].add_roles(getrole) elif config["removePreviousRewards"] == True: if x["id"] in [ roleId for roleId in [ guildMember.roles for guildMember in guild.members if guildMember.id == int( user["id"]) ] ]: # Check if user has the role getrole = get(guild.roles, id=x["id"]) member = [ guildMember for guildMember in guild.members if guildMember.id == int(user["id"]) ] await member[0].remove_roles(getrole)
async def save_leaderboard(ctx): """async function to save the MEE6 leaderboard into a google spreadsheet""" # Connect and retrieve information from MEE6 Leaderboard mee6api = API(credentials["bot_key"]["key_api"]) leaderboard_page = await mee6api.levels.get_leaderboard_page(0) # Connect to Google Drive g_client = ConnectDrive() g_client.save_leaderboard(leaderboard_page) await ctx.send("Saved")
def config_load(guild): """ Reload all the configuration fr this guild """ global botconfig botconfig[guild.id] = dict() botconfig[guild.id]['config'] = config_read(guild, 'config') botconfig[guild.id]['access'] = config_read(guild, 'access') botconfig[guild.id]['dict'] = config_read(guild, 'dict') botconfig[guild.id]['convert'] = config_read(guild, 'convert') botconfig[guild.id]['mee6'] = API(guild.id) botconfig[guild.id]['last_msg'] = dict()
async def on_message(self, message): mee6 = self.bot.get_user(159985870458322944) if message.content == "!levels" and mee6 in message.guild.members: api = API(message.guild.id) result = await api.levels.get_leaderboard_page(0) embed = discord.Embed(title=f"{message.guild.name} 詳細順位表 (Top10)", color=0x00ffff) players = result["players"][:10] for i, player in enumerate(players): tmp = f"レベル:{player['level']}\nメッセージ数:{player['message_count']}\n経験値:{player['xp']}" embed.add_field(name=f"{player['username']} ({i + 1} 位)", value=tmp, inline=bool(i % 3)) await message.channel.send(embed=embed)
async def getlevels(self, ctx): try: connection = self.auth() cursor = connection.cursor() mee6_api = API(ctx.message.guild.id) index = 0 embed = self.bot.create_completed_embed("Getting user stats...", "generating embed....") msg = await ctx.reply(embed=embed) for users in [m for m in ctx.guild.members if not m.bot]: index += 1 xp = await mee6_api.levels.get_user_xp(users.id) level = await mee6_api.levels.get_user_level(users.id) if xp == None: cursor.execute( f"INSERT INTO Levels(userId, guildId, LastValidTimestamp, level, XP, totalXP) VALUES" f"({users.id}, {ctx.guild.id}, {int(time.time())}, 0, 0, 0)" ) else: cursor.execute( f"INSERT INTO Levels(userId, guildId, LastValidTimestamp, level, XP, totalXP) VALUES" f"({users.id}, {ctx.guild.id}, {int(time.time())}, {level}, 0, {xp})" ) connection.commit() embed.set_footer( text= f"Getting user {index}/{ctx.message.guild.member_count}") embed.description = f"{users.name}#{users.discriminator}({users.id})\n{xp}\n{level}" await msg.edit(text="", embed=embed) await asyncio.sleep(1) except Exception as ex: if config.debug: print( f"Can you like, stop writing awful code please?\n {ex}\n\n{traceback.format_exc()}" ) finally: if connection is not None and connection.is_connected(): connection.close() if cursor is not None: cursor.close()
async def on_message(self, message): if (message.author.bot): return # Check config json with open("configuration.json", "r") as configFile: config = json.load(configFile) if config["updateEachMessage"]: # Find guild id guild_id = message.guild.id mee6API = API(guild_id) # Check user level user_id = message.author.id userLevel = await mee6API.levels.get_user_level(user_id) if userLevel is None: date = datetime.datetime.now().strftime("%x %X") return print(f"{date} The search for the player's level failed.") # Check roles json with open("roles.json", "r") as roleFile: data = json.load(roleFile) # Add role for x in data["roles"]: if x["level"] <= userLevel: if x["id"] not in [y.id for y in message.author.roles]: # Check if user has not the role getrole = get(message.guild.roles, id = x["id"]) await message.author.add_roles(getrole) # Remove previous roles if config["removePreviousRewards"]: for userRole in message.author.roles: if (userRole.id in [roleReward["id"] for roleReward in data["roles"]]) and userRole.id != x["id"]: getrole = get(message.guild.roles, id = x["id"]) await message.author.remove_roles(getrole)
async def profile(ctx, user: discord.Member): try: print(f"Getting user info from {user}, requested by {ctx.author}") mee6API = API(ctx.message.guild.id) Details = await mee6API.levels.get_user_details( user.mention.replace("<@", "").replace(">", "").replace("!", "").replace( "<@", "").replace(">", "")) LVL = dict(Details).get("level") XP = dict(Details).get("xp") MSG = dict(Details).get("message_count") Embed = discord.Embed( title=f"{user} user info", description= f"Profile Info:\nUser Nick: **{user.nick}**\nUser Join Date: **{user.joined_at}**\nUser Nitro Since: **{user.premium_since}**\nUser MEE6 Stats:\n Level: **{LVL}**\n Message Count: **{MSG}**\nXP: **{XP}**\nUser profile picture: {user.avatar_url}\n", color=discord.Color.gold()).set_image(url=user.avatar_url) await ctx.send(embed=Embed) except: Embed = discord.Embed( title=f"{user} user info", description= f"Profile Info:\nUser Nick: **{user.nick}**\nUser Join Date: **{user.joined_at}**\nUser Nitro Since: **{user.premium_since}**\nUser profile picture: {user.avatar_url}\n", color=discord.Color.gold()).set_image(url=user.avatar_url) await ctx.send(embed=Embed)