本文整理汇总了Python中plugin_manager.PluginManager.load_all方法的典型用法代码示例。如果您正苦于以下问题:Python PluginManager.load_all方法的具体用法?Python PluginManager.load_all怎么用?Python PluginManager.load_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类plugin_manager.PluginManager
的用法示例。
在下文中一共展示了PluginManager.load_all方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Mee6
# 需要导入模块: from plugin_manager import PluginManager [as 别名]
# 或者: from plugin_manager.PluginManager import load_all [as 别名]
class Mee6(discord.Client):
"""A modified discord.Client class
This mod dispatched most events to the different plugins.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.redis_url = kwargs.get('redis_url')
self.mongo_url = kwargs.get('mongo_url')
self.dd_agent_url = kwargs.get('dd_agent_url')
self.db = Db(self.redis_url, self.mongo_url, self.loop)
self.plugin_manager = PluginManager(self)
self.plugin_manager.load_all()
self.last_messages = []
self.stats = DDAgent(self.dd_agent_url)
async def on_ready(self):
"""Called when the bot is ready.
Connects to the database
Dispatched all the ready events
"""
log.info('Connected to the database')
if hasattr(self, 'shard_id'):
msg = 'Chat Shard {}/{} restarted'.format(self.shard_id, self.shard_count)
else:
msg = 'Mee6 Chat restarted'
self.stats.event(msg, 'Server count: {}'.format(len(self.servers)))
await self.add_all_servers()
for plugin in self.plugins:
self.loop.create_task(plugin.on_ready())
async def add_all_servers(self):
"""Syncing all the servers to the DB"""
log.debug('Syncing servers and db')
for server in self.servers:
self.stats.set('mee6.servers', server.id)
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.sadd('servers', server.id)
if server.name:
await self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
await self.db.redis.set('server:{}:icon'.format(server.id), server.icon)
async def on_server_join(self, server):
"""Called when joining a new server"""
self.stats.set('mee6.servers', server.id)
self.stats.incr('mee6.server_join')
log.info('Joined {} server : {} !'.format(server.owner.name, server.name))
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.sadd('servers', server.id)
await self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
await self.db.redis.set('server:{}:icon'.format(server.id), server.icon)
# Dispatching to global plugins
for plugin in self.plugins:
if plugin.is_global:
self.loop.create_task(plugin.on_server_join(server))
async def on_server_remove(self, server):
"""Called when leaving or kicked from a server
Removes the server from the db.
"""
log.info('Leaving {} server : {} !'.format(server.owner.name, server.name))
log.debug('Removing server {}\'s id from the db'.format(server.id))
await self.db.redis.srem('servers', server.id)
async def get_plugins(self, server):
plugins = await self.plugin_manager.get_all(server)
return plugins
async def send_message(self, *args, **kwargs):
self.stats.incr('mee6.sent_messages')
return await super().send_message(*args, **kwargs)
async def on_message(self, message):
self.stats.incr('mee6.recv_messages')
if message.channel.is_private:
return
server = message.server
if message.content == "!shard?":
if hasattr(self, 'shard_id'):
await self.send_message(message.channel, "shard {}/{}".format(self.shard_id+1,
self.shard_count))
enabled_plugins = await self.get_plugins(server)
for plugin in enabled_plugins:
self.loop.create_task(plugin.on_message(message))
#.........这里部分代码省略.........
示例2: Mee6
# 需要导入模块: from plugin_manager import PluginManager [as 别名]
# 或者: from plugin_manager.PluginManager import load_all [as 别名]
class Mee6(discord.Client):
"""A modified discord.Client class
This mod dispatches most events to the different plugins.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.redis_url = kwargs.get('redis_url')
self.mongo_url = kwargs.get('mongo_url')
self.dd_agent_url = kwargs.get('dd_agent_url')
self.sentry_dsn = kwargs.get('sentry_dsn')
self.db = Db(self.redis_url, self.mongo_url, self.loop)
self.plugin_manager = PluginManager(self)
self.plugin_manager.load_all()
self.last_messages = []
self.stats = DDAgent(self.dd_agent_url)
def run(self, *args):
self.loop.run_until_complete(self.start(*args))
async def ping(self):
PING_INTERVAL = 3
key = 'mee6.gateway.{}-{}'.format(self.shard_id,
self.shard_count)
while True:
if self.is_logged_in:
self.stats.check(key, 'OK')
await self.db.redis.setex(key, PING_INTERVAL + 2, '1')
else:
self.stats.check(key, 'Critical')
await asyncio.sleep(PING_INTERVAL)
async def on_ready(self):
"""Called when the bot is ready.
Connects to the database
Dispatched all the ready events
"""
log.info('Connected to the database')
await self.add_all_servers()
for plugin in self.plugins:
self.loop.create_task(plugin.on_ready())
self.loop.create_task(self.ping())
async def add_all_servers(self):
"""Syncing all the servers to the DB"""
log.debug('Syncing servers and db')
for server in self.servers:
self.stats.set('mee6.servers', server.id)
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.sadd('servers', server.id)
if server.name:
await self.db.redis.set(
'server:{}:name'.format(server.id),
server.name
)
if server.icon:
await self.db.redis.set(
'server:{}:icon'.format(server.id),
server.icon
)
async def on_server_join(self, server):
"""Called when joining a new server"""
self.stats.set('mee6.servers', server.id)
self.stats.incr('mee6.server_join')
await self.db.redis.sadd('servers', server.id)
log.info('Joined {} server : {} !'.format(
server.owner.name,
server.name
))
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
await self.db.redis.set(
'server:{}:icon'.format(server.id),
server.icon
)
# Dispatching to global plugins
for plugin in self.plugins:
if plugin.is_global:
self.loop.create_task(plugin.on_server_join(server))
async def on_server_remove(self, server):
"""Called when leaving or kicked from a server
Removes the server from the db.
"""
log.info('Leaving {} server : {} !'.format(
server.owner.name,
server.name
#.........这里部分代码省略.........
示例3: Mee6
# 需要导入模块: from plugin_manager import PluginManager [as 别名]
# 或者: from plugin_manager.PluginManager import load_all [as 别名]
class Mee6(discord.Client):
"""A modified discord.Client class
This mod dispatched most events to the different plugins.
"""
def __init__(self, *args, **kwargs):
discord.Client.__init__(self, *args, **kwargs)
self.redis_url = kwargs.get('redis_url')
self.db = Db(self.redis_url, self.loop)
self.plugin_manager = PluginManager(self)
self.plugin_manager.load_all()
self.last_messages = []
async def on_ready(self):
"""Called when the bot is ready.
Launched heartbeat, update_stats cron jobs
Connects to the database
Dispatched all the ready events
"""
log.info('Connected to the database')
await self.add_all_servers()
discord.utils.create_task(self.heartbeat(5), loop=self.loop)
for plugin in self.plugins:
self.loop.create_task(plugin.on_ready())
async def add_all_servers(self):
"""Syncing all the servers to the DB"""
log.debug('Syncing servers and db')
await self.db.redis.delete('servers')
for server in self.servers:
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.sadd('servers', server.id)
if server.name:
await self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
await self.db.redis.set('server:{}:icon'.format(server.id), server.icon)
async def on_server_join(self, server):
"""Called when joining a new server
Adds the server to the db.
Also adds its name and it's icon if it has one.
"""
log.info('Joined {} server : {} !'.format(server.owner.name, server.name))
log.debug('Adding server {}\'s id to db'.format(server.id))
await self.db.redis.sadd('servers', server.id)
await self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
await self.db.redis.set('server:{}:icon'.format(server.id), server.icon)
# Dispatching to global plugins
for plugin in self.plugins:
if plugin.is_global:
self.loop.create_task(plugin.on_server_join(server))
async def on_server_remove(self, server):
"""Called when leaving or kicked from a server
Removes the server from the db.
"""
log.info('Leaving {} server : {} !'.format(server.owner.name, server.name))
log.debug('Removing server {}\'s id from the db'.format(server.id))
await self.db.redis.srem('servers', server.id)
async def heartbeat(self, interval):
"""Sends a heartbeat to the db every interval seconds"""
while self.is_logged_in:
await self.db.redis.set('heartbeat', 1, expire=interval)
await asyncio.sleep(0.9 * interval)
async def get_plugins(self, server):
plugins = await self.plugin_manager.get_all(server)
return plugins
async def send_message(self, *args, **kwargs):
counter = 0
msg = None
while counter!=3:
try:
msg = await super().send_message(*args, **kwargs)
counter=2
except discord.errors.HTTPException as e:
if e.response.status==502:
log.info('502 HTTP Exception, retrying...')
else:
log.info('{} HTTP Exception.'.format(
e.response.status
))
counter=2
counter+=1
return msg
async def on_message(self, message):
if message.channel.is_private:
#.........这里部分代码省略.........
示例4: Mee6
# 需要导入模块: from plugin_manager import PluginManager [as 别名]
# 或者: from plugin_manager.PluginManager import load_all [as 别名]
class Mee6(discord.Client):
def __init__(self, *args, **kwargs):
discord.Client.__init__(self, *args, **kwargs)
self.redis_url = kwargs.get('redis_url')
self.db = Db(self.redis_url)
self.plugin_manager = PluginManager(self)
self.plugin_manager.load_all()
self.last_messages = []
async def on_ready(self):
with open('welcome_ascii.txt') as f:
print(f.read())
self.add_all_servers()
discord.utils.create_task(self.heartbeat(5), loop=self.loop)
discord.utils.create_task(self.update_stats(60), loop=self.loop)
await self.change_status(game=discord.Game(name='http://mee6.xyz'))
def add_all_servers(self):
log.debug('Syncing servers and db')
self.db.redis.delete('servers')
for server in self.servers:
log.debug('Adding server {}\'s id to db'.format(server.id))
self.db.redis.sadd('servers', server.id)
@asyncio.coroutine
def send_message(self, *args, **kwargs):
dest = args[0]
if hasattr(args[0], 'server'):
dest = args[0].server
if isinstance(args[0], discord.Member):
dest = args[0]
log.info('[email protected]{} >> {}'.format(dest.name,args[1].replace('\n', '~')))
yield from super().send_message(*args, **kwargs)
@asyncio.coroutine
def on_server_join(self, server):
log.info('Joined {} server : {} !'.format(server.owner.name, server.name))
log.debug('Adding server {}\'s id to db'.format(server.id))
self.db.redis.sadd('servers', server.id)
self.db.redis.set('server:{}:name'.format(server.id), server.name)
if server.icon:
self.db.redis.set('server:{}:icon'.format(server.id), server.icon)
@asyncio.coroutine
def on_server_remove(self, server):
log.info('Leaving {} server : {} !'.format(server.owner.name, server.name))
log.debug('Removing server {}\'s id from the db'.format(server.id))
self.db.redis.srem('servers', server.id)
@asyncio.coroutine
def heartbeat(self, interval):
while self.is_logged_in:
self.db.redis.set('heartbeat', 1, ex=interval)
yield from asyncio.sleep(0.9 * interval)
@asyncio.coroutine
def update_stats(self, interval):
while self.is_logged_in:
# Total members and online members
members = list(self.get_all_members())
online_members = filter(lambda m: m.status is discord.Status.online, members)
online_members = list(online_members)
self.db.redis.set('mee6:stats:online_members', len(online_members))
self.db.redis.set('mee6:stats:members', len(members))
# Last messages
for index, timestamp in enumerate(self.last_messages):
if timestamp + interval < time():
self.last_messages.pop(index)
self.db.redis.set('mee6:stats:last_messages', len(self.last_messages))
yield from asyncio.sleep(interval)
@asyncio.coroutine
def _run_plugin_event(self, plugin, event, *args, **kwargs):
# A modified coro that is based on Client._run_event
try:
yield from getattr(plugin, event)(*args, **kwargs)
except asyncio.CancelledError:
pass
except Exception:
try:
yield from self.on_error(event, *args, **kwargs)
except asyncio.CancelledError:
pass
async def on_message(self, message):
mee6_server_id = "159962941502783488"
update_channel_id = "160784396679380992"
if not hasattr(message, 'server'):
return
if (message.server.id, message.channel.id) == (mee6_server_id, update_channel_id):
owners = set(server.owner for server in self.servers)
for owner in owners:
await self.send_message(
owner,
message.content
)
def dispatch(self, event, *args, **kwargs):
#.........这里部分代码省略.........
示例5: Mee6
# 需要导入模块: from plugin_manager import PluginManager [as 别名]
# 或者: from plugin_manager.PluginManager import load_all [as 别名]
class Mee6(discord.Client):
"""A modified discord.Client class
This mod dispatches most events to the different plugins.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.redis_url = kwargs.get('redis_url')
self.mongo_url = kwargs.get('mongo_url')
self.dd_agent_url = kwargs.get('dd_agent_url')
self.sentry_dsn = kwargs.get('sentry_dsn')
self.db = Db(self.redis_url, self.mongo_url, self.loop)
self.plugin_manager = PluginManager(self)
self.plugin_manager.load_all()
self.last_messages = []
self.stats = DDAgent(self.dd_agent_url)
self.voice_sessions_ids = dict()
if self.shard_id is not None:
self.shard = [self.shard_id, self.shard_count]
else:
self.shard = [0, 1]
def run(self, *args):
console_coro = self.loop.create_server(make_console(self),
'127.0.0.1', 8000)
self.loop.run_until_complete(console_coro)
self.loop.create_task(self.connect_schwifty())
self.loop.run_until_complete(self.start(*args))
async def connect_schwifty(self):
self.schwifty = await SchwiftyWebsocket.create(
self.shard,
self
)
while not self.is_closed:
try:
await self.schwifty.poll_event()
except (ConnectionClosed, asyncio.TimeoutError) as e:
await asyncio.sleep(1)
self.schwifty = await SchwiftyWebsocket.create(
self.shard,
self
)
async def console(reader, writer):
loop = asyncio.get_event_loop()
data = await reader.read(100)
message = data.decode()
result = eval(message)
writer.write(result)
await writer.drain()
writer.close()
def send_monitoring_message(self, msg):
self.loop.create_task(self._send_monitoring_message(msg))
async def _send_monitoring_message(self, msg):
url = os.getenv('MONITORING_WH')
if not url:
return
headers = {}
headers['Content-Type'] = 'application/json'
data = json.dumps({'content': msg})
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=data) as d:
pass
async def on_socket_raw_receive(self, payload):
tags = {'op': str(payload['op']),
't': payload.get('t') or "NONE"}
self.stats.incr('discord.event', tags=tags)
if not payload['op'] == 0:
return
if payload['t'] == 'VOICE_STATE_UPDATE':
d = payload['d']
if str(d['user_id']) != self.user.id:
return
guild_id = d['guild_id']
self.voice_sessions_ids[guild_id] = d['session_id']
if payload['t'] == 'VOICE_SERVER_UPDATE':
d = payload['d']
guild_id = d['guild_id']
d['session_id'] = self.voice_sessions_ids.get(guild_id)
if d.get('endpoint'):
await self.schwifty.voice_update(d)
if payload['t'] == 'READY':
if not hasattr(self, 'shard_id'):
return
msg = "**[READY]** shard {}/{}".format(
self.shard_id,
#.........这里部分代码省略.........