Advertisement
YT_CRAZYPLAYS

Python Calc

Jan 6th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. # do pip install discord-components to install lib
  4. from discord_components import *
  5. import datetime
  6.  
  7. client = commands.Bot(command_prefix = 'd!')
  8.  
  9. @client.event
  10. async def on_ready():
  11. #turns on discord components lib
  12. DiscordComponents(client)
  13.  
  14. #buttons array
  15. buttons = [
  16. [
  17. Button(style=ButtonStyle.grey, label='1'),
  18. Button(style=ButtonStyle.grey, label='2'),
  19. Button(style=ButtonStyle.grey, label='3'),
  20. Button(style=ButtonStyle.blue, label='×'),
  21. Button(style=ButtonStyle.red, label='Exit')
  22. ],
  23. [
  24. Button(style=ButtonStyle.grey, label='4'),
  25. Button(style=ButtonStyle.grey, label='5'),
  26. Button(style=ButtonStyle.grey, label='6'),
  27. Button(style=ButtonStyle.blue, label='÷'),
  28. Button(style=ButtonStyle.red, label='←')
  29. ],
  30. [
  31. Button(style=ButtonStyle.grey, label='7'),
  32. Button(style=ButtonStyle.grey, label='8'),
  33. Button(style=ButtonStyle.grey, label='9'),
  34. Button(style=ButtonStyle.blue, label='+'),
  35. Button(style=ButtonStyle.red, label='Clear')
  36. ],
  37. [
  38. Button(style=ButtonStyle.grey, label='00'),
  39. Button(style=ButtonStyle.grey, label='0'),
  40. Button(style=ButtonStyle.grey, label='.'),
  41. Button(style=ButtonStyle.blue, label='-'),
  42. Button(style=ButtonStyle.green, label='=')
  43. ],
  44. ]
  45.  
  46. #calculates answer
  47. def calculate(exp):
  48. o = exp.replace('×', '*')
  49. o = o.replace('÷', '/')
  50. result = ''
  51. try:
  52. result = str(eval(o))
  53. except:
  54. result = 'An error occurred.'
  55. return result
  56.  
  57. @client.command()
  58. async def calc(ctx):
  59. m = await ctx.send(content='Loading Calculators...')
  60. expression = 'None'
  61. delta = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
  62. e = discord.Embed(title=f'{ctx.author.name}\'s calculator | {ctx.author.id}', description=expression,
  63. timestamp=delta)
  64. await m.edit(components=buttons, embed=e)
  65. while m.created_at < delta:
  66. res = await client.wait_for('button_click')
  67. if res.author.id == int(res.message.embeds[0].title.split('|')[1]) and res.message.embeds[
  68. 0].timestamp < delta:
  69. expression = res.message.embeds[0].description
  70. if expression == 'None' or expression == 'An error occurred.':
  71. expression = ''
  72. if res.component.label == 'Exit':
  73. await res.respond(content='Calculator Closed', type=7)
  74. break
  75. elif res.component.label == '←':
  76. expression = expression[:-1]
  77. elif res.component.label == 'Clear':
  78. expression = 'None'
  79. elif res.component.label == '=':
  80. expression = calculate(expression)
  81. else:
  82. expression += res.component.label
  83. f = discord.Embed(title=f'{res.author.name}\'s calculator|{res.author.id}', description=expression,
  84. timestamp=delta)
  85. await res.respond(content='', embed=f, components=buttons, type=7)
  86.  
  87. client.run('keep dreaming :D')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement