File size: 6,330 Bytes
8e8ce9f
 
 
 
 
 
 
2c84504
8e8ce9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c84504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e8ce9f
 
 
 
 
 
 
 
 
 
 
 
 
 
2c84504
8e8ce9f
 
 
 
2c84504
8e8ce9f
 
 
 
2c84504
 
 
8e8ce9f
 
 
 
 
 
 
2c84504
8e8ce9f
 
 
 
2c84504
8e8ce9f
 
 
 
 
 
 
 
 
 
2c84504
8e8ce9f
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# ============================================
# PRODUCTION BOT WITH API - bot_with_api.py
# Clean command processing, no excess logs
# ============================================

import asyncio
import threading
import aiohttp                     # ADDED for keep-alive
from api import app, command_queue
import sys

# ============================================
# COMMAND PROCESSOR - Clean & Efficient
# ============================================

async def process_api_commands():
    """Process commands from API queue - production version"""
    while True:
        try:
            if not command_queue.empty():
                command = command_queue.get_nowait()
                
                if command['type'] == 'join':
                    await handle_join(command)
                
                elif command['type'] == 'emote':
                    await handle_emote(command)
            
            await asyncio.sleep(0.1)
        
        except Exception as e:
            print(f"[API ERROR] {e}")
            await asyncio.sleep(1)


# ============================================
# JOIN HANDLER
# ============================================

async def handle_join(command):
    """Handle join team command"""
    teamcode = command['teamcode']
    
    try:
        from main import online_writer, key, iv
        from xC4 import GenJoinSquadsPacket
        
        if not online_writer:
            print(f"[JOIN] Failed - Bot not connected")
            return
        
        if not key or not iv:
            print(f"[JOIN] Failed - No encryption keys")
            return
        
        packet = await GenJoinSquadsPacket(teamcode, key, iv)
        online_writer.write(packet)
        await online_writer.drain()
        
        print(f"[JOIN] βœ“ Team: {teamcode}")
        
    except Exception as e:
        print(f"[JOIN] βœ— Error: {e}")


# ============================================
# EMOTE HANDLER - Only works if in team
# ============================================

async def handle_emote(command):
    """Handle emote command - checks team status first"""
    uids = command['uids']
    emote_code = command['emote_code']
    
    try:
        from main import online_writer, key, iv, region, team_collection_active, team_members_data
        from xC4 import Emote_k
        
        # Check if bot is in team
        if not team_collection_active or len(team_members_data) == 0:
            print(f"[EMOTE] βœ— Bot not in team - Skipped")
            return
        
        if not online_writer or not key or not iv or not region:
            print(f"[EMOTE] βœ— Bot not ready")
            return
        
        success = 0
        failed = 0
        
        for uid in uids:
            try:
                packet = await Emote_k(
                    int(uid), 
                    int(emote_code), 
                    key, 
                    iv,
                    region
                )
                
                online_writer.write(packet)
                await online_writer.drain()
                success += 1
                
                await asyncio.sleep(0.3)  # Rate limit
                
            except Exception as e:
                failed += 1
        
        if success > 0:
            print(f"[EMOTE] βœ“ Sent: {success}/{len(uids)} | Code: {emote_code}")
        if failed > 0:
            print(f"[EMOTE] βœ— Failed: {failed}")
        
    except Exception as e:
        print(f"[EMOTE] βœ— Error: {e}")


# ============================================
# KEEP-ALIVE FOR HUGGING FACE
# ============================================

async def keep_alive():
    """Ping status endpoint every 30 minutes to keep Hugging Face space awake."""
    # Wait a bit for Flask to start
    await asyncio.sleep(30)
    url = "https://matrixcoder-emote-api.hf.space/status"
    while True:
        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as resp:
                    if resp.status == 200:
                        print("[KEEPALIVE] βœ“ Ping successful")
                    else:
                        print(f"[KEEPALIVE] βœ— Ping failed (status {resp.status})")
        except Exception as e:
            print(f"[KEEPALIVE] βœ— Error: {e}")
        await asyncio.sleep(1800)  # 30 minutes


# ============================================
# FLASK SERVER THREAD
# ============================================

def run_flask():
    """Run Flask in separate thread"""
    app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False)


# ============================================
# MAIN RUNNER
# ============================================

async def run_bot_with_api():
    """Main function - runs bot + API together + keep-alive"""
    
    # Start Flask API
    flask_thread = threading.Thread(target=run_flask, daemon=True)
    flask_thread.start()
    print("[API] Started on http://0.0.0.0:7860")
    
    # Start command processor
    api_processor = asyncio.create_task(process_api_commands())
    
    # Start keep-alive task
    keep_alive_task = asyncio.create_task(keep_alive())
    
    # Start main bot
    try:
        from main import StarTinG
        
        print("[BOT] Starting...")
        bot_task = asyncio.create_task(StarTinG())
        
        await asyncio.gather(api_processor, keep_alive_task, bot_task)
    
    except ImportError as e:
        print(f"[ERROR] Could not import bot: {e}")
        print("[API] Running in API-only mode...")
        await asyncio.gather(api_processor, keep_alive_task)


# ============================================
# ENTRY POINT
# ============================================

if __name__ == '__main__':
    print("""
╔══════════════════════════════════════════════╗
β•‘      FF Emote Bot - Production Mode          β•‘
β•‘      (Keep-alive active - pings every 30m)   β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Starting services...
    """)
    
    try:
        asyncio.run(run_bot_with_api())
    except KeyboardInterrupt:
        print("\n[SHUTDOWN] Stopping...")
        sys.exit(0)