def on_broadcast(self, context, data): user = context['user'] if user and context['access_level'] >= ACL.moderator: message = { 'id': str(uuid.uuid4()), 'channel': 'broadcast', 'content': data['content'], 'author': self.format_user(context), 'created_at': time.time(), } self.publish_event('broadcast', message, 'channel') # add message to history util.zhaddex(self.redis, 'webchat-message-history', message['id'], config.message_history_ttl, json.dumps(message, separators=(',', ':')), message['created_at'], config.message_history_length) self.publish_event( 'webchat_broadcast', { 'username': context['username'], 'avatar_url': context['avatar_url'], 'content': data['content'], }, 'bot') self.ack() else: self.error('Insufficient privileges.') return context
async def on_message(self, message): if hasattr(message, 'author') and message.author == self.user: return if message.channel.type == discord.ChannelType.private: await self.on_private_message(message) return if self.is_relevant_message(message): if message.content.startswith('%!'): await self.dispatch_command(message.author, message.content) await message.delete(delay=0) return timeout = util.shget(self.redis, 'timed-out-members', str(message.author.id)) if timeout: ttl = util.shttl(self.redis, 'timed-out-members', str(message.author.id)) await message.author.send( 'You are still timed out. ' f'Please wait another {ttl} seconds.\n') await message.delete(delay=0) return formatted = format_message(message) await self.publish_event('message', formatted) # add message to history util.zhaddex(self.redis, 'webchat-message-history', formatted['id'], config.message_history_ttl, json.dumps(formatted, separators=(',', ':')), formatted['created_at'], config.message_history_length)
async def command_broadcast(self, member, argstr): rank = format_role(member.top_role) if rank not in ['mod', 'crew']: await member.send('You do not have permission to send broadcasts.') return if self.live_channel_id: channel = self.get_channel(self.live_channel_id) embed = discord.Embed(description=argstr, colour=0xe74c3c) embed.set_author(name=member.display_name, icon_url=member.avatar_url) await channel.send('Webchat Broadcast:', embed=embed) formatted = { 'id': str(uuid.uuid4()), 'channel': 'broadcast', 'content': argstr, 'author': format_user(member), 'created_at': time.time(), } await self.publish_event('broadcast', formatted) util.zhaddex(self.redis, 'webchat-message-history', formatted['id'], config.message_history_ttl, json.dumps(formatted, separators=(',', ':')), formatted['created_at'], config.message_history_length)
def on_message(self, context, data): user = context['user'] username = context['username'] avatar_url = context['avatar_url'] # check if user has been banned if user.is_banned(): raise ClientError('You have been banned.') # check if user has been timed out timeout = util.shget(self.redis, 'timed-out-members', self.webchat_user_key(context)) if timeout: timeout = json.loads(timeout.decode('utf-8')) ttl = util.shttl(self.redis, 'timed-out-members', self.webchat_user_key(context)) detail = f'You have to wait another {ttl} seconds.' if timeout.get('reason'): detail += f'\nReason: {timeout["reason"]}' self.error('You have been timed out.', detail) return context # push to channel util.print_info(f'{username} says: {data["content"]}') message = { 'id': str(uuid.uuid4()), 'content': data['content'], 'channel_name': data['channel_name'], 'author': self.format_user(context), 'created_at': time.time(), } self.publish_event('message', message, 'channel') # add message to history util.zhaddex(self.redis, 'webchat-message-history', message['id'], config.message_history_ttl, json.dumps(message, separators=(',', ':')), message['created_at'], config.message_history_length) # push to bot for discord users self.publish_event( 'webchat_message', { 'channel_name': data['channel_name'], 'username': username, 'avatar_url': avatar_url, 'content': data['content'], }, 'bot') self.ack() return context