Skip to content

Commit

Permalink
Allow up to 5 inputs for add, subtract, multiply
Browse files Browse the repository at this point in the history
This allows users to provide up to 5 inputs to each of `/maths add`,
`/maths subtract`, and `/maths multiply`. The third, fourth, and fifth
inputs are optional, and the user can pick whichever ones they want,
skip ones in between, etc. The code only uses the actually provided
values.
  • Loading branch information
MajorTanya committed Jul 31, 2023
1 parent a3e9e9e commit 056fbe4
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions maths.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from functools import reduce
from math import fsum, prod
from operator import sub

import discord
from discord import app_commands
from discord.ext import commands
Expand All @@ -13,25 +17,53 @@ def __init__(self, bot: DTbot):
self.bot = bot

@app_commands.command(description="Add two numbers together")
async def add(self, interaction: discord.Interaction, first: float, second: float):
first = rint(first, digits=3)
second = rint(second, digits=3)
result = rint(first + second, digits=3)
await interaction.response.send_message(f"{first:,} + {second:,} = {result:,}")
async def add(
self,
interaction: discord.Interaction,
first: float,
second: float,
third: float | None,
fourth: float | None,
fifth: float | None,
):
raw_inputs = [first, second, third, fourth, fifth]
numbers = [rint(num, digits=3) for num in raw_inputs if num is not None]
strs = [f"({i:,})" if i < 0 else f"{i:,}" for i in numbers]
result = rint(fsum(numbers), digits=3)
await interaction.response.send_message(f"{' + '.join(strs)} = {result:,}")

@app_commands.command(description="Subtract two numbers")
async def subtract(self, interaction: discord.Interaction, first: float, second: float):
first = rint(first, digits=3)
second = rint(second, digits=3)
result = rint(first - second, digits=3)
await interaction.response.send_message(f"{first:,} - {second:,} = {result:,}")
async def subtract(
self,
interaction: discord.Interaction,
first: float,
second: float,
third: float | None,
fourth: float | None,
fifth: float | None,
):
raw_inputs = [first, second, third, fourth, fifth]
numbers = [rint(num, digits=3) for num in raw_inputs if num is not None]
strs = [f"({i:,})" if i < 0 else f"{i:,}" for i in numbers]
result = rint(reduce(sub, numbers), digits=3) # type: ignore
await interaction.response.send_message(f"{' - '.join(strs)} = {result:,}")

@app_commands.command(description="Multiply two numbers")
async def multiply(self, interaction: discord.Interaction, first: float, second: float):
first = rint(first, digits=3)
second = rint(second, digits=3)
result = rint(first * second, digits=3)
await interaction.response.send_message(f"{first:,} * {second:,} = {result:,}")
async def multiply(
self,
interaction: discord.Interaction,
first: float,
second: float,
third: float | None,
fourth: float | None,
fifth: float | None,
):
raw_inputs = [first, second, third, fourth, fifth]
numbers = [rint(num, digits=3) for num in raw_inputs if num is not None]
strs = [f"({i:,})" if i < 0 else f"{i:,}" for i in numbers]
result = rint(prod(numbers), digits=3)
escaped_asterisk = " \\* " # Prevent Discord Markdown rendering (f-strings disallow escape sequences in <3.12)
await interaction.response.send_message(f"{escaped_asterisk.join(strs)} = {result:,}")

@app_commands.command(description="Divide a number by another number")
async def divide(self, interaction: discord.Interaction, dividend: float, divisor: float):
Expand Down

0 comments on commit 056fbe4

Please sign in to comment.