mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 09:19:23 +01:00
games: request/remove commands
This commit is contained in:
parent
45651858e4
commit
c2e3ce5bdf
@ -19,10 +19,8 @@ exportdb_timer = None
|
|||||||
|
|
||||||
|
|
||||||
class DataStore:
|
class DataStore:
|
||||||
default_store = {
|
# will come into play with subclassing and db version upgrading
|
||||||
'version': 1,
|
initial_version = 1
|
||||||
'channels': {},
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, name, filename, db_format='json', save_frequency={'seconds': 30}):
|
def __init__(self, name, filename, db_format='json', save_frequency={'seconds': 30}):
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -52,6 +50,7 @@ class DataStore:
|
|||||||
self._store = json.loads(open(self._filename, 'r').read())
|
self._store = json.loads(open(self._filename, 'r').read())
|
||||||
except (ValueError, IOError, FileNotFoundError):
|
except (ValueError, IOError, FileNotFoundError):
|
||||||
log.exception('(db:{}) failed to load existing db, creating new one in memory'.format(self.name))
|
log.exception('(db:{}) failed to load existing db, creating new one in memory'.format(self.name))
|
||||||
|
self.put('db.version', self.initial_version)
|
||||||
else:
|
else:
|
||||||
raise Exception('(db:{}) Data store format [{}] not recognised'.format(self.name, self._format))
|
raise Exception('(db:{}) Data store format [{}] not recognised'.format(self.name, self._format))
|
||||||
|
|
||||||
@ -145,7 +144,7 @@ class Command:
|
|||||||
|
|
||||||
|
|
||||||
class CommandHandler:
|
class CommandHandler:
|
||||||
def __init__(self, default_help_cmd=True):
|
def __init__(self, default_help_cmd=True, default_request_cmds=True):
|
||||||
self.public_command_prefix = '.'
|
self.public_command_prefix = '.'
|
||||||
self.commands = {}
|
self.commands = {}
|
||||||
self.command_help = None
|
self.command_help = None
|
||||||
@ -153,19 +152,78 @@ class CommandHandler:
|
|||||||
# default commands
|
# default commands
|
||||||
if default_help_cmd:
|
if default_help_cmd:
|
||||||
self.add('help', self.help_cmd)
|
self.add('help', self.help_cmd)
|
||||||
|
if default_request_cmds:
|
||||||
|
self.add('request', self.request_cmd)
|
||||||
|
self.add('remove', self.remove_cmd)
|
||||||
|
|
||||||
def add(self, name, handler):
|
def add(self, name, handler):
|
||||||
self.commands[name.casefold()] = handler
|
self.commands[name.casefold()] = handler
|
||||||
self.command_help = None
|
self.command_help = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def help_cmd(self, irc, user, command):
|
def help_cmd(self, bot, irc, user, 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
|
# TODO(dan): Write help handler
|
||||||
irc.proto.notice(user.uid, command.sender, '== Help ==')
|
irc.proto.notice(user.uid, command.sender, '== Help ==')
|
||||||
|
|
||||||
def handle_messages(self, user, irc, numeric, command, args):
|
@staticmethod
|
||||||
|
def request_cmd(self, bot, irc, user, command):
|
||||||
|
"<channel> -- Make this bot join your channel!"
|
||||||
|
channel = command.args.split(' ', 1)[0]
|
||||||
|
if channel is None:
|
||||||
|
return
|
||||||
|
# TODO: casefold this as per irc net
|
||||||
|
channame = channel.lower()
|
||||||
|
|
||||||
|
# make sure they're an op in there
|
||||||
|
channel = irc.channels.get(channame)
|
||||||
|
if channel is None or not channel.isOpPlus(command.sender):
|
||||||
|
irc.proto.notice(user.uid, command.sender, "You are not an op in that channel")
|
||||||
|
return
|
||||||
|
|
||||||
|
# check if we're already joined to the channel
|
||||||
|
joinedkey = 'channels.joined_to {} {}'.format(irc.name, channame)
|
||||||
|
joined_to_chan = bot.db.get(joinedkey, default=False)
|
||||||
|
|
||||||
|
if joined_to_chan:
|
||||||
|
irc.proto.notice(user.uid, command.sender, "I'm already joined to that channel!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# join the channel
|
||||||
|
irc.proto.notice(user.uid, command.sender, "Joining channel ".format(channame))
|
||||||
|
bot.db.put(joinedkey, True)
|
||||||
|
irc.proto.join(user.uid, channame)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_cmd(self, bot, irc, user, command):
|
||||||
|
"<channel> -- Make this bot leave your channel!"
|
||||||
|
channel = command.args.split(' ', 1)[0]
|
||||||
|
if channel is None:
|
||||||
|
return
|
||||||
|
# TODO: casefold this as per irc net
|
||||||
|
channame = channel.lower()
|
||||||
|
|
||||||
|
# make sure they're an op in there
|
||||||
|
channel = irc.channels.get(channame)
|
||||||
|
if channel is None or not channel.isOpPlus(command.sender):
|
||||||
|
irc.proto.notice(user.uid, command.sender, "You are not an op in that channel")
|
||||||
|
return
|
||||||
|
|
||||||
|
# check if we're already joined to the channel
|
||||||
|
joinedkey = 'channels.joined_to {} {}'.format(irc.name, channame)
|
||||||
|
joined_to_chan = bot.db.get(joinedkey, default=False)
|
||||||
|
|
||||||
|
if not joined_to_chan:
|
||||||
|
irc.proto.notice(user.uid, command.sender, "I'm not in that channel!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# join the channel
|
||||||
|
irc.proto.notice(user.uid, command.sender, "Leaving channel ".format(channame))
|
||||||
|
bot.db.delete(joinedkey)
|
||||||
|
irc.proto.part(user.uid, channame)
|
||||||
|
|
||||||
|
def handle_messages(self, bot, user, irc, numeric, command, args):
|
||||||
notice = (command in ('NOTICE', 'PYLINK_SELF_NOTICE'))
|
notice = (command in ('NOTICE', 'PYLINK_SELF_NOTICE'))
|
||||||
target = args['target']
|
target = args['target']
|
||||||
text = args['text']
|
text = args['text']
|
||||||
@ -213,7 +271,7 @@ class CommandHandler:
|
|||||||
# 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, irc, user, command)
|
handler(self, bot, irc, user, command)
|
||||||
|
|
||||||
|
|
||||||
# bot clients
|
# bot clients
|
||||||
@ -241,13 +299,18 @@ class BotClient:
|
|||||||
user = irc.proto.spawnClient(self.name, self.name, irc.serverdata["hostname"])
|
user = irc.proto.spawnClient(self.name, self.name, irc.serverdata["hostname"])
|
||||||
irc.bot_clients[self.name] = user
|
irc.bot_clients[self.name] = user
|
||||||
|
|
||||||
|
# join required channels
|
||||||
|
for key in self.db.list_keys(prefix='channels.joined_to {}'.format(irc.name)):
|
||||||
|
channel = key.rsplit(' ', 1)[-1]
|
||||||
|
irc.proto.join(user.uid, channel)
|
||||||
|
|
||||||
def handle_messages(self, irc, numeric, command, args):
|
def handle_messages(self, irc, numeric, command, args):
|
||||||
# make sure we're spawned
|
# make sure we're spawned
|
||||||
user = irc.bot_clients.get(self.name)
|
user = irc.bot_clients.get(self.name)
|
||||||
if user is None:
|
if user is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.cmds.handle_messages(user, irc, numeric, command, args)
|
self.cmds.handle_messages(self, user, irc, numeric, command, args)
|
||||||
|
|
||||||
gameclient = BotClient('games')
|
gameclient = BotClient('games')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user