Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- # do pip install discord-components to install lib
- from discord_components import *
- import datetime
- client = commands.Bot(command_prefix = 'd!')
- @client.event
- async def on_ready():
- #turns on discord components lib
- DiscordComponents(client)
- #buttons array
- buttons = [
- [
- Button(style=ButtonStyle.grey, label='1'),
- Button(style=ButtonStyle.grey, label='2'),
- Button(style=ButtonStyle.grey, label='3'),
- Button(style=ButtonStyle.blue, label='×'),
- Button(style=ButtonStyle.red, label='Exit')
- ],
- [
- Button(style=ButtonStyle.grey, label='4'),
- Button(style=ButtonStyle.grey, label='5'),
- Button(style=ButtonStyle.grey, label='6'),
- Button(style=ButtonStyle.blue, label='÷'),
- Button(style=ButtonStyle.red, label='←')
- ],
- [
- Button(style=ButtonStyle.grey, label='7'),
- Button(style=ButtonStyle.grey, label='8'),
- Button(style=ButtonStyle.grey, label='9'),
- Button(style=ButtonStyle.blue, label='+'),
- Button(style=ButtonStyle.red, label='Clear')
- ],
- [
- Button(style=ButtonStyle.grey, label='00'),
- Button(style=ButtonStyle.grey, label='0'),
- Button(style=ButtonStyle.grey, label='.'),
- Button(style=ButtonStyle.blue, label='-'),
- Button(style=ButtonStyle.green, label='=')
- ],
- ]
- #calculates answer
- def calculate(exp):
- o = exp.replace('×', '*')
- o = o.replace('÷', '/')
- result = ''
- try:
- result = str(eval(o))
- except:
- result = 'An error occurred.'
- return result
- @client.command()
- async def calc(ctx):
- m = await ctx.send(content='Loading Calculators...')
- expression = 'None'
- delta = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
- e = discord.Embed(title=f'{ctx.author.name}\'s calculator | {ctx.author.id}', description=expression,
- timestamp=delta)
- await m.edit(components=buttons, embed=e)
- while m.created_at < delta:
- res = await client.wait_for('button_click')
- if res.author.id == int(res.message.embeds[0].title.split('|')[1]) and res.message.embeds[
- 0].timestamp < delta:
- expression = res.message.embeds[0].description
- if expression == 'None' or expression == 'An error occurred.':
- expression = ''
- if res.component.label == 'Exit':
- await res.respond(content='Calculator Closed', type=7)
- break
- elif res.component.label == '←':
- expression = expression[:-1]
- elif res.component.label == 'Clear':
- expression = 'None'
- elif res.component.label == '=':
- expression = calculate(expression)
- else:
- expression += res.component.label
- f = discord.Embed(title=f'{res.author.name}\'s calculator|{res.author.id}', description=expression,
- timestamp=delta)
- await res.respond(content='', embed=f, components=buttons, type=7)
- client.run('keep dreaming :D')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement