不一致的API包装。
aiocord的Python项目详细描述
安装
python3 -m pip install aiocord
简单用法
importasyncioimportaiohttpimportaiocord.engine# externaltoken='APPLICATION_TOKEN'# get the event looploop=asyncio.get_event_loop()# create a sessionsession=aiohttp.ClientSession(loop=loop)# cooperative clientclient=aiocord.engine.Client(session,token,loop=loop)@client.track('ready')asyncdefhandle_0(shard,version,initial_guild_ids):# caution! blockingprint(shard,'ready')@client.track('message create')asyncdefhandle_1(shard,guild,channel,message):signal='.say 'ifmessage.content.startswith(signal):response=message.content[len(signal):]awaitclient.create_message(channel.id,content=response)asyncdefinitialize():awaitclient.start()asyncdeffinalize():awaitclient.close()awaitsession.close()try:loop.run_until_complete(initialize())try:loop.run_forever()exceptKeyboardInterrupt:passfinally:loop.run_until_complete(finalize())
高级用法
importasyncioimportaiohttpimportaiocordimportfunctoolstoken='APPLICATION_TOKEN'# get the event looploop=asyncio.get_event_loop()# create a sessionsession=aiohttp.ClientSession(loop=loop)# used for fetching inforest=aiocord.rest.Client(session,loop=loop)# authorizerest.authorize(token)# this is our stream handlerdefhandle(shard,event,data):# caution! blockingprint(shard,event)ifevent=='READY':session_id=data['session_id']# needed for resumeshard.patch(session_id)returnshards=[]asyncdefinitialize():# sharding informationdata=awaitrest.get_gateway_bot()# shard count and gateway urlcount,url=data['shards'],data['url']# index is the shard idforindexinrange(count):# needed for identifyinfo=(index,count)# used for listening to eventsgateway=aiocord.gateway.Client(session,token,info,loop=loop)# docs advice to routinely update the response of the# get gateway bot request, hence why this method existsgateway.update(url)# pass the current gateway to our handlercallback=functools.partial(handle,gateway)# will be called with# every event dispatchgateway.track(callback)# connect, start event steam# and identify with info givenawaitgateway.start()shards.append(gateway)ifnotindex<count:breakawaitasyncio.sleep(5.5)asyncdeffinalize():forgatewayinshards:awaitgateway.close()awaitsession.close()try:loop.run_until_complete(initialize())try:loop.run_forever()exceptKeyboardInterrupt:passfinally:loop.run_until_complete(finalize())