Cleaner code for optional parameters

This commit is contained in:
moneromooo 2014-12-26 17:13:26 +00:00
parent 79f3a00415
commit dd98853861

View file

@ -152,6 +152,14 @@ def Part(chan):
def Who(chan): def Who(chan):
SendIRC ( 'WHO ' + chan) SendIRC ( 'WHO ' + chan)
def IsParamPresent(parms,idx):
return len(parms) > idx
def GetParam(parms,idx):
if IsParamPresent(parms,idx):
return parms[idx]
return None
def CheckRegistered(nick,ifyes,yesdata,ifno,nodata): def CheckRegistered(nick,ifyes,yesdata,ifno,nodata):
if nick not in calltable: if nick not in calltable:
calltable[nick] = [] calltable[nick] = []
@ -305,14 +313,13 @@ def Rain(nick,data):
except Exception,e: except Exception,e:
SendTo(sendto, "Usage: rain amount [users]") SendTo(sendto, "Usage: rain amount [users]")
return return
try: users = GetParam(data,2)
if data[2] == None: if users:
users = None try:
else: users=long(users)
users=long(data[2]) except Exception,e:
except Exception,e: SendTo(sendto, "Usage: rain amount [users]")
SendTo(sendto, "Usage: rain amount [users]") return
return
if amount <= 0: if amount <= 0:
SendTo(sendto, "Usage: rain amount [users]") SendTo(sendto, "Usage: rain amount [users]")
@ -398,10 +405,10 @@ def IsValidAddress(address):
def Withdraw(nick,data): def Withdraw(nick,data):
address=data[0] address=data[0]
amount=data[1]
if not IsValidAddress(address): if not IsValidAddress(address):
SendTo(nick, "Invalid address") SendTo(nick, "Invalid address")
return return
amount = GetParam(data,1)
if amount: if amount:
try: try:
famount=float(amount) famount=float(amount)
@ -994,15 +1001,14 @@ while True:
CheckRegistered(GetNick(who),GetBalance,[sendto],SendTo,"You must be registered with Freenode to query balance") CheckRegistered(GetNick(who),GetBalance,[sendto],SendTo,"You must be registered with Freenode to query balance")
elif cmd[0] == 'tip': elif cmd[0] == 'tip':
if len(cmd) == 3: if len(cmd) == 3:
CheckRegistered(GetNick(who),Tip,[sendto,cmd[1],cmd[2]],SendTo,"You must be registered with Freenode to tip") parms=[sendto]
parms.extend(cmd[1:])
CheckRegistered(GetNick(who),Tip,parms,SendTo,"You must be registered with Freenode to tip")
else: else:
SendTo(GetNick(who), "Usage: !tip nick amount"); SendTo(GetNick(who), "Usage: !tip nick amount");
elif cmd[0] == 'withdraw': elif cmd[0] == 'withdraw':
if len(cmd) == 2 or len(cmd) == 3: if len(cmd) == 2 or len(cmd) == 3:
amount = None CheckRegistered(GetNick(who),Withdraw,cmd[1:],SendTo,"You must be registered with Freenode to withdraw")
if len(cmd) == 3:
amount = cmd[2]
CheckRegistered(GetNick(who),Withdraw,[cmd[1],amount],SendTo,"You must be registered with Freenode to withdraw")
else: else:
SendTo(GetNick(who), "Usage: !withdraw address"); SendTo(GetNick(who), "Usage: !withdraw address");
elif cmd[0] == 'info': elif cmd[0] == 'info':
@ -1010,12 +1016,11 @@ while True:
elif cmd[0] == 'rain': elif cmd[0] == 'rain':
if chan[0] == '#': if chan[0] == '#':
if len(cmd) == 2 or len(cmd) == 3: if len(cmd) == 2 or len(cmd) == 3:
users = None parms=[chan]
if len(cmd) == 3: parms.extend(cmd[1:])
users = cmd[2] CheckRegistered(GetNick(who),Rain,parms,SendTo,"You must be registered with Freenode to rain")
CheckRegistered(GetNick(who),Rain,[chan,cmd[1],users],SendTo,"You must be registered with Freenode to rain")
else: else:
SendTo(sendto, "Usage: !rain amount [users]"); SendTo(sendto, "Usage: !rain amount [users]");
else: else:
SendTo(sendto, "Raining can only be done in a channel") SendTo(sendto, "Raining can only be done in a channel")
# admin commands # admin commands
@ -1035,7 +1040,7 @@ while True:
CheckAdmin(GetNick(who),DumpUsers,None,SendTo,"You must be admin") CheckAdmin(GetNick(who),DumpUsers,None,SendTo,"You must be admin")
elif cmd[0] == 'show_activity': elif cmd[0] == 'show_activity':
if len(cmd)==3: if len(cmd)==3:
CheckAdmin(GetNick(who),ShowActivity,[cmd[1],cmd[2]],SendTo,"You must be admin") CheckAdmin(GetNick(who),ShowActivity,cmd[1:],SendTo,"You must be admin")
else: else:
SendTo(sendto,"Usage: show_activity channel nick") SendTo(sendto,"Usage: show_activity channel nick")
else: else: