Skip to content

Commit

Permalink
Added latest subs to +user
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyliu committed Jun 25, 2020
1 parent 375e08e commit aa69d77
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions JOMD.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
import re
import time
import html

from discord.ext import commands

Expand Down Expand Up @@ -87,16 +88,32 @@ def calculate_points(points,fully_solved):
p += (0.95**i)*points[i]
return b+p

def get_latest_submission(username,num):
response = requests.get(f'https://dmoj.ca/submissions/user/{username}/')
text=response.text.replace('\n','')
text=text.replace('<div class="time">','<div title="---s" class="time">')
matches=re.findall(r'<div class="submission-row" id=".*?"><div class="sub-result .*?"><div class="score">(---|.*? / .*?)</div><div class="state"><span title=".*?" class="status">(.*?)</span> \|<span class="language">(.*?)</span></div></div><div class="sub-info"><div class="name"><a href="/problem/(.*?)">(.*?)</a></div><div><span class="rating (.*?)"><a href="/user/.*?">(.*?)</a></span><span class="time"><span data-iso="(.*?)" class="time-with-rel" title="(.*?)" data-format="{time}">.*?</span></span>.*?</div></div><div class="sub-usage"><div title="(.*?)s" class="time">.*?</div><div class="memory">(.*?)</div></div>',text)
return matches[:num]

@bot.command(name='user')
async def user(ctx,*username):
async def user(ctx,*args):
# Beautify the errors
if len(username) > 1:
return await ctx.send(f'Too many arguments, {pref}user <user>')
if len(args) > 2:
return await ctx.send(f'Too many arguments, {pref}user <user> <latest submissions>')

if len(username) < 1:
return await ctx.send(f'Too few arguments, {pref}user <user>')
if len(args) < 1:
return await ctx.send(f'Too few arguments, {pref}user <user> <latest submissions>')
if len(args) == 2:
if not args[1].isdigit():
return await ctx.send(f'{args[1]} is not an integer')

if int(args[1]) > 8 :
return await ctx.send(f'Requesting too many submissions, Max (8)')

if int(args[1]) < 1 :
return await ctx.send(f'Pls no troll :>')

username=username[0]
username=args[0]

user = get_user(username)

Expand All @@ -120,6 +137,29 @@ async def user(ctx,*username):
embed.add_field(name="Rating", value=data['rating'], inline=True)
embed.add_field(name="Contests Written", value=sum(map(is_rated,data['contests'])), inline=True)
await ctx.send(embed=embed)

if len(args) == 1:
return

latest_subs = int(args[1])
print("Getting latest submissions")
# Send latest submissions
submissions = get_latest_submission(username,latest_subs)
print("Making embed message")
embed=discord.Embed(title=f"{username}'s latest submissions",color=0xfcdb05)
msg = ""
for sub in submissions:
print(sub)
embed.add_field(name=sub[0], value="%s | %s" % (sub[1],sub[2]), inline=True)
embed.add_field(name="%s" % html.unescape(sub[4]), value="%s" % sub[8], inline=True)
try:
float(sub[9])
embed.add_field(name="%.2f" % (float(sub[9])), value="%s" % sub[10], inline=True)
except:
embed.add_field(name="%s" % (sub[9]), value="%s" % sub[10], inline=True)
# msg+="%s | %-15s | %-10s | %-70s | %-5.2f | %-5s |\n" % (sub[1],sub[2],sub[0],html.unescape(sub[4]),float(sub[9]),sub[10])

await ctx.send(embed=embed)
return None

@bot.command(name='predict')
Expand Down Expand Up @@ -171,14 +211,9 @@ async def predict(ctx,*args):
fully_solved_problems+=1
points.sort(reverse=True)
updated_points=calculate_points(points,fully_solved_problems)
embed.add_field(name="Solve another %sp" % i, value="Total points: %.2f" % updated_points, inline=False)
embed.add_field(name="Solve another %sp" % i, value="Total points: %.2fp" % updated_points, inline=False)
await msg.edit(content='',embed=embed)






def main():
print("Bot is Running")
bot.run(BOT_TOKEN)
Expand Down

0 comments on commit aa69d77

Please sign in to comment.