def dash_index(): if g.user: if g.user.admin: stats = json.loads(rdb.get('web:dashboard:stats') or '{}') if not stats or 'refresh' in request.args: stats['messages'] = pretty_number(Message.select().count()) stats['guilds'] = pretty_number(Guild.select().count()) stats['users'] = pretty_number(User.select().count()) stats['channels'] = pretty_number(Channel.select().count()) rdb.setex('web:dashboard:stats', json.dumps(stats), 300) guilds = Guild.select().order_by(Guild.guild_id) else: stats = {} guilds = Guild.select( Guild, Guild.config['web'][str(g.user.user_id)].alias('role') ).where( (Guild.enabled == 1) & (~(Guild.config['web'][str(g.user.user_id)] >> None)) ) return render_template( 'dashboard.html', stats=stats, guilds=guilds, ) return render_template('login.html')
def guild_z_config_update(guild): if guild.role not in ['admin', 'editor']: return 'Missing Permissions', 403 # Calculate users diff try: data = yaml.safe_load(request.json['config']) except: return 'Invalid YAML', 400 before = sorted(list(guild.config.get('web', {}).items()), key=lambda i: i[0]) after = sorted([(str(k), v) for k, v in list(data.get('web', {}).items())], key=lambda i: i[0]) if guild.role != 'admin' and before != after: return 'Invalid Access', 403 role = data.get('web', {}).get(g.user.user_id) or data.get('web', {}).get( str(g.user.user_id)) if guild.role != role and not g.user.admin: return 'Cannot change your own permissions', 400 if data.get('modlog', {}): for modlog in data.get('modlog', {}).get('channels'): try: _mod = Channel.get(channel_id=modlog, guild_id=guild.guild_id) except Channel.DoesNotExist: return 'No modlog exists with id {}'.format(modlog), 400 if data.get('starboard', {}): for starboard in data.get('starboard', {}).get('channels'): try: _starboard = Channel.get(channel_id=starboard, guild_id=guild.guild_id) except Channel.DoesNotExist: return u'No channel exists with id {}'.format(starboard), 400 try: guild.update_config(g.user.user_id, request.json['config']) return '', 200 except Guild.DoesNotExist: return 'Invalid Guild', 404 except Exception as e: return 'Invalid Data: %s' % e, 400
def stats(): stats = json.loads(rdb.get('web:dashboard:stats') or '{}') if not stats or 'refresh' in request.args: # stats['messages'] = pretty_number(Message.select().count()) # stats['guilds'] = pretty_number(Guild.select().count()) # stats['users'] = pretty_number(User.select().count()) # stats['channels'] = pretty_number(Channel.select().count()) stats['messages'] = Message.select().count() stats['guilds'] = Guild.select().count() stats['users'] = User.select().count() stats['channels'] = Channel.select().count() rdb.setex('web:dashboard:stats', json.dumps(stats), 300) return jsonify(stats)
def encode_message_json(msg): try: channel_name = Channel.select(Channel.name).where( Channel.channel_id == msg.channel_id).tuples()[0][0] except IndexError: channel_name = None return dict(id=str(msg.id), timestamp=str(msg.timestamp), author_id=str(msg.author.id), channel=channel_name, channel_id=str(msg.channel_id), username=msg.author.username, discriminator=str(msg.author.discriminator).zfill(4), content=msg.content, deleted=msg.deleted, attachments=msg.attachments)
def on_channel_delete(self, event): Channel.update(deleted=True).where(Channel.channel_id == event.channel.id).execute()
def on_channel_update(self, event): Channel.from_disco_channel(event.channel)
def on_guild_delete(self, event): if event.deleted: Channel.update(deleted=True).where( Channel.guild_id == event.id ).execute()
def on_guild_create(self, event): for channel in list(event.channels.values()): Channel.from_disco_channel(channel) for emoji in list(event.emojis.values()): GuildEmoji.from_disco_guild_emoji(emoji, guild_id=event.guild.id)