added some examples

This commit is contained in:
əlemi 2022-02-20 15:14:18 +01:00
parent 46ef53496c
commit dcf5ad1f08
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
2 changed files with 51 additions and 0 deletions

29
examples/authenticator.py Normal file
View file

@ -0,0 +1,29 @@
"""Microsoft made everyhing worse and now logging in is such a hassle..."""
import asyncio
from aiocraft.mc.auth import MicrosoftAuthenticator
print("To login into your Microsoft account, aiocraft needs to request an authentication code from Microsoft via OAuth.")
print("You will need to register an Azure application (or get access to an existing one).")
print("Please visit https://portal.azure.com/, select 'Azure Active Directory', then 'App registrations' and register a new application.")
print("You will need to register a redirect_uri, put anything there (even https://localhost)")
print()
client_id = input("your Azure app client_id > ")
client_secret = input("your Azure app client_secret > ")
redirect_uri = input("your Azure app redirect_uri > ") or "https://localhost"
auth = MicrosoftAuthenticator(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
print("Please visit")
print('\t' + auth.url())
print("Login into your microsoft account and authorize your application.")
print("Once you have logged in, you will be redirected to a (possibly blank) page.")
print("Check the page url for 'code=...'. Copy that code: it will allow to login into your MS account.")
print()
print("If you just wanted the login code, press enter to exit.")
print("If you instead want a Minecraft JWT token, to login into a server, paste your login code and press enter.")
code = input(" > ")
if code:
asyncio.run(auth.login(code=code))
print(f"\tJWT token : {auth.accessToken}")
print(f"\tGame Profile : {repr(auth.selectedProfile)}")

22
examples/chat_printer.py Normal file
View file

@ -0,0 +1,22 @@
"""
A super simple MinecraftClient exmaple.
Will connect to a server and print chat messages.
Requires an authorization code to login
"""
import sys
from aiocraft.client import MinecraftClient
from aiocraft.mc.proto import PacketChat
from aiocraft.util.helpers import parse_chat
server = sys.argv[1]
login_code = sys.argv[2] # Check authenticator.py example
app = MinecraftClient(server, login_code=login_code)
@app.on_packet(PacketChat)
async def on_chat(packet: PacketChat):
print(parse_chat(packet.message, ansi_color=True))
app.run()