Skip to main content

WatonPlugin 的 Python API

项目描述

沃顿API

WatonAPI 徽标

WatonPlugin 的 Python API

WatonAPI 是一个 API,用于通过 WatonPlugin 连接到 spigot 服务器以进行通信。您可以向服务器发送消息并阅读消息,使其对跨聊天程序很有用。

一些可能的用例示例如下:

  • 我的世界聊天 CLI
  • 交叉聊天不和谐机器人

还有很多

如何使用

文档将很快发布

WatonAPI 是一个异步库,这意味着它必须使用 asyncio 运行。您可以在示例目录中查找使用 WatonAPI 的示例程序。

CLI 示例

from watonapi.server import *
import asyncio

server_ip = ""
token = ""

async def async_input():
    return await asyncio.get_event_loop().run_in_executor(None, input)

async def take_input(server):
    while True:
        msg = await async_input()
        await server.send_msg('Waton CLI', msg) # When input is received, send it to the server

async def main(server):
    await server.add_listener("minecraft_msg") # Begin listening for a "minecraft_msg" packet
    await server.add_listener("player_join") # Begin listening for a "player_join" packet
    await server.add_listener("player_leave") # Begin listening for a "player_leave" packet
    
    authorized = False
    try:
        # If this doesn't fail, the token was correct and the connection authorized
        authorized = await server.connect(token)
    except Exception as e:
        print(f"Couldn't connect to server ( ERROR: {e} )")
    
    if authorized:
        input_handler = asyncio.create_task(take_input(server)) # Start an async task to get user input
        async for packet in server.get_listeners(): # Get all of the packets that we are listening for
            if packet["type"] == "minecraft_msg":
                print(f"<{packet['user']}> {packet['content']}")
            elif packet["type"] == "player_join":
                print(f"{packet['user']} joined the game!")
            elif packet["type"] == "player_leave":
                print(f"{packet['user']} left the game!")
        input_handler.cancel() # If it gets to here, the connection was lost
        print("Lost Connection")

server = Server(server_ip) # Create a server object using the server ip
asyncio.run(main(server)) # Run the main function

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

WatonAPI-0.1.tar.gz (16.9 kB 查看哈希

已上传 source

内置分布

WatonAPI-0.1-py3-none-any.whl (18.8 kB 查看哈希

已上传 py3