Ok, I have an IRC bot and I have a file full of nicknames (which are the admins for the bot), for example if someone types "!op" in the channel how would I go about making the bot read the file to see if the user who typed "!op" is in the list of admins and if he is then proc开发者_如何学JAVAeed with the action.. I'm just confused on how to make it read the file for the authorized users.. Your help will be appreciate it. Thanks.
Can I use something like this..
def isadmin(nick, 'masters.txt'):
for admin in file:
if nick == admin.rstrip():
return True
return False
if data.find('!op') != -1:
nick = data.split('!')[ 0 ].replace(':','')
if nick == isadmin(nick, open('masters.txt')):
sck.send('MODE ' + chan + ' +o ' + nick + '\r\n')
else:
sck.send('PRIVMSG ' + chan + ' :' + ' youre not my master ' + '\r\n')
You should probably just run through the whole Python tutorial. There is a whole section about reading and writing files.
I assume your names are line seperated.
def isadmin(name, file):
for admin in file:
if name == admin.rstrip():
return True
return False
But they could also be comma seperated.
def isadmin(name, file):
for admins in file:
if name in admins.rstrip().split(","):
return True
return False
And use it like this:
isadmin("bob", open("admins.txt"))
admins = set(nick.strip() for nick in open('masters.txt'))
def isadmin(nick):
return nick.strip() in admins
File reading is expensive enough that you don't want to re-read the file too often; if it's short, read it once and keep it in memory.
精彩评论