async def link(self, ctx, username: str): """Links your discord account to your dmoj account""" # Check if user exists query = Query() user = await query.get_user(username) if user is None: await ctx.send(f"{username} does not exist on DMOJ") return username = user.username if query.get_handle(ctx.author.id, ctx.guild.id): await ctx.send('%s, your handle is already linked with %s.' % (ctx.author.mention, query.get_handle(ctx.author.id, ctx.guild.id))) return if query.get_handle_user(username, ctx.guild.id): await ctx.send('This handle is already linked with another user') return problem = query.get_random_problem() if problem is None: await ctx.send('No problems are cached.. ' 'Pls do something about that') # Will implement this # Just cache the problems and get a random one return await ctx.send( '%s, submit a compiler error to <https://dmoj.ca/problem/%s> ' 'within 60 seconds' % (ctx.author.mention, problem.code)) await asyncio.sleep(60) submissions = await query.get_latest_submissions(username, 10) # if the user links twice, it might break the db # Should add decorator to prevent this if query.get_handle(ctx.author.id, ctx.guild.id): return for submission in submissions: if (submission.result == 'CE' and submission.problem[0].code == problem.code): handle = Handle_DB() handle.id = ctx.author.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.guild.id session.add(handle) session.commit() return await ctx.send( "%s, you now have linked your account to %s." % (ctx.author.name, username)) else: return await ctx.send('I don\'t see anything :monkey: ' '(Failed to link accounts)')
async def _set(self, ctx, member: discord.Member, username: str): """Manually link two accounts together""" query = Query() user = await query.get_user(username) if user is None: await ctx.send(f'{username} does not exist on dmoj') return username = user.username if query.get_handle(member.id, ctx.guild.id): await ctx.send( '%s, this handle is already linked with %s.' % (ctx.author.mention, query.get_handle(member.id, ctx.guild.id)) ) return if query.get_handle_user(username, ctx.guild.id): await ctx.send('This handle is already linked with another user') return handle = Handle_DB() handle.id = member.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.guild.id session.add(handle) session.commit() return await ctx.send("%s, %s is now linked with %s." % (ctx.author.name, member.name, username))
async def link(ctx: lightbulb.Context) -> None: username = ctx.options.username # Check if user exists query = Query() try: user = await query.get_user(username) if user is None: raise ObjectNotFound() except ObjectNotFound: await ctx.respond(escape_markdown(f"{username} does not exist on DMOJ")) return username = user.username if query.get_handle(ctx.author.id, ctx.get_guild().id): await ctx.respond( "%s, your handle is already linked with %s." % (ctx.author.mention, query.get_handle(ctx.author.id, ctx.get_guild().id)) ) return if query.get_handle_user(username, ctx.get_guild().id): await ctx.respond("This handle is already linked with another user") return # verify from dmoj user description description = await query.get_user_description(username) userKey = hashlib.sha256(str(ctx.author.id).encode()).hexdigest() if userKey not in description: await ctx.respond( "Put `" + userKey + "` in your DMOJ user description (https://dmoj.ca/edit/profile/) " "and run the command again." ) return handle = Handle_DB() handle.id = ctx.author.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.get_guild().id session.add(handle) session.commit() await ctx.respond(escape_markdown("%s, you now have linked your account to %s" % (ctx.author, username))) rank_to_role = {} rc = lightbulb.RoleConverter(ctx) for role_id in ctx.get_guild().get_roles(): role = await rc.convert(str(role_id)) if role.name in RANKS: rank_to_role[role.name] = role rank = rating_to_rank(user.rating) # TODO Add guild specific option to disable updating roles if rank in rank_to_role: await _update_rank(ctx.member, rank_to_role[rank], "Dmoj account linked") else: await ctx.respond("You are missing the `" + rank + "` role")
async def _set(self, ctx, member, username: str): """Manually link two accounts together""" query = Query() member = await query.parseUser(ctx, member) if username != "+remove": user = await query.get_user(username) if user is None: await ctx.send(f'{username} does not exist on dmoj') return username = user.username handle = query.get_handle(member.id, ctx.guild.id) if handle == username: return await ctx.send( f'{member.display_name} is already linked with {handle}') if handle: handle = session.query(Handle_DB)\ .filter(Handle_DB.id == member.id)\ .filter(Handle_DB.guild_id == ctx.guild.id).first() session.delete(handle) session.commit() await ctx.send( f'Unlinked {member.display_name} with handle {handle.handle}') if username == "+remove": return if query.get_handle_user(username, ctx.guild.id): await ctx.send('This handle is already linked with another user') return handle = Handle_DB() handle.id = member.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.guild.id session.add(handle) session.commit() await ctx.send(f"Linked {member.name} with {username}.") rank_to_role = { role.name: role for role in ctx.guild.roles if role.name in RANKS } rank = self.rating_to_rank(user.rating) if rank in rank_to_role: await self._update_rank(ctx.author, rank_to_role[rank], 'Dmoj account linked') else: await ctx.send("You are missing the " + rank.name + " role")
async def link(self, ctx, username: str): '''Links your discord account to your dmoj account''' # Check if user exists query = Query() user = await query.get_user(username) if user is None: await ctx.send(f'{username} does not exist on DMOJ') return username = user.username if query.get_handle(ctx.author.id, ctx.guild.id): await ctx.send('%s, your handle is already linked with %s.' % (ctx.author.mention, query.get_handle(ctx.author.id, ctx.guild.id))) return if query.get_handle_user(username, ctx.guild.id): await ctx.send('This handle is already linked with another user') return # verify from dmoj user description description = await query.get_user_description(username) userKey = hashlib.sha256(str(ctx.author.id).encode()).hexdigest() if userKey not in description: await ctx.send( 'Put `' + userKey + '` in your DMOJ user description (https://dmoj.ca/edit/profile/) ' 'and run the command again.') return handle = Handle_DB() handle.id = ctx.author.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.guild.id session.add(handle) session.commit() await ctx.send('%s, you now have linked your account to %s' % (ctx.author.name, username)) return rank_to_role = { role.name: role for role in ctx.guild.roles if role.name in RANKS } rank = self.rating_to_rank(user.rating) if rank in rank_to_role: await self._update_rank(ctx.author, rank_to_role[rank], 'Dmoj account linked') else: await ctx.send('You are missing the ' + rank.name + ' role')
async def _set(ctx): """Manually link two accounts together""" # TODO: I don't like the bot spamming replies to itself, figure out a way to lessen that member = ctx.options.member username = ctx.options.handle query = Query() if username != "+remove": try: user = await query.get_user(username) if user is None: raise ObjectNotFound() except ObjectNotFound: await ctx.respond(escape_markdown(f"{username} does not exist on dmoj")) return username = user.username handle = query.get_handle(member.id, ctx.get_guild().id) if handle == username: return await ctx.respond(escape_markdown(f"{member.display_name} is already linked with {handle}")) if handle: handle = ( session.query(Handle_DB) .filter(Handle_DB.id == member.id) .filter(Handle_DB.guild_id == ctx.get_guild().id) .first() ) session.delete(handle) session.commit() await ctx.respond(escape_markdown(f"Unlinked {member.display_name} with handle {handle.handle}")) if username == "+remove": return if query.get_handle_user(username, ctx.get_guild().id): await ctx.respond("This handle is already linked with another user") return handle = Handle_DB() handle.id = member.id handle.handle = username handle.user_id = user.id handle.guild_id = ctx.get_guild().id session.add(handle) session.commit() await ctx.respond(escape_markdown(f"Linked {member.display_name} with {username}")) rank_to_role = {} rc = lightbulb.RoleConverter(ctx) for role_id in ctx.get_guild().get_roles(): role = await rc.convert(str(role_id)) if role.name in RANKS: rank_to_role[role.name] = role rank = rating_to_rank(user.rating) if rank in rank_to_role: await _update_rank(member, rank_to_role[rank], "Dmoj account linked") else: await ctx.respond("You are missing the `" + rank.name + "` role")