1
0

top.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import discord.ext.commands as discord_cmds
  2. import myanimebot.globals as globals
  3. @discord_cmds.command(name="top")
  4. async def top_cmd(ctx, *, keyword):
  5. ''' Processes the command "top" and returns statistics on registered feeds '''
  6. # TODO Redo this function
  7. if keyword is None:
  8. try:
  9. cursor = globals.conn.cursor(buffered=True)
  10. cursor.execute("SELECT * FROM v_Top")
  11. data = cursor.fetchone()
  12. if data is None: await ctx.send("It seems that there is no statistics... (what happened?!)")
  13. else:
  14. topText = "**__Here is the global statistics of this bot:__**\n\n"
  15. while data is not None:
  16. topText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
  17. data = cursor.fetchone()
  18. cursor = globals.conn.cursor(buffered=True)
  19. cursor.execute("SELECT * FROM v_TotalFeeds")
  20. data = cursor.fetchone()
  21. topText += "\n***Total user entry***: " + str(data[0])
  22. cursor = globals.conn.cursor(buffered=True)
  23. cursor.execute("SELECT * FROM v_TotalAnimes")
  24. data = cursor.fetchone()
  25. topText += "\n***Total unique manga/anime***: " + str(data[0])
  26. await ctx.send(topText)
  27. cursor.close()
  28. except Exception as e:
  29. globals.logger.warning("An error occured while displaying the global top: " + str(e))
  30. await ctx.send("Unable to reply to your request at the moment...")
  31. else:
  32. globals.logger.info("Displaying the global top for the keyword: " + keyword)
  33. try:
  34. cursor = globals.conn.cursor(buffered=True)
  35. cursor.callproc('sp_UsersPerKeyword', [str(keyword), '20'])
  36. for result in cursor.stored_results():
  37. data = result.fetchone()
  38. if data is None: await ctx.send("It seems that there is no statistics for the keyword **" + keyword + "**.")
  39. else:
  40. topKeyText = "**__Here is the statistics for the keyword " + keyword + ":__**\n\n"
  41. while data is not None:
  42. topKeyText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
  43. data = result.fetchone()
  44. await ctx.send(topKeyText)
  45. cursor.close()
  46. except Exception as e:
  47. globals.logger.warning("An error occured while displaying the global top for keyword '" + keyword + "': " + str(e))
  48. await ctx.send("Unable to reply to your request at the moment...")