def on_guild_member_add(self, event): created = humanize.naturaltime(datetime.utcnow() - to_datetime(event.user.id)) new = ( event.config.new_member_threshold and (time.time() - to_unix(event.user.id)) < event.config.new_member_threshold ) self.log_action(Actions.GUILD_MEMBER_ADD, event, new=' :new:' if new else '', created=created)
def joined_command(self, event, server, user=None): """= joined = Displays date and time user joined discord or guild in EST You can convert the result to your timezone with the !timezone command usage :: !joined <guild | discord> [user] aliases :: None category :: Utilities == Examples !joined discord `No user supplied, defaults to you' !joined discord linux !joined guild `No user supplied, defaults to you' !joined guild linux """ if user is None: user = event.guild.members[event.msg.author.id] else: for member in event.guild.members.values(): if user == member.name: user = member break else: event.msg.reply('No such user') return if server == 'guild': joined = user.joined_at joined = pendulum.datetime(joined.year, joined.month, joined.day, joined.hour, joined.minute, joined.second, tz='America/New_York') elif server == 'discord': joined = pendulum.from_timestamp(snowflake.to_unix(user.user.id), tz='America/New_York') else: return event.msg.reply( joined.format( '[{} joined {}] dddd MMMM Do YYYY [at] h:mmA zz'.format( user.name, server)))
def drama_command(self, event, args): """= drama = Keeps track of the last time there was drama on the server usage :: !drama [alert | record | archive] aliases :: nonsense category :: Fun == Examples !drama `Will display when the last occurrence of drama happened' !drama record `Will display the record of most days without drama' !drama archive `Will display all recorded incidents of drama' !drama alert `Will declare that drama has just happened (STAFF ONLY)' """ if event.channel.is_dm: event.msg.reply('This command only works in a server :/') return action = args.action server_id = event.guild.id # Use pendulum for timezone information today = pendulum.now(tz='America/New_York') # Convert to standard datetime so sqlite doesn't complain today = datetime(today.year, today.month, today.day, today.hour, today.minute, today.second) try: conn = sqlite3.connect('db/{}.db'.format(server_id), detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) c = conn.cursor() if action is None: c.execute('select last_drama from drama') last_drama = c.fetchone() if last_drama is None: event.msg.reply('There has never been any {}'.format( event.name)) else: last_drama = last_drama[0] days_ago = (today.date() - last_drama.date()).days event.msg.reply( 'Last time there was {} was {} {} ago on {:%x at %I:%M%p EST}' .format(event.name, days_ago, 'day' if days_ago == 1 else 'days', last_drama)) elif action == 'record': if c.execute('select count(*) from drama').fetchone()[0] == 0: event.msg.reply('There has never been any {}'.format( event.name)) else: c.execute('select * from drama') record, last_drama = c.fetchone() cur_record = (today.date() - last_drama.date()).days record = max(record, cur_record) event.msg.reply('{} {} {}'.format( record, 'day' if record == 1 else 'days', ':unamused:' if record <= 1 else '')) elif action == 'alert': user_level = self.bot.get_level(event.author) if user_level < CommandLevels.MOD: event.msg.reply("You're not qualified to declare drama") return if c.execute('select count(*) from drama').fetchone( )[0] == 0: # First run in a server if event.guild.id == 414960415018057738: # Retro retro_opening = pendulum.datetime( 2018, 6, 22, tz='America/New_York') # Retro opening day start_date = datetime(retro_opening.year, retro_opening.month, retro_opening.day) self.log.info( 'Detected drama in retro, using custom date instead of server creation' ) else: server_creation = pendulum.from_timestamp( snowflake.to_unix(event.guild.id), tz='America/New_York') start_date = datetime(server_creation.year, server_creation.month, server_creation.day) record = (today.date() - start_date.date()).days c.execute('insert into drama values (?,?)', (record, start_date)) conn.commit() c.execute('select * from drama') record, last_drama = c.fetchone() cur_record = (today.date() - last_drama.date()).days new_record = max(record, cur_record) c.execute('update drama set record = ?, last_drama = ?', (new_record, today)) c.execute('insert into drama_archive values (?)', (today, )) conn.commit() event.msg.reply( 'Uh oh, {} alert :popcorn:\nThe peace lasted for {} {}. {}\n\n{}' .format( event.name, cur_record, 'day' if cur_record == 1 else 'days', ':unamused:' if cur_record <= 1 else '', 'New record btw :clap:' if cur_record > record and record > 0 else '')) elif action == 'archive': if c.execute('select count(*) from drama_archive').fetchone( )[0] == 0: event.msg.reply('There has never been any {}'.format( event.name)) else: incidents = c.execute( 'select count(*) from drama_archive').fetchone()[0] r = '{} {} {}:\n'.format( incidents, 'Dramatic' if event.name == 'drama' else 'Nonsensical', 'event' if incidents == 1 else 'events') c.execute('select incident from drama_archive') for row in c: r += '{:%x - %I:%M%p EST}\n'.format(row[0]) event.msg.reply(r) except sqlite3.OperationalError: event.msg.reply( '[INFO] - First time running `!{}` command, creating new table in database' .format(event.name)) c.execute( 'create table drama (record integer, last_drama timestamp)') c.execute('create table drama_archive (incident timestamp)') conn.commit() event.msg.reply('Table created. Please run your command again.') finally: conn.close()