I’ve seen dozens of IRCBot frameworks over the years, and while there are definitely some that work well, I’ve always wanted to create my own. I thought it would be a great python learning experience, and I really had no idea just how much of one it would be! Here is a small snippit of my ModuleBase class, which has base functions that most of my modules use. The full source is here: http://git.io/KH8DIw if you have any notes/comments, feel free to make an issue on the git page or drop by the IRC channel.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import json
def open_user_database(database):
''' opens the database of aliased users'''
with open(database, 'rb') as f:
return json.load(f, encoding='utf-8')
def save_user_database(user_dict, database):
''' saves the database of aliased users'''
with open(database, 'wb') as f:
json.dump(user_dict, f, encoding='utf-8')
def check_alias(username, database):
''' checks if an alias exists, else passes input instead '''
try:
users = open_user_database(database)
except IOError:
return username
for key in users:
if username.lower() in users[key]:
return key
return username
def register_user_(source_, user, database):
''' registers aliases of IRC nicknames '''
user = unicode(user)
source = source_
database = database
try:
users = open_user_database(database)
except IOError:
users = {}
try:
user_list = users[user]
if source.lower() in user_list:
return '{0} is already aliased to {1}.'.format(user, source)
else:
users[user].append(source.lower())
save_user_database(users, database)
return 'Successfully aliased {0} to {1}.'.format(user, source)
except KeyError:
users[user] = [source.lower()]
if users != None:
save_user_database(users, database)
return 'Added {0} with alias {1}.'.format(user, source) |



0 Responses.