151 lines
4.7 KiB
Python
151 lines
4.7 KiB
Python
import discord
|
|
|
|
addition = lambda x, y: x + y
|
|
subtraction = lambda x, y: x - y
|
|
multiplication = lambda x, y: x * y
|
|
division = lambda x, y: x / y
|
|
exponent = lambda x, y: x ** y
|
|
|
|
funckyDict = {
|
|
addition: "+",
|
|
subtraction: "-",
|
|
multiplication: "*",
|
|
division: "/",
|
|
exponent: "^"
|
|
}
|
|
|
|
|
|
async def number(self, interaction: discord.Interaction, number):
|
|
if self.output is not None:
|
|
self.inputOne = 0
|
|
self.output = None
|
|
self.inputOne = self.inputOne * 10 + number
|
|
self.numDigs = self.numDigs + 1
|
|
await interaction.response.edit_message(
|
|
content=self.display(),
|
|
view=self)
|
|
|
|
async def operator(self, interaction: discord.Interaction, operation):
|
|
self.output = None
|
|
self.inputTwo = self.inputOne
|
|
self.inputOne = 0
|
|
self.expression = operation
|
|
await interaction.response.edit_message(
|
|
content=self.display(),
|
|
view=self)
|
|
|
|
|
|
class NumberButton(discord.ui.Button):
|
|
|
|
def __init__(self, number: int, row) -> None:
|
|
super().__init__()
|
|
self.number = number
|
|
self.row = row
|
|
self.style = discord.ButtonStyle.blurple
|
|
self.label = str(number)
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
await number(self.view, interaction, self.number)
|
|
|
|
class OperatorButton(discord.ui.Button):
|
|
|
|
def __init__(self, function, row) -> None:
|
|
super().__init__()
|
|
self.function = function
|
|
self.row = row
|
|
self.style = discord.ButtonStyle.red
|
|
self.label = funckyDict[function]
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
await operator(self.view, interaction, self.function)
|
|
|
|
class Calculator(discord.ui.View):
|
|
def __init__(self, *, timeout=180):
|
|
super().__init__(timeout=timeout)
|
|
self.numDigs = 0
|
|
self.MAXIMUMBARSIZE = 30
|
|
m = 0
|
|
self.inputOne = 0
|
|
self.inputTwo = None
|
|
self.expression = None
|
|
self.output = None
|
|
self.digit = 1
|
|
|
|
|
|
self.clearRequirement = 0
|
|
|
|
numRows = 4
|
|
numColumns = 4
|
|
|
|
buttonFunctions = [
|
|
[1, 2, 3, addition],
|
|
[4, 5, 6, multiplication],
|
|
[7, 8, 9, subtraction],
|
|
[exponent, 0, None, division]
|
|
]
|
|
|
|
for i in range(0, numRows):
|
|
for j in range(0, numColumns):
|
|
item = buttonFunctions[i][j]
|
|
if item is None:
|
|
continue
|
|
if isinstance(item, int):
|
|
|
|
button = NumberButton(item, i)
|
|
else:
|
|
|
|
button = OperatorButton(item, i)
|
|
self.add_item(button)
|
|
|
|
|
|
|
|
def numDigits(self, integer):
|
|
numDigits = 0
|
|
while(integer):
|
|
numDigits += 1
|
|
integer = integer // 10
|
|
return numDigits
|
|
|
|
def clear(self):
|
|
self.inputOne = 0
|
|
self.inputTwo = None
|
|
self.output = None
|
|
self.clearRequirement = 0
|
|
|
|
def display(self):
|
|
inputOneLength = self.numDigits(self.inputOne)
|
|
inputTwoLength = self.numDigits(self.inputTwo)
|
|
outputLength = self.numDigits(self.output)
|
|
if (self.inputTwo is None) and (self.output is None):
|
|
padding = self.MAXIMUMBARSIZE - inputOneLength - 1
|
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputOne}", "`"])
|
|
elif (self.output is None):
|
|
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - 3
|
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{funckyDict[self.expression]}", f"{self.inputOne}", "`"])
|
|
else:
|
|
padding = self.MAXIMUMBARSIZE - inputOneLength - inputTwoLength - outputLength - 4
|
|
displayOutput = " ".join(['`', ' ' * padding, f"{self.inputTwo}", f"{funckyDict[self.expression]}", f"{self.inputOne}", "=", f"{self.output}", "`"])
|
|
return displayOutput
|
|
|
|
@discord.ui.button(label="=", style=discord.ButtonStyle.green, row=4)
|
|
async def equals(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
|
|
if self.inputTwo is None:
|
|
await interaction.response.edit_message(
|
|
content="ERROR",
|
|
view=self)
|
|
|
|
if self.clearRequirement:
|
|
self.clearRequirement = 0
|
|
self.clear()
|
|
else:
|
|
self.clearRequirement += 1
|
|
self.output = self.expression(self.inputTwo, self.inputOne)
|
|
if (self.inputTwo == 9) and (self.inputOne == 10):
|
|
self.output = 21
|
|
await interaction.response.edit_message(
|
|
content=self.display(),
|
|
view=self)
|
|
|
|
self.inputOne = self.output
|
|
self.inputTwo = None |