Added Command.execute_cmd for use in Player commands requiring session info. Resolves #1123.

This commit is contained in:
Griatch 2016-11-22 20:33:50 +01:00
parent 45a4d01a0a
commit 897036e7cb
4 changed files with 37 additions and 15 deletions

View file

@ -324,6 +324,28 @@ class Command(with_metaclass(CommandMeta, object)):
session = to_obj.sessions.get()
to_obj.msg(text=text, from_obj=from_obj, session=session, **kwargs)
def execute_cmd(self, raw_string, session=None, obj=None, **kwargs):
"""
A shortcut of execute_cmd on the caller. It appends the
session automatically.
Args:
raw_string (str): Execute this string as a command input.
session (Session, optional): If not given, the current command's Session will be used.
obj (Object or Player, optional): Object or Player on which to call the execute_cmd.
If not given, self.caller will be used.
Kwargs:
Other keyword arguments will be added to the found command
object instace as variables before it executes. This is
unused by default Evennia but may be used to set flags and
change operating paramaters for commands at run-time.
"""
obj = self.caller if obj is None else obj
session = self.session if session is None else session
obj.execute_cmd(raw_string, session=session, **kwargs)
# Common Command hooks
def at_pre_cmd(self):

View file

@ -436,9 +436,9 @@ class CmdMvAttr(ObjManipCommand):
# simply use @cpattr for all the functionality
if "copy" in self.switches:
self.caller.execute_cmd("@cpattr %s" % self.args)
self.execute_cmd("@cpattr %s" % self.args)
else:
self.caller.execute_cmd("@cpattr/move %s" % self.args)
self.execute_cmd("@cpattr/move %s" % self.args)
class CmdCreate(ObjManipCommand):
@ -901,7 +901,7 @@ class CmdTunnel(COMMAND_DEFAULT_CLASS):
# build the string we will use to call @dig
digstring = "@dig%s %s = %s;%s%s" % (telswitch, roomname,
exitname, exitshort, backstring)
self.caller.execute_cmd(digstring)
self.execute_cmd(digstring)
class CmdLink(COMMAND_DEFAULT_CLASS):

View file

@ -217,7 +217,7 @@ class CmdAllCom(COMMAND_DEFAULT_CLASS):
caller = self.caller
args = self.args
if not args:
caller.execute_cmd("@channels")
self.execute_cmd("@channels")
self.msg("(Usage: allcom on | off | who | destroy)")
return
@ -227,18 +227,18 @@ class CmdAllCom(COMMAND_DEFAULT_CLASS):
channels = [chan for chan in ChannelDB.objects.get_all_channels()
if chan.access(caller, 'listen')]
for channel in channels:
caller.execute_cmd("addcom %s" % channel.key)
self.execute_cmd("addcom %s" % channel.key)
elif args == "off":
#get names all subscribed channels and disconnect from them all
channels = ChannelDB.objects.get_subscriptions(caller)
for channel in channels:
caller.execute_cmd("delcom %s" % channel.key)
self.execute_cmd("delcom %s" % channel.key)
elif args == "destroy":
# destroy all channels you control
channels = [chan for chan in ChannelDB.objects.get_all_channels()
if chan.access(caller, 'control')]
for channel in channels:
caller.execute_cmd("@cdestroy %s" % channel.key)
self.execute_cmd("@cdestroy %s" % channel.key)
elif args == "who":
# run a who, listing the subscribers on visible channels.
string = "\n{CChannel subscriptions{n"
@ -301,7 +301,7 @@ class CmdChannels(COMMAND_DEFAULT_CLASS):
"%s" % ",".join(nick.db_key for nick in make_iter(nicks)
if nick and nick.value[3].lower() == clower),
chan.db.desc])
caller.msg("\n{wChannel subscriptions{n (use {w@channels{n to list all, {waddcom{n/{wdelcom{n to sub/unsub):{n\n%s" % comtable)
self.msg("\n{wChannel subscriptions{n (use {w@channels{n to list all, {waddcom{n/{wdelcom{n to sub/unsub):{n\n%s" % comtable)
else:
# full listing (of channels caller is able to listen to)
comtable = evtable.EvTable("{wsub{n", "{wchannel{n", "{wmy aliases{n", "{wlocks{n", "{wdescription{n", maxwidth=_DEFAULT_WIDTH)
@ -325,7 +325,7 @@ class CmdChannels(COMMAND_DEFAULT_CLASS):
chan.db.desc])
comtable.reformat_column(0, width=9)
comtable.reformat_column(3, width=14)
caller.msg("\n{wAvailable channels{n (use {wcomlist{n,{waddcom{n and {wdelcom{n to manage subscriptions):\n%s" % comtable)
self.msg("\n{wAvailable channels{n (use {wcomlist{n,{waddcom{n and {wdelcom{n to manage subscriptions):\n%s" % comtable)
class CmdCdestroy(COMMAND_DEFAULT_CLASS):
@ -512,7 +512,7 @@ class CmdCWho(COMMAND_DEFAULT_CLASS):
self.msg(string)
return
string = "\n{CChannel subscriptions{n"
string += "\n{w%s:{n\n %s" % (channel.key, channel.wholist)
string += "\n{w%s:{n\n %s" % (channel.key, channel.wholist)
self.msg(string.strip())
@ -844,7 +844,7 @@ class CmdIRC2Chan(COMMAND_DEFAULT_CLASS):
for ircbot in ircbots:
ircinfo = "%s (%s:%s)" % (ircbot.db.irc_channel, ircbot.db.irc_network, ircbot.db.irc_port)
table.add_row(ircbot.id, ircbot.db.irc_botname, ircbot.db.ev_channel, ircinfo, ircbot.db.irc_ssl)
self.caller.msg(table)
self.msg(table)
else:
self.msg("No irc bots found.")
return
@ -954,7 +954,7 @@ class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
"{wRSS feed URL{n", border="cells", maxwidth=_DEFAULT_WIDTH)
for rssbot in rssbots:
table.add_row(rssbot.id, rssbot.db.rss_rate, rssbot.db.ev_channel, rssbot.db.rss_url)
self.caller.msg(table)
self.msg(table)
else:
self.msg("No rss bots found.")
return

View file

@ -214,9 +214,9 @@ class CmdCharDelete(COMMAND_DEFAULT_CLASS):
caller.db._playable_characters = [char for char
in caller.db._playable_characters if char != delobj]
delobj.delete()
caller.msg("Character '%s' was permanently deleted." % key)
self.msg("Character '%s' was permanently deleted." % key)
else:
caller.msg("Deletion was aborted.")
self.msg("Deletion was aborted.")
del caller.ndb._char_to_delete
match = match[0]
@ -581,7 +581,7 @@ class CmdOption(COMMAND_DEFAULT_CLASS):
if val and name in validators:
optiondict = update(name, val, validators[name])
else:
self.session.msg("|rNo option named '|w%s|r'." % name)
self.msg("|rNo option named '|w%s|r'." % name)
if optiondict:
# a valid setting
if "save" in self.switches: