Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nfearnley committed May 20, 2024
1 parent a1c9020 commit c9812fc
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 343 deletions.
13 changes: 8 additions & 5 deletions discordplus/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any
from collections.abc import Iterable
from typing import TypeVar
from collections.abc import Iterator

from copy import copy

Expand All @@ -16,7 +16,10 @@ class BadMultilineCommand(commands.errors.CommandError):
pass


def find_one(iterable: Iterable) -> Any:
T = TypeVar("T")


def find_one(iterable: Iterator[T]) -> T | None:
try:
return next(iterable)
except StopIteration:
Expand All @@ -33,12 +36,12 @@ async def process_commands(self: BotBase, message: discord.Message):

# No command found, invoke will handle it
if not ctx.command:
await self.invoke(ctx)
await self.invoke(ctx) # type: ignore
return

# One multiline command (command string starts with a multiline command)
if ctx.command.multiline:
await self.invoke(ctx)
await self.invoke(ctx) # type: ignore
return

# Multiple commands (first command is not multiline)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ console_scripts =
sizebot-upgrade = sizebot.scripts.upgradeusers:main

[flake8]
ignore = E501,W503,E241,E251,E266,ANN101,ANN102
ignore = E501,W503,E241,E251,E266,ANN101,ANN102,ANN401,ANN002,ANN003
per-file-ignores =
*/__init__.py:F401,F403
sizebot/lib/proportions.py:E241,E272
Expand Down
2 changes: 1 addition & 1 deletion sizebot/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def halt(self, ctx: BotContext):
hidden = True
)
@commands.is_owner()
async def dump(self, ctx: BotContext, *, user: discord.Member = None):
async def dump(self, ctx: BotContext, *, user: discord.Member | None = None):
"""Dump a user's data."""
if user is None:
user = ctx.author
Expand Down
126 changes: 72 additions & 54 deletions sizebot/cogs/change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib.resources as pkg_resources
import logging
import random
from typing import cast
from typing import Literal, cast
from sizebot.lib import errors, utils
from sizebot.lib.digidecimal import Decimal

Expand All @@ -12,6 +12,7 @@
from sizebot.lib import changes, userdb, nickmanager
from sizebot.lib.diff import Diff, LimitedRate, Rate
from sizebot.lib.errors import ChangeMethodInvalidException
from sizebot.lib.guilddb import Guild
from sizebot.lib.objs import DigiObject, objects
from sizebot.lib.types import BotContext, StrToSend
from sizebot.lib.units import SV
Expand Down Expand Up @@ -52,7 +53,7 @@ async def change(self, ctx: BotContext, *, arg: LimitedRate | Rate | Diff | str)
`&change -1in/min until 1ft`
`&change -1mm/sec for 1hr`
"""
guildid = ctx.guild.id
guildid = cast(Guild, ctx.guild).id
userid = ctx.author.id
userdata = userdb.load(guildid, userid) # Load this data but don't use it as an ad-hoc user test.

Expand All @@ -61,13 +62,13 @@ async def change(self, ctx: BotContext, *, arg: LimitedRate | Rate | Diff | str)
amount = arg.amount

if style == "add":
userdata.height = userdata.height + cast(SV, amount)
userdata.height = SV(userdata.height + cast(SV, amount))
elif style == "multiply":
userdata.height = userdata.height * cast(Decimal, amount)
userdata.height = SV(userdata.height * cast(Decimal, amount))
elif style == "power":
userdata.scale = userdata.scale ** cast(Decimal, amount)
else:
raise ChangeMethodInvalidException
raise ChangeMethodInvalidException(style)
await nickmanager.nick_update(ctx.author)
userdb.save(userdata)
await ctx.send(f"{userdata.nickname} is now {userdata.height:m} ({userdata.height:u}) tall.")
Expand Down Expand Up @@ -113,10 +114,10 @@ async def eatme(self, ctx: BotContext):
guildid = ctx.guild.id
userid = ctx.author.id

userdata = userdb.load(guildid, userid)
randmult = round(random.randint(2, 20), 1)
change_user(guildid, userid, "multiply", randmult)
randmult = Decimal(round(random.randint(2, 20), 1))
change_user_mult(guildid, userid, randmult)
await nickmanager.nick_update(ctx.author)

userdata = userdb.load(guildid, userid)

lines = pkg_resources.read_text(sizebot.data, "eatme.txt").splitlines()
Expand All @@ -138,10 +139,10 @@ async def drinkme(self, ctx: BotContext):
guildid = ctx.guild.id
userid = ctx.author.id

userdata = userdb.load(guildid, userid)
randmult = round(random.randint(2, 20), 1)
change_user(guildid, ctx.author.id, "divide", randmult)
randmult = Decimal(round(random.randint(2, 20), 1))
change_user_div(guildid, userid, randmult)
await nickmanager.nick_update(ctx.author)

userdata = userdb.load(guildid, userid)

lines = pkg_resources.read_text(sizebot.data, "drinkme.txt").splitlines()
Expand Down Expand Up @@ -231,59 +232,76 @@ async def changeTask(self):
logger.error(utils.format_traceback(e))


def change_user(guildid: int, userid: int, changestyle: str, amount: SV):
changestyle = changestyle.lower()
if changestyle in ["add", "+", "a", "plus"]:
changestyle = "add"
if changestyle in ["subtract", "sub", "-", "minus"]:
changestyle = "subtract"
if changestyle in ["power", "exp", "pow", "exponent", "^", "**"]:
changestyle = "power"
if changestyle in ["multiply", "mult", "m", "x", "times", "*"]:
changestyle = "multiply"
if changestyle in ["divide", "d", "/", "div"]:
changestyle = "divide"
if changestyle in ["percent", "per", "perc", "%"]:
changestyle = "percent"

if changestyle not in ["add", "subtract", "multiply", "divide", "power", "percent"]:
raise errors.ChangeMethodInvalidException(changestyle)
ChangeStyles = Literal["add", "subtract", "multiply", "divide", "power", "percent"]

amountSV = None
amountVal = None
newamount = None

if changestyle in ["add", "subtract"]:
amountSV = SV.parse(amount)
elif changestyle in ["multiply", "divide", "power"]:
amountVal = Decimal(amount)
if amountVal == 1:
raise errors.ValueIsOneException
if amountVal == 0:
raise errors.ValueIsZeroException
elif changestyle in ["percent"]:
amountVal = Decimal(amount)
if amountVal == 0:
raise errors.ValueIsZeroException

userdata = userdb.load(guildid, userid)
def change_user(guildid: int, userid: int, changestyle: str, amount: SV | Decimal):
mapper = utils.AliasMapper[ChangeStyles]({
"add": ["add", "+", "a", "plus"],
"subtract": ["subtract", "sub", "-", "minus"],
"power": ["power", "exp", "pow", "exponent", "^", "**"],
"multiply": ["multiply", "mult", "m", "x", "times", "*"],
"divide": ["divide", "d", "/", "div"],
"percent": ["percent", "per", "perc", "%"],
})

if changestyle not in mapper:
raise errors.ChangeMethodInvalidException(changestyle)
changestyle = mapper[changestyle]

userdata = userdb.load(guildid, userid)
if changestyle == "add":
newamount = userdata.height + amountSV
change_user_add(guildid, userid, cast(SV, amount))
elif changestyle == "subtract":
newamount = userdata.height - amountSV
change_user_sub(guildid, userid, cast(SV, amount))
elif changestyle == "multiply":
newamount = userdata.height * amountVal
change_user_mult(guildid, userid, cast(Decimal, amount))
elif changestyle == "divide":
newamount = userdata.height / amountVal
change_user_div(guildid, userid, cast(Decimal, amount))
elif changestyle == "power":
userdata = userdata ** amountVal
elif changestyle == "percent":
newamount = userdata.height * (amountVal / 100)
change_user_power(guildid, userid, cast(Decimal, amount))
elif changestyle == "perc":
change_user_perc(guildid, userid, cast(Decimal, amount))

userdb.save(userdata)

if changestyle != "power":
userdata.height = newamount

def change_user_add(guildid: int, userid: int, amount: SV):
userdata = userdb.load(guildid, userid)
userdata.height = SV(userdata.height + amount)
userdb.save(userdata)


def change_user_sub(guildid: int, userid: int, amount: SV):
change_user_add(guildid, userid, SV(-amount))


def change_user_mult(guildid: int, userid: int, amount: Decimal):
userdata = userdb.load(guildid, userid)
if amount == 1:
raise errors.ValueIsOneException
if amount == 0:
raise errors.ValueIsZeroException
userdata.height = SV(userdata.height * amount)
userdb.save(userdata)


def change_user_div(guildid: int, userid: int, amount: Decimal):
change_user_mult(guildid, userid, Decimal(1) / amount)


def change_user_power(guildid: int, userid: int, amount: Decimal):
userdata = userdb.load(guildid, userid)
userdata.scale = userdata.scale ** amount
userdb.save(userdata)


def change_user_perc(guildid: int, userid: int, amount: Decimal):
userdata = userdb.load(guildid, userid)
if amount == 0:
raise errors.ValueIsZeroException
amount_perc = amount / 100
userdata.height = SV(userdata.height * amount_perc)
userdb.save(userdata)


Expand Down
8 changes: 4 additions & 4 deletions sizebot/cogs/multiplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ async def pushbutton(self, ctx: BotContext, user: discord.Member):
return
diff = userdata.button
if diff.changetype == "multiply":
userdata.height *= diff.amount
userdata.height = SV(userdata.height * diff.amount)
elif diff.changetype == "add":
userdata.height += diff.amount
userdata.height = SV(userdata.height + diff.amount)
elif diff.changetype == "power":
userdata = userdata ** diff.amount
userdata.scale = userdata.scale ** diff.amount
userdb.save(userdata)
await nickmanager.nick_update(user)
await ctx.send(f"You pushed {userdata.nickname}'s button! They are now **{userdata.height:,.3mu}** tall.")
Expand Down Expand Up @@ -164,7 +164,7 @@ async def changeother(self, ctx: BotContext, other: discord.Member, *, string: D
elif style == "power":
userdata = userdata ** amount
else:
raise ChangeMethodInvalidException
raise ChangeMethodInvalidException(style)
await nickmanager.nick_update(other)

userdb.save(userdata)
Expand Down
2 changes: 1 addition & 1 deletion sizebot/lib/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def toJSON(self) -> Any:
}


def start(userid: int, guildid: int, *, addPerSec: SV = 0, mulPerSec: Decimal = 1, stopSV: SV = None, stopTV: TV = None):
def start(userid: int, guildid: int, *, addPerSec: SV = 0, mulPerSec: Decimal = 1, stopSV: SV | None = None, stopTV: TV | None = None):
"""Start a new change task"""
startTime = lastRan = time.time()
change = Change(userid, guildid, addPerSec=addPerSec, mulPerSec=mulPerSec, stopSV=stopSV, stopTV=stopTV, startTime=startTime, lastRan=lastRan)
Expand Down
Loading

0 comments on commit c9812fc

Please sign in to comment.