mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 09:19:23 +01:00
games: Simplify/betterise handlers, create example dice handler
This commit is contained in:
parent
4054276ac0
commit
ed15af72ec
@ -19,12 +19,16 @@ dbname = utils.getDatabaseName('pylinkgames')
|
|||||||
|
|
||||||
# commands
|
# commands
|
||||||
class Command:
|
class Command:
|
||||||
def __init__(self, irc, name, args, sender, target):
|
def __init__(self, name, args, sender, target, from_to):
|
||||||
self.irc = irc
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.args = args
|
self.args = args
|
||||||
self.sender = sender
|
self.sender = sender
|
||||||
self.target = target
|
self.target = target
|
||||||
|
# from_to represents the channel if sent to a channel, and the sender
|
||||||
|
# if sent to the user directly. stops commands from having to worry
|
||||||
|
# about and handle sender vs target themselves for responses that can
|
||||||
|
# be public, but can also be sent privately for privmsgs
|
||||||
|
self.from_to = from_to
|
||||||
|
|
||||||
|
|
||||||
class CommandHandler:
|
class CommandHandler:
|
||||||
@ -65,7 +69,9 @@ class CommandHandler:
|
|||||||
log.debug('(%s) games.handle_messages: prefix is %r, target is %r', irc.name, prefix, target)
|
log.debug('(%s) games.handle_messages: prefix is %r, target is %r', irc.name, prefix, target)
|
||||||
|
|
||||||
# check public command prefixes
|
# check public command prefixes
|
||||||
|
from_to = numeric
|
||||||
if utils.isChannel(target):
|
if utils.isChannel(target):
|
||||||
|
from_to = target
|
||||||
if text.startswith(self.public_command.prefix):
|
if text.startswith(self.public_command.prefix):
|
||||||
text = text[len(self.public_command.prefix) - 1:]
|
text = text[len(self.public_command.prefix) - 1:]
|
||||||
else:
|
else:
|
||||||
@ -81,21 +87,35 @@ class CommandHandler:
|
|||||||
|
|
||||||
command_name = command_name.casefold()
|
command_name = command_name.casefold()
|
||||||
|
|
||||||
command = Command(irc, command_name, command_args, numeric, target)
|
command = Command(command_name, command_args, numeric, target, from_to)
|
||||||
|
|
||||||
# check for matching handler and dispatch
|
# check for matching handler and dispatch
|
||||||
handler = self.commands.get(command_name)
|
handler = self.commands.get(command_name)
|
||||||
if handler:
|
if handler:
|
||||||
handler(self, command)
|
handler(self, irc, command)
|
||||||
|
|
||||||
cmdhandler = CommandHandler()
|
cmdhandler = CommandHandler()
|
||||||
|
|
||||||
def help_cmd(command_handler, command):
|
|
||||||
|
# commands
|
||||||
|
def help_cmd(command_handler, irc, command):
|
||||||
"[command] -- Help for the given commands"
|
"[command] -- Help for the given commands"
|
||||||
print('COMMAND DETAILS:', command)
|
print('COMMAND DETAILS:', command)
|
||||||
|
# TODO(dan): Write help handler
|
||||||
|
irc.proto.notice(irc.games_user.uid, command.sender, '== Help ==')
|
||||||
|
|
||||||
cmdhandler.add_command('help', help_cmd)
|
cmdhandler.add_command('help', help_cmd)
|
||||||
|
|
||||||
|
|
||||||
|
def dice_cmd(command_handler, irc, command):
|
||||||
|
"<dice string> -- Roll the dice!"
|
||||||
|
# TODO(dan): Write dice handler
|
||||||
|
irc.proto.message(irc.games_user.uid, command.from_to, '42')
|
||||||
|
|
||||||
|
cmdhandler.add_command('d', dice_cmd)
|
||||||
|
cmdhandler.add_command('dice', dice_cmd)
|
||||||
|
|
||||||
|
|
||||||
# loading
|
# loading
|
||||||
def main(irc=None):
|
def main(irc=None):
|
||||||
"""Main function, called during plugin loading at start."""
|
"""Main function, called during plugin loading at start."""
|
||||||
@ -124,6 +144,8 @@ def initializeAll(irc):
|
|||||||
def handle_endburst(irc, numeric, command, args):
|
def handle_endburst(irc, numeric, command, args):
|
||||||
# TODO(dan): name/user/hostname to be configurable, possible status channel?
|
# TODO(dan): name/user/hostname to be configurable, possible status channel?
|
||||||
user = irc.proto.spawnClient("games", "g", irc.serverdata["hostname"])
|
user = irc.proto.spawnClient("games", "g", irc.serverdata["hostname"])
|
||||||
|
# TODO(dan): handle this more nicely, don't just append to games_user (esp.
|
||||||
|
# when/as we make CommandHandler applicable to more bots)
|
||||||
irc.games_user = user
|
irc.games_user = user
|
||||||
if numeric == irc.uplink:
|
if numeric == irc.uplink:
|
||||||
initializeAll(irc)
|
initializeAll(irc)
|
||||||
|
Loading…
Reference in New Issue
Block a user