Skip to content

Commit

Permalink
Quick fix for message clearing error.
Browse files Browse the repository at this point in the history
Now looks a bit better
  • Loading branch information
Simon committed Sep 22, 2016
1 parent 2cf9596 commit 56e61e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
38 changes: 21 additions & 17 deletions Preferences/UserPreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@

logger = logging.getLogger(__name__)

class UserPreferences():

class UserPreferences:
def __init__(self, config=None):
self.__users = dict()
self.__config = config

def __checkUser(self,userid):
def __checkUser(self, chat_id):
if self.__config is None:
logger.error('Configuration file is needed')
logger.error('Failed due to configuration file not set.')
return None
if userid in self.__users:
if chat_id in self.__users:
return True
else:
self.__users[userid] = UserPreferencesModel(userid,self.__config)
self.__users[chat_id] = UserPreferencesModel(chat_id, self.__config)
return False

def add_config(self,config):
def add_config(self, config):
self.__config = config
logger.info('Configuration file set')

def get(self,userid):
result = self.__checkUser(userid)
def get(self, chat_id):
result = self.__checkUser(chat_id)
if result is None:
logger.info('Failed')
return None
logger.error('Failed due to configuration file not set.')
return None
if not result:
logger.info('Adding user %s',userid)
return self.__users[userid]
logger.info('Adding user %s.', chat_id)
return self.__users[chat_id]

def rem(self,userid):
if userid in self.__users:
del self.__users[userid]
logger.info('User %s has been removed' % userid)
def rem(self, chat_id):
if chat_id in self.__users:
del self.__users[chat_id]
logger.info('User %s has been removed.' % chat_id)
else:
logger.info('User %s does not exist' % userid)
logger.info('User %s does not exist.' % chat_id)

@property
def config(self):
return copy.deepcopy(self.__config)

def users(self):
return list(self.__users.keys())
19 changes: 10 additions & 9 deletions Preferences/UserPreferencesModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@


class UserPreferencesModel(object):
def __init__(self, chat_id,config):
self.chat_id = chat_id
self.loadedconfig = config
self.__set_directory(directory=self.__getDefaulteDir())
self.__set_filename(filename=self.__getDefaultFilename(chat_id))
# load existing or create file
self.__load_or_create(False)
logger.info('[%s] Created new preferences.' % self.chat_id)

def __defaultDict(self):
global config
Expand All @@ -35,7 +27,16 @@ def __defaultDict(self):
language = self.loadedconfig.get('DEFAULT_LANG', 'en'),
search_ids = []
)
return preferences
return preferences

def __init__(self, chat_id,config):
self.chat_id = chat_id
self.loadedconfig = config
self.__set_directory(directory=self.__getDefaulteDir())
self.__set_filename(filename=self.__getDefaultFilename(chat_id))
# load existing or create file
self.__load_or_create(False)
logger.info('[%s] Created new preferences.' % self.chat_id)

def __getDefaulteDir(self):
user_path = os.path.join(
Expand Down
7 changes: 7 additions & 0 deletions pogobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def cmd_clear(bot, update):
# Remove from jobs
job = jobs[chat_id]
job.schedule_removal()
del jobs[chat_id]

# Remove from sent
del sent[chat_id]
# Remove from locks
del locks[chat_id]

pref.reset_user()

bot.sendMessage(chat_id, text='Notifications successfully removed!')
Expand Down

0 comments on commit 56e61e6

Please sign in to comment.