def message_log(msg, server_id): '''Log a message into the database.''' database.init() table_log = Table('user_messages_{}'.format(server_id), TableTypes.pGlobal) Table.insert(table_log, dict(userid=msg.author.id, username=msg.author.name, message=msg.content, serverid=msg.server.id, servername=msg.server.name))
def json_store(string, plugin, filename): '''Store a json in the database.''' database.init() table_strcache = Table('strcache', TableTypes.pGlobal) filename = '{}_{}'.format(plugin, filename) try: entry_strcache = Table.search(table_strcache, 'filename', filename) except: # Table must be empty. entry_strcache = None if entry_strcache: entry_strcache.edit(dict(filename=filename, text=string)) else: Table.insert(table_strcache, dict(filename=filename, text=string))
def owners_add(id_user): '''Add an owner to the database''' database.init() table_owners = Table('owners', TableTypes.pGlobal) try: entry_owner = Table.search(table_owners, 'userid', '{}'.format(id_user)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_owner = None if not entry_owner: Table.insert(table_owners, dict(userid=id_user))
def owners_check(id_user): '''Check if a user is an owner''' database.init() table_owners = Table('owners', TableTypes.pGlobal) try: entry_owner = Table.search(table_owners, 'userid', '{}'.format(id_user)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_owner = None if entry_owner: return True else: return False
def message_count_get(id_server): '''Get the current message count.''' database.init() table_message_count = Table('messagecounts', TableTypes.pGlobal) try: entry_message_count = Table.search(table_message_count, 'serverid', '{}'.format(id_server)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_message_count = [0, 0] if entry_message_count: return int(entry_message_count.data[1]) else: return 0
def prefix_get(id_server): '''Get a server\'s prefix.''' database.init() table_prefix = Table('prefixes', TableTypes.pGlobal) try: entry_prefix = Table.search(table_prefix, 'serverid', '{}'.format(id_server)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_prefix = None if entry_prefix: return entry_prefix.data[1] else: return '!'
def message_count_set(id_server, count): '''Set the current message count.''' database.init() table_message_count = Table('messagecounts', TableTypes.pGlobal) try: entry_message_count = Table.search(table_message_count, 'serverid', '{}'.format(id_server)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_message_count = None if entry_message_count: entry_message_count.edit(dict(serverid=id_server, count=count)) else: Table.insert(table_message_count, dict(serverid=id_server, count=count))
def prefix_set(id_server, prefix): '''Set a server\'s prefix.''' database.init() table_prefix = Table('prefixes', TableTypes.pGlobal) try: entry_prefix = Table.search(table_prefix, 'serverid', '{}'.format(id_server)) except: # TODO: Narrow this and other Exception clauses. # Table must be empty. entry_prefix = None if entry_prefix: entry_prefix.edit(dict(serverid=id_server, prefix=prefix)) else: # Create new entry Table.insert(table_prefix, dict(serverid=id_server, prefix=prefix))
def json_get(url, caller='', name_custom='', save=True): '''Retrieve and cache a JSON.''' if caller == '': caller_get() if name_custom == '': name_custom = url.split('/')[-1] filename = '{}_{}'.format(caller, name_custom) # Get cached String database.init() table_strcache = Table('strcache', TableTypes.pGlobal) try: entry_strcache = Table.search(table_strcache, 'filename', filename) json_string = entry_strcache.data[2] except: json_string = urllib.request.urlopen( urllib.request.Request(url)).read().decode("utf-8") if save: json_store(json_string, caller, name_custom) return json_string
async def onCommand(message_in): # Initialize Database database.init() OffsetTable = Table('offsets', TableTypes.pGlobal) # Get user. if message_in.server: me = message_in.server.me else: me = message_in.channel.me if message_in.command == 'setoffset': # Normalize offset offsetstr = message_in.body.strip() if offsetstr == "": return message.Message( 'Incorrect Offset format. Has to be in +/-HH:MM!') if offsetstr[0] == '+': prefix = '+' else: prefix = '' try: hours, minutes = map(int, offsetstr.split(':')) except Exception: try: hours = int(offsetstr) minutes = 0 except Exception: return message.Message( 'Incorrect Offset format. Has to be in +/-HH:MM!') normalizedoffset = '{}{}:{}'.format(prefix, hours, minutes) # Set Offset in Database # Try to update Offset if it exists existingOffset = Table.search(OffsetTable, 'id', '{}'.format(message_in.author.id)) if existingOffset != None: existingOffset.edit( dict(id=message_in.author.id, offset=normalizedoffset)) else: # Create new entry Table.insert( OffsetTable, dict(id=message_in.author.id, offset=normalizedoffset)) # Get time right now # Return time along with offset timeutc = datetime.datetime.utcnow() # Apply offset if hours > 0: # Apply positive offset timedelta = datetime.timedelta(hours=hours, minutes=minutes) newTime = timeutc + timedelta elif hours < 0: # Apply negative offset timedelta = datetime.timedelta(hours=(-1 * hours), minutes=(-1 * minutes)) newTime = timeutc - timedelta else: # No offset newTime = timeutc return message.Message( 'Your UTC offset has been set to *{}*, for which the time is {}.'. format(normalizedoffset, newTime.strftime("%I:%M %p"))) if message_in.command == 'time': memberOrOffset = message_in.body.strip() # Check whose time we need (or if we got an offset) if not memberOrOffset: member = message_in.author else: # Try to get a user first member = displayname.memberForName(memberOrOffset, message_in.server, me) if member: existingOffset = Table.search(OffsetTable, 'id', '{}'.format(member.id)) # Check if entry exists try: offset = existingOffset.data[1] except Exception: return message.Message( '*{}* didn\'t set an offset. Set an offset with `!setoffset (offset)`.' .format(displayname.name(member))) else: # Assume input is offset offset = memberOrOffset offset = offset.replace('+', '') # Split time string by : and get hour/minute values try: hours, minutes = map(int, offset.split(':')) except Exception: try: hours = int(offset) minutes = 0 except Exception: return message.Message( 'Invalid offset format. Has to be in +/-HH:MM!') # Get time right now timeutc = datetime.datetime.utcnow() # Apply offset if hours > 0: # Apply positive offset offsetmsg = 'UTC+{}'.format(offset) timedelta = datetime.timedelta(hours=hours, minutes=minutes) newTime = timeutc + timedelta elif hours < 0: # Apply negative offset offsetmsg = 'UTC{}'.format(offset) timedelta = datetime.timedelta(hours=(-1 * hours), minutes=(-1 * minutes)) newTime = timeutc - timedelta else: # No offset newTime = t if member: msg = '{}; where *{}* is, it\'s currently *{}*'.format( offsetmsg, displayname.name(member), newTime.strftime("%I:%M %p")) else: msg = '{} is currently *{}*'.format(offsetmsg, newTime.strftime("%I:%M %p")) # Say message return message.Message(msg)
async def onCommand(message_in): # Initialize database. database.init() table_greetings = Table("greetings", TableTypes.pServer)
def message_count_get(server_id): '''Get the current message count.''' database.init() table_message_count = Table('user_messages_{}'.format(server_id), TableTypes.pGlobal) return table_message_count.getLatestID()