Reputation: 99
Alright, I'm using a dictionary, such as:
l = {'@': self.ops, '~': self.ops, '&': self.ops, '+': self.voices, '%': self.voices}
Then what I want to do is basically say, if the "User" (an argument) has '@' in front of their name, then assign the variable prefix as '@', if they have '~' in front of their name, assign the variable prefix as '~' and if they have '&' in front of their name, assign the variable prefix as '&'. So for this I need if statements and be comparing them to self.ops value.
Then I want to basically say that if the user has '%' in front of their name, set the prefix variable as '%' else set it as '+' (since there are only two keys for those values). So for this I need an if statement and be comparing them to self.voices value.
The current code where the prefix variable is assigned
Currently all it does is check if user's prefix / symbol before their name is in the dictionary value self.ops, regardless of which one, and sets it as '@', otherwise sets it as '+'. I want it to be more specific for each symbol. So for example if it's & then the variable prefix becomes '&'
def irc_RPL_NAMREPLY(self, *arg):
if not arg[1][2].lower() == self.factory.channel:
return
for name in arg[1][3].split():
mode = name[0]
l = {'@': self.ops, '~': self.ops, '&': self.ops, '+': self.voices, '%': self.voices}
if mode in l:
l[mode].add(name[:])
def left(self, channel):
if channel.lower() == self.factory.channel:
self.ops = None
self.voices = None
@channel
def modeChanged(self, user, channel, set, modes, args):
ll = {'o' : self.ops, 'q' : self.ops, 'a' : self.ops, 'v' : self.voices, 'h' : self.voices}
for i in range(len(args)):
mode, name = modes[i], args[i]
if mode not in ll:
continue
l = ll[mode]
if set:
l.add(name)
elif not set:
l.discard(name)
@channel
def privmsg(self, user, channel, msg):
if user in self.ops or user in self.voices:
prefix = '@' if user in self.ops else '+'
alias = self.factory.aliases.get(user, user)
if msg.startswith(self.factory.commandprefix) and user in self.ops:
self.unaliased_name = user
self.name = prefix + alias
input = msg[len(self.factory.commandprefix):]
result = commands.handle_input(self, input)
if result is not None:
self.send("%s: %s" % (user, result))
elif msg.startswith(self.factory.chatprefix):
max_len = MAX_IRC_CHAT_SIZE - len(self.protocol.server_prefix) - 1
msg = msg[len(self.factory.chatprefix):].strip()
message = ("<%s> %s" % (prefix + alias, msg))[:max_len]
message = message.decode('cp1252')
print message.encode('ascii', 'replace')
self.factory.server.send_chat(encode(message))
Thank you in advance for any help.
If my question is unclear do notify me and I'll do my best to correct it. I've checked around on other questions, but because of the nature of my question, I can't seem to find questions like this one. Apologies otherwise.
EDIT: Further detailed purpose of this
Basically this is IRC chat related. Channel operators/users will have symbols as prefixes before their name (eg. @Shaz, ~Shaz, &Shaz, +Shaz or %Shaz). I'm running servers for a game which use the IRC chat channel(s) to communicate to players in-game. What I'm trying to do is make the prefixes taken from the users name (@, ~, &, + and %) from the channel, to send through the chat in-game, when the user enters a message (eg. @Shaz: hi blahblah). As far any other details go, I don't think there's much relevance. All I need to know is how to use multiple if statements to assign a variable, so it sends any of the above symbols based on which one the user is using (which is basically what the dictionary determines), to do this I need to somehow be able to set the correct symbol to the "prefixes" variable based on which the dictionary finds as the one that the user has in front of their name. So then that can be used to be the symbol as the one which sends through the chat, so if I was "&Shaz" when I'd say something to the game chat it would be like "&Shaz: hi blahblah". Currently no matter what symbol I'm using in front of my name, it'll just send "@Shaz: hi blahblah" through the game chat else if I'm not "self.op" rights, it'll just send it as "+Shaz: hi blahblah", as seen on the line where the prefix variable gets assigned.
Entire code file - http://pastebin.com/mDsJQA3X
Sorry if I've repeated myself anywhere, just trying to make the background purpose clearer.
Upvotes: 0
Views: 238
Reputation: 33903
It feels like maybe you need to have a User
object with properties like is_op
, is_voice
, prefix
instead of just passing around a username string. Since it appears you're working with an existing IRCBot
class it's going to be difficult to achieve what you really want without rewriting more of the class - the information you need has been discarded. You are already doing the only thing possible, applying either @
or +
as the prefix.
Nonetheless, the question you literally asked is:
"how to use multiple if statements to assign a variable"
Since you are already using elif
in your code, I understand this question to mean:
"how do you do a switch/case statement in Python"
This is answered here: Replacements for switch statement in Python?
Upvotes: 1