The idea of doing this "within" has_op
is probably preventing you from making progress.
Make it work at all and then consider how you might improve the factoring of the implementation.
Notice that get_names
has the data that you're interested in already. What do you want to do with that information? Check to see if it contains a certain name? For that, you'll need to know which name you're considering. To do this, you can use the feature of Deferred.addCallback
that lets you pass an extra argument to the callback:
def get_names(self, nicklist, user):
if user in nicklist:
log.msg("%r has op!" % (user,))
def has_op(self, channel, user):
self.names('#' + channel).addCallback(self.get_names, user)
Now perhaps what you really want isn't to log this information but to make a decision based on it in change_mode
(I'm only guessing, of course).
If this is so, then you want to take advantage of another Deferred
feature - the ability of each callback attached to a Deferred
to process and change the result of the Deferred
.
You can change has_op
so that instead of logging information it also returns a Deferred
- but this Deferred
can have a True
result if the given user has op in the given channel and a False
result of they don't.
def get_names(self, nicklist, user):
return user in nicklist
def has_op(self, channel, user):
return self.names('#' + channel).addCallback(self.get_names, user)
Now you can use this functionality in change_mode
:
def change_mode(self, channel, user, msg):
target = msg[5:-3]
checking = self.has_op(channel, user)
checking.addCallback(self._ofIfNotOp, channel, target)
def _opIfNotOp(self, isOp, channel, user):
if not isOp:
self.mode(channel, True, '+o', user=user)