-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminecraft_bot_mineproxy.py
More file actions
60 lines (49 loc) · 1.63 KB
/
minecraft_bot_mineproxy.py
File metadata and controls
60 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from mineproxy.client import MinecraftClient
import asyncio
import logging
# Setup logging
logging.basicConfig(level=logging.INFO)
async def main():
# Bot configuration
server_address = 'localhost'
server_port = 25565
username = 'MyPythonBot'
print(f"Attempting to connect to {server_address}:{server_port} as {username}")
# Create client instance
client = MinecraftClient()
# Set up event handlers
@client.on("login")
async def on_login():
print(f"Successfully logged in as {username}")
# Send a chat message after joining
await asyncio.sleep(3)
await client.chat("Hello! I am a Python bot.")
@client.on("chat")
async def on_chat(message, sender):
print(f"Chat: {sender} > {message}")
# Respond to specific commands
if message.lower() == "hello bot":
await client.chat(f"Hello, {sender}!")
@client.on("disconnect")
async def on_disconnect(reason):
print(f"Disconnected: {reason}")
# Connect to the server
try:
await client.connect(
server_address,
port=server_port,
username=username,
auth_mode="offline" # For offline-mode servers
)
# Keep the bot running
while client.connected:
await asyncio.sleep(1)
except KeyboardInterrupt:
print("Bot shutting down...")
except Exception as e:
print(f"Error: {e}")
finally:
if client.connected:
await client.disconnect("Bot shutting down")
if __name__ == "__main__":
asyncio.run(main())