Files
Ultroid-fork/plugins/calculator.py
Devesh Pal 0df53caf4c Ultroid v0.3 Updates
Co-authored-by: Aditya <xditya@ultroid.tech>
Co-authored-by: Danish <danish@ultroid.tech>
Co-authored-by: Amit Sharma <48654350+buddhhu@users.noreply.github.com>
Co-authored-by: TechiError <error@notavailable.live>
Co-authored-by: Avish Kumar <85635883+aviskumar@users.noreply.github.com>
Co-authored-by: Vɪɴᴀʏᴀᴋ Pᴀɴᴅᴇʏ <87496159+harpia-vieillot@users.noreply.github.com>
Co-authored-by: Shrimadhav U K <6317196+spechide@users.noreply.github.com>
Co-authored-by: Dark <darkbeamer.official@gmail.com>
Co-authored-by: Muhamad Risman Aziz <62795826+mrismanaziz@users.noreply.github.com>
Co-authored-by: Ashik Muhammed <84127769+MR-JINN-OF-TG@users.noreply.github.com>
Co-authored-by: MMETMA <79155572+MMETMA@users.noreply.github.com>
Co-authored-by: amirmehdinzri <94852182+amirmehdinzri@users.noreply.github.com>
2021-12-31 23:48:53 +05:30

154 lines
3.7 KiB
Python

# Ultroid - UserBot
# Copyright (C) 2021-2022 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
•`{i}calc` - Inline Calculator
"""
import re
from . import Button, asst, callback, get_string, in_pattern, udB, ultroid_cmd
CALC = {}
m = [
"AC",
"C",
"",
"%",
"7",
"8",
"9",
"+",
"4",
"5",
"6",
"-",
"1",
"2",
"3",
"x",
"00",
"0",
".",
"÷",
]
tultd = [Button.inline(f"{x}", data=f"calc{x}") for x in m]
lst = list(zip(tultd[::4], tultd[1::4], tultd[2::4], tultd[3::4]))
lst.append([Button.inline("=", data="calc=")])
@ultroid_cmd(pattern="calc")
async def icalc(e):
udB.del_key("calc")
if e.client._bot:
return await e.reply(get_string("calc_1"), buttons=lst)
results = await e.client.inline_query(asst.me.username, "calc")
await results[0].click(e.chat_id, silent=True, hide_via=True)
await e.delete()
@in_pattern("calc", owner=True)
async def _(e):
calc = e.builder.article("Calc", text=get_string("calc_1"), buttons=lst)
await e.answer([calc])
@callback(re.compile("calc(.*)"), owner=True)
async def _(e):
x = (e.data_match.group(1)).decode()
user = e.query.user_id
get = None
if x == "AC":
if CALC.get(user):
CALC.pop(user)
await e.edit(
get_string("calc_1"),
buttons=[Button.inline(get_string("calc_2"), data="recalc")],
)
elif x == "C":
if CALC.get(user):
CALC.pop(user)
await e.answer("cleared")
elif x == "":
if CALC.get(user):
get = CALC[user]
if get:
CALC.update({user: get[:-1]})
await e.answer(str(get[:-1]))
elif x == "%":
if CALC.get(user):
get = CALC[user]
if get:
CALC.update({user: get + "/100"})
await e.answer(str(get + "/100"))
elif x == "÷":
if CALC.get(user):
get = CALC[user]
if get:
CALC.update({user: get + "/"})
await e.answer(str(get + "/"))
elif x == "x":
if CALC.get(user):
get = CALC[user]
if get:
CALC.update({user: get + "*"})
await e.answer(str(get + "*"))
elif x == "=":
if CALC.get(user):
get = CALC[user]
if get:
if get.endswith(("*", ".", "/", "-", "+")):
get = get[:-1]
out = eval(get)
try:
num = float(out)
await e.answer(f"Answer : {num}", cache_time=0, alert=True)
except BaseException:
CALC.pop(user)
await e.answer(get_string("sf_8"), cache_time=0, alert=True)
await e.answer("None")
else:
if CALC.get(user):
get = CALC[user]
if get:
CALC.update({user: get + x})
return await e.answer(str(get + x))
CALC.update({user: x})
await e.answer(str(x))
@callback("recalc", owner=True)
async def _(e):
m = [
"AC",
"C",
"",
"%",
"7",
"8",
"9",
"+",
"4",
"5",
"6",
"-",
"1",
"2",
"3",
"x",
"00",
"0",
".",
"÷",
]
tultd = [Button.inline(f"{x}", data=f"calc{x}") for x in m]
lst = list(zip(tultd[::4], tultd[1::4], tultd[2::4], tultd[3::4]))
lst.append([Button.inline("=", data="calc=")])
await e.edit(get_string("calc_1"), buttons=lst)