Add utils.EventError, utilise it for stderr in commands.py/coins.py
This commit is contained in:
parent
7a54076de1
commit
ba3cd00c78
3 changed files with 31 additions and 46 deletions
|
@ -176,8 +176,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
try:
|
try:
|
||||||
coins = self._parse_coins(event["args_split"][0], DECIMAL_ZERO)
|
coins = self._parse_coins(event["args_split"][0], DECIMAL_ZERO)
|
||||||
except CoinParseException as e:
|
except CoinParseException as e:
|
||||||
event["stderr"].write("%s: %s" % (event["user"].nickname, str(e)))
|
raise utils.EventError("%s: %s" % (event["user"].nickname, str(e)))
|
||||||
return
|
|
||||||
|
|
||||||
self._give(event["server"], target, coins)
|
self._give(event["server"], target, coins)
|
||||||
event["stdout"].write("Gave '%s' %s coins" % (target.nickname,
|
event["stdout"].write("Gave '%s' %s coins" % (target.nickname,
|
||||||
|
@ -235,27 +234,23 @@ class Module(ModuleManager.BaseModule):
|
||||||
if coin_bet == "all":
|
if coin_bet == "all":
|
||||||
coin_bet = self._get_user_coins(event["user"])
|
coin_bet = self._get_user_coins(event["user"])
|
||||||
if coin_bet <= DECIMAL_ZERO:
|
if coin_bet <= DECIMAL_ZERO:
|
||||||
event["stderr"].write("%s: You have no coins to bet" %
|
raise utils.EventError("%s: You have no coins to bet" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
coin_bet = self._parse_coins(coin_bet, DECIMAL_ZERO)
|
coin_bet = self._parse_coins(coin_bet, DECIMAL_ZERO)
|
||||||
except CoinParseException as e:
|
except CoinParseException as e:
|
||||||
event["stderr"].write("%s: %s" % (event["user"].nickname,
|
raise utils.EventError("%s: %s" % (event["user"].nickname,
|
||||||
str(e)))
|
str(e)))
|
||||||
return
|
|
||||||
|
|
||||||
if not side_name in SIDES:
|
if not side_name in SIDES:
|
||||||
event["stderr"].write("%s: Please provide 'heads' or 'tails'" %
|
raise utils.EventError("%s: Please provide 'heads' or 'tails'" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
user_coins = self._get_user_coins(event["user"])
|
user_coins = self._get_user_coins(event["user"])
|
||||||
if coin_bet > user_coins:
|
if coin_bet > user_coins:
|
||||||
event["stderr"].write("%s: You don't have enough coins to bet" %
|
raise utils.EventError("%s: You don't have enough coins to bet" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
chosen_side = secrets.choice(list(SIDES.keys()))
|
chosen_side = secrets.choice(list(SIDES.keys()))
|
||||||
win = side_name == chosen_side
|
win = side_name == chosen_side
|
||||||
|
@ -288,39 +283,34 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
if event["user"].get_id() == event["server"].get_user(event[
|
if event["user"].get_id() == event["server"].get_user(event[
|
||||||
"args_split"][0]).get_id():
|
"args_split"][0]).get_id():
|
||||||
event["stderr"].write("%s: You can't send coins to yourself" %
|
raise utils.EventError("%s: You can't send coins to yourself" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
send_amount = event["args_split"][1]
|
send_amount = event["args_split"][1]
|
||||||
try:
|
try:
|
||||||
send_amount = self._parse_coins(send_amount, DECIMAL_ZERO)
|
send_amount = self._parse_coins(send_amount, DECIMAL_ZERO)
|
||||||
except CoinParseException as e:
|
except CoinParseException as e:
|
||||||
event["stderr"].write("%s: %s" % (event["user"].nickname, str(e)))
|
raise utils.EventError("%s: %s" % (event["user"].nickname, str(e)))
|
||||||
return
|
|
||||||
|
|
||||||
user_coins = self._get_user_coins(event["user"])
|
user_coins = self._get_user_coins(event["user"])
|
||||||
redeem_amount = self._redeem_amount(event["server"])
|
redeem_amount = self._redeem_amount(event["server"])
|
||||||
new_user_coins = user_coins-send_amount
|
new_user_coins = user_coins-send_amount
|
||||||
|
|
||||||
if user_coins == DECIMAL_ZERO:
|
if user_coins == DECIMAL_ZERO:
|
||||||
event["stderr"].write("%s: You have no coins" %
|
raise utils.EventError("%s: You have no coins" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
elif new_user_coins < redeem_amount:
|
elif new_user_coins < redeem_amount:
|
||||||
event["stderr"].write(
|
raise utils.EventError(
|
||||||
"%s: You cannot send an amount of money that puts"
|
"%s: You cannot send an amount of money that puts"
|
||||||
" you below %s coins" % (
|
" you below %s coins" % (
|
||||||
event["user"].nickname,
|
event["user"].nickname,
|
||||||
self._coin_str(redeem_amount)))
|
self._coin_str(redeem_amount)))
|
||||||
return
|
|
||||||
|
|
||||||
target_user = event["server"].get_user(event["args_split"][0])
|
target_user = event["server"].get_user(event["args_split"][0])
|
||||||
target_user_coins = self._get_user_coins(target_user)
|
target_user_coins = self._get_user_coins(target_user)
|
||||||
if target_user_coins == None:
|
if target_user_coins == None:
|
||||||
event["stderr"].write("%s: You can only send coins to users that "
|
raise utils.EventError("%s: You can only send coins to users that "
|
||||||
"have had coins before" % event["user"].nickname)
|
"have had coins before" % event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
self._move(event["user"], target_user, send_amount)
|
self._move(event["user"], target_user, send_amount)
|
||||||
|
|
||||||
|
@ -336,37 +326,32 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
bets = event["args_split"][0].lower().split(",")
|
bets = event["args_split"][0].lower().split(",")
|
||||||
if "0" in bets:
|
if "0" in bets:
|
||||||
event["stderr"].write("%s: You can't bet on 0" %
|
raise utils.EventError("%s: You can't bet on 0" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
bet_amounts = [amount.lower() for amount in event["args_split"][1:]]
|
bet_amounts = [amount.lower() for amount in event["args_split"][1:]]
|
||||||
if len(bet_amounts) < len(bets):
|
if len(bet_amounts) < len(bets):
|
||||||
event["stderr"].write("%s: Please provide an amount for each bet" %
|
raise utils.EventError("%s: Please provide an amount for each bet" %
|
||||||
event["user"].nickanme)
|
event["user"].nickanme)
|
||||||
return
|
|
||||||
if len(bet_amounts) == 1 and bet_amounts[0] == "all":
|
if len(bet_amounts) == 1 and bet_amounts[0] == "all":
|
||||||
bet_amounts[0] = self._get_user_coins(event["user"])
|
bet_amounts[0] = self._get_user_coins(event["user"])
|
||||||
if bet_amounts[0] <= DECIMAL_ZERO:
|
if bet_amounts[0] <= DECIMAL_ZERO:
|
||||||
event["stderr"].write("%s: You have no coins to bet" %
|
raise utils.EventError("%s: You have no coins to bet" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
bet_amounts[0] = self._coin_str(bet_amounts[0])
|
bet_amounts[0] = self._coin_str(bet_amounts[0])
|
||||||
|
|
||||||
for i, bet_amount in enumerate(bet_amounts):
|
for i, bet_amount in enumerate(bet_amounts):
|
||||||
try:
|
try:
|
||||||
bet_amount = utils._parse_coins(bet_amount, DECIMAL_ZERO)
|
bet_amount = utils._parse_coins(bet_amount, DECIMAL_ZERO)
|
||||||
except CoinParseException as e:
|
except CoinParseException as e:
|
||||||
event["stderr"].write("%s: %s" % (event["user"].nickname,
|
raise utils.EventError("%s: %s" % (event["user"].nickname,
|
||||||
str(e)))
|
str(e)))
|
||||||
return
|
|
||||||
|
|
||||||
bet_amount_total = sum(bet_amounts)
|
bet_amount_total = sum(bet_amounts)
|
||||||
|
|
||||||
user_coins = self._get_user_coins(event["user"])
|
user_coins = self._get_user_coins(event["user"])
|
||||||
if bet_amount_total > user_coins:
|
if bet_amount_total > user_coins:
|
||||||
event["stderr"].write("%s: You don't have enough coins to bet" %
|
raise utils.EventError("%s: You don't have enough coins to bet" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
# black, red, odds, evens, low (1-18), high (19-36)
|
# black, red, odds, evens, low (1-18), high (19-36)
|
||||||
# 1dozen (1-12), 2dozen (13-24), 3dozen (25-36)
|
# 1dozen (1-12), 2dozen (13-24), 3dozen (25-36)
|
||||||
|
@ -383,7 +368,6 @@ class Module(ModuleManager.BaseModule):
|
||||||
event["user"].nickname, loss))
|
event["user"].nickname, loss))
|
||||||
return
|
return
|
||||||
|
|
||||||
failed = False
|
|
||||||
colour = "red" if choice in RED else "black"
|
colour = "red" if choice in RED else "black"
|
||||||
for i, bet in enumerate(bets):
|
for i, bet in enumerate(bets):
|
||||||
street_match = REGEX_STREET.match(bet)
|
street_match = REGEX_STREET.match(bet)
|
||||||
|
@ -418,16 +402,13 @@ class Module(ModuleManager.BaseModule):
|
||||||
elif bet.isdigit() and (1 <= int(bet) <= 36):
|
elif bet.isdigit() and (1 <= int(bet) <= 36):
|
||||||
odds = 35*(choice == int(bet))
|
odds = 35*(choice == int(bet))
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("%s: Unknown bet" %
|
raise utils.EventError("%s: Unknown bet" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
failed = True
|
|
||||||
break
|
|
||||||
if odds == 0:
|
if odds == 0:
|
||||||
losses[bet] = bet_amounts[i]
|
losses[bet] = bet_amounts[i]
|
||||||
else:
|
else:
|
||||||
winnings[bet] = [odds, bet_amounts[i]*odds]
|
winnings[bet] = [odds, bet_amounts[i]*odds]
|
||||||
if failed:
|
|
||||||
return
|
|
||||||
|
|
||||||
winnings_str = ["%s for %s (%d to 1)" % (winnings[bet][1], bet,
|
winnings_str = ["%s for %s (%d to 1)" % (winnings[bet][1], bet,
|
||||||
winnings[bet][0]) for bet in winnings.keys()]
|
winnings[bet][0]) for bet in winnings.keys()]
|
||||||
|
@ -485,17 +466,15 @@ class Module(ModuleManager.BaseModule):
|
||||||
if event["args_split"]:
|
if event["args_split"]:
|
||||||
amount = event["args_split"][0]
|
amount = event["args_split"][0]
|
||||||
if not amount.isdigit():
|
if not amount.isdigit():
|
||||||
event["stderr"].write("%s: Please provide a positive number "
|
raise utils.EventError("%s: Please provide a positive number "
|
||||||
"of tickets to buy" % event["user"].nickname)
|
"of tickets to buy" % event["user"].nickname)
|
||||||
return
|
|
||||||
amount = int(amount)
|
amount = int(amount)
|
||||||
|
|
||||||
user_coins = self._get_user_coins(event["user"])
|
user_coins = self._get_user_coins(event["user"])
|
||||||
coin_amount = decimal.Decimal(LOTTERY_BUYIN)*amount
|
coin_amount = decimal.Decimal(LOTTERY_BUYIN)*amount
|
||||||
if coin_amount > user_coins:
|
if coin_amount > user_coins:
|
||||||
event["stderr"].write("%s: You don't have enough coins" %
|
raise utils.EventError("%s: You don't have enough coins" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
self._take(event["server"], event["user"], coin_amount)
|
self._take(event["server"], event["user"], coin_amount)
|
||||||
|
|
||||||
|
|
|
@ -165,12 +165,15 @@ class Module(ModuleManager.BaseModule):
|
||||||
args = " ".join(args_split)
|
args = " ".join(args_split)
|
||||||
server = event["server"]
|
server = event["server"]
|
||||||
user = event["user"]
|
user = event["user"]
|
||||||
self.events.on("received.command").on(command
|
try:
|
||||||
).call_limited(1, user=user, server=server,
|
self.events.on("received.command").on(command
|
||||||
target=target, args=args,
|
).call_unsafe_limited(1, user=user, server=server,
|
||||||
args_split=args_split, stdout=stdout, stderr=stderr,
|
target=target, args=args, tags=event["tags"],
|
||||||
command=command.lower(), is_channel=is_channel,
|
args_split=args_split, stdout=stdout, stderr=stderr,
|
||||||
tags=event["tags"])
|
command=command.lower(), is_channel=is_channel)
|
||||||
|
except EventManager.EventError as e:
|
||||||
|
stdout.write(str(e))
|
||||||
|
|
||||||
if not hook.kwargs.get("skip_out", False):
|
if not hook.kwargs.get("skip_out", False):
|
||||||
stdout.send()
|
stdout.send()
|
||||||
stderr.send()
|
stderr.send()
|
||||||
|
|
|
@ -134,6 +134,9 @@ def get_closest_setting(event, setting, default=None):
|
||||||
def prevent_highlight(nickname):
|
def prevent_highlight(nickname):
|
||||||
return nickname[0]+"\u200c"+nickname[1:]
|
return nickname[0]+"\u200c"+nickname[1:]
|
||||||
|
|
||||||
|
class EventError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def _set_get_append(obj, setting, item):
|
def _set_get_append(obj, setting, item):
|
||||||
if not hasattr(obj, setting):
|
if not hasattr(obj, setting):
|
||||||
setattr(obj, setting, [])
|
setattr(obj, setting, [])
|
||||||
|
|
Loading…
Reference in a new issue