Skip to content

Commit

Permalink
privilege system updates
Browse files Browse the repository at this point in the history
  • Loading branch information
double-fault committed Sep 17, 2017
1 parent dcb288f commit 18dea30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Source/Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ChatRoom import *
from Utilities import *
import Utilities
import os

class Bot:
def __init__(self, bot_name, client, commands, room_ids, background_tasks=[], bot_link="https://example.com", github_link="https://github.com"):
Expand All @@ -28,7 +29,8 @@ def __init__(self, bot_name, client, commands, room_ids, background_tasks=[], bo
self.bot_link = bot_link
self.github_link = github_link
self.background_task_manager = BackgroundTaskManager(background_tasks)
self.chatcommunicate = Chatcommunicate(bot_name, CommandManager(commands, self.rooms))
self.chatcommunicate = Chatcommunicate(bot_name, CommandManager(commands, self.rooms))
self.save_directory = os.path.expanduser("~") + "." + self.name.lower()

def add_background_task(self, background_task, interval=30, restart=True):
self.background_task_manager.add_background_task(background_task)
Expand All @@ -44,7 +46,7 @@ def add_essential_background_tasks(self, restart=True):
def join_rooms(self, watch_callback):
self.rooms[:] = []
for each_id in self.room_ids:
self.rooms.append(ChatRoom(self.client, each_id, watch_callback))
self.rooms.append(ChatRoom(self.client, self.save_directory, each_id, watch_callback))

for each_room in self.rooms:
each_room.join_room()
Expand All @@ -59,6 +61,10 @@ def watch_rooms(self):
for each_room in self.rooms:
each_room.watch_room()

def add_privilege_type(self, privilege_level, privilege_name):
for each_room in self.rooms:
each_room.add_privilege_type(privilege_level, privilege_name)

def start_bot(self):
self.join_rooms(self.chatcommunicate.handle_message)
self.watch_rooms()
Expand Down
33 changes: 32 additions & 1 deletion Source/ChatRoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import chatexchange as ce
from PrivilegeType import *
from PrivilegedChatUser import *
import os
import pickle

class ChatRoom:
def __init__(self, client, room_id, function_callback):
def __init__(self, client, save_directory, room_id, function_callback):
self.room_id = room_id
self.client = client
self.save_directory = save_directory
self.function_callback = function_callback
self.privilege_types = list()
self.privileged_users = list()
Expand Down Expand Up @@ -67,3 +70,31 @@ def get_privilege_type_by_name(self, name):
return each_type

return None

def save_privileged_users(self):
filename = self.save_directory + "room_" + str(self.room_id) + "_name_" + self.room.name + "_privileged_users"

try:
with open(filename, "wb") as file_to_write:
pickle.dump(self.privileged_users, file_to_write)
except IOError as ioerr:
print("IOError occurred: ")
print(str(ioerr))
except pickle.PickleError as perr:
print("Pickling error occurred: ")
print(str(perr))

def load_privileged_users(self):
filename = self.save_directory + "room_" + str(self.room_id) + "_name_" + self.room.name + "_privileged_users"

try:
with open(filename, "rb") as file_to_read:
self.privileged_users[:] = []
self.privileged_users = pickle.load(file_to_read)
except IOError as ioerr:
print("IOError occurred: ")
print(str(ioerr))
except pickle.PickleError as perr:
print("Pickling error occurred: ")
print(str(perr))

4 changes: 4 additions & 0 deletions Source/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
bot.add_essential_background_tasks()

bot.start_bot()

bot.add_privilege_type(1, "owner")

bot.rooms[0].add_privileged_user(4688119, bot.rooms[0].get_privilege_type_by_name("owner"))

0 comments on commit 18dea30

Please sign in to comment.