Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,8 +3,10 @@ import asyncio
|
|
| 3 |
import threading
|
| 4 |
import logging
|
| 5 |
import traceback
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
|
|
|
| 8 |
import discord
|
| 9 |
import gradio as gr
|
| 10 |
|
|
@@ -18,26 +20,50 @@ DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
|
| 18 |
if not DISCORD_TOKEN:
|
| 19 |
raise RuntimeError(
|
| 20 |
"DISCORD_TOKEN is missing. "
|
| 21 |
-
"
|
| 22 |
)
|
| 23 |
|
| 24 |
|
| 25 |
# ============================================================
|
| 26 |
-
#
|
| 27 |
# ============================================================
|
| 28 |
|
| 29 |
-
LOG_LIMIT =
|
| 30 |
|
| 31 |
log_lines = []
|
| 32 |
log_lock = threading.Lock()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 37 |
|
| 38 |
|
| 39 |
def add_log(level, message):
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
with log_lock:
|
| 43 |
log_lines.append(line)
|
|
@@ -45,6 +71,22 @@ def add_log(level, message):
|
|
| 45 |
if len(log_lines) > LOG_LIMIT:
|
| 46 |
del log_lines[:-LOG_LIMIT]
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
print(line, flush=True)
|
| 49 |
|
| 50 |
|
|
@@ -54,13 +96,60 @@ def get_logs():
|
|
| 54 |
|
| 55 |
|
| 56 |
# ============================================================
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ============================================================
|
| 59 |
|
| 60 |
class DiscordLogHandler(logging.Handler):
|
| 61 |
|
| 62 |
def emit(self, record):
|
|
|
|
| 63 |
try:
|
|
|
|
| 64 |
message = self.format(record)
|
| 65 |
|
| 66 |
if record.levelno >= logging.ERROR:
|
|
@@ -75,7 +164,10 @@ class DiscordLogHandler(logging.Handler):
|
|
| 75 |
else:
|
| 76 |
level = "DEBUG"
|
| 77 |
|
| 78 |
-
add_log(
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
except Exception:
|
| 81 |
pass
|
|
@@ -84,20 +176,21 @@ class DiscordLogHandler(logging.Handler):
|
|
| 84 |
discord_handler = DiscordLogHandler()
|
| 85 |
|
| 86 |
discord_handler.setFormatter(
|
| 87 |
-
logging.Formatter(
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
add_log("SYSTEM", "Gradio interface initializing")
|
| 99 |
-
add_log("SYSTEM", "Discord client initializing")
|
| 100 |
-
add_log("SYSTEM", "==============================================")
|
| 101 |
|
| 102 |
|
| 103 |
# ============================================================
|
|
@@ -106,35 +199,46 @@ add_log("SYSTEM", "==============================================")
|
|
| 106 |
|
| 107 |
intents = discord.Intents.default()
|
| 108 |
|
| 109 |
-
# We don't need message content for this simple bot.
|
| 110 |
intents.guilds = True
|
| 111 |
|
| 112 |
|
| 113 |
# ============================================================
|
| 114 |
-
# DISCORD
|
| 115 |
# ============================================================
|
| 116 |
|
| 117 |
class OnlineBot(discord.Client):
|
| 118 |
|
| 119 |
def __init__(self, **kwargs):
|
|
|
|
| 120 |
super().__init__(**kwargs)
|
| 121 |
|
| 122 |
-
self.started_at = datetime.now()
|
| 123 |
self.ready_once = False
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
#
|
| 127 |
-
#
|
| 128 |
|
| 129 |
async def on_ready(self):
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
add_log(
|
| 132 |
"SUCCESS",
|
| 133 |
-
|
| 134 |
)
|
| 135 |
|
| 136 |
add_log(
|
| 137 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
f"Logged in as: {self.user}"
|
| 139 |
)
|
| 140 |
|
|
@@ -145,15 +249,21 @@ class OnlineBot(discord.Client):
|
|
| 145 |
|
| 146 |
add_log(
|
| 147 |
"INFO",
|
| 148 |
-
f"Connected
|
| 149 |
)
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
for guild in self.guilds:
|
| 152 |
|
| 153 |
add_log(
|
| 154 |
"INFO",
|
| 155 |
-
f"
|
| 156 |
-
f"(ID: {guild.id})"
|
| 157 |
)
|
| 158 |
|
| 159 |
if not self.ready_once:
|
|
@@ -161,26 +271,41 @@ class OnlineBot(discord.Client):
|
|
| 161 |
self.ready_once = True
|
| 162 |
|
| 163 |
add_log(
|
| 164 |
-
"
|
| 165 |
"Bot is ONLINE and ready"
|
| 166 |
)
|
| 167 |
|
| 168 |
else:
|
| 169 |
|
| 170 |
add_log(
|
| 171 |
-
"
|
| 172 |
-
"Bot
|
| 173 |
)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
async def on_guild_join(self, guild):
|
| 180 |
|
| 181 |
add_log(
|
| 182 |
"EVENT",
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
add_log(
|
|
@@ -190,25 +315,40 @@ class OnlineBot(discord.Client):
|
|
| 190 |
|
| 191 |
add_log(
|
| 192 |
"INFO",
|
| 193 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
-
channel = await self.find_first_writable_channel(
|
|
|
|
|
|
|
| 197 |
|
| 198 |
if channel is None:
|
| 199 |
|
| 200 |
add_log(
|
| 201 |
"ERROR",
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
return
|
| 207 |
|
| 208 |
add_log(
|
| 209 |
"SUCCESS",
|
| 210 |
-
f"First writable channel
|
| 211 |
-
f"#{channel.name}"
|
| 212 |
)
|
| 213 |
|
| 214 |
add_log(
|
|
@@ -216,6 +356,11 @@ class OnlineBot(discord.Client):
|
|
| 216 |
f"Channel ID: {channel.id}"
|
| 217 |
)
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
try:
|
| 220 |
|
| 221 |
await channel.send(
|
|
@@ -224,16 +369,26 @@ class OnlineBot(discord.Client):
|
|
| 224 |
|
| 225 |
add_log(
|
| 226 |
"SUCCESS",
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
except discord.Forbidden:
|
| 232 |
|
| 233 |
add_log(
|
| 234 |
"ERROR",
|
| 235 |
-
f"
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
)
|
| 238 |
|
| 239 |
except discord.HTTPException as e:
|
|
@@ -247,22 +402,36 @@ class OnlineBot(discord.Client):
|
|
| 247 |
|
| 248 |
add_log(
|
| 249 |
"ERROR",
|
| 250 |
-
f"Unexpected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
)
|
| 252 |
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
# FIND FIRST WRITABLE CHANNEL
|
| 255 |
-
#
|
| 256 |
|
| 257 |
-
async def find_first_writable_channel(
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
try:
|
| 260 |
|
| 261 |
-
# Discord normally returns channels according
|
| 262 |
-
# to their position.
|
| 263 |
channels = sorted(
|
| 264 |
guild.text_channels,
|
| 265 |
-
key=lambda channel:
|
|
|
|
|
|
|
|
|
|
| 266 |
)
|
| 267 |
|
| 268 |
add_log(
|
|
@@ -270,25 +439,54 @@ class OnlineBot(discord.Client):
|
|
| 270 |
f"Found {len(channels)} text channels"
|
| 271 |
)
|
| 272 |
|
| 273 |
-
|
| 274 |
|
| 275 |
-
|
| 276 |
-
guild.me
|
| 277 |
-
)
|
| 278 |
|
| 279 |
add_log(
|
| 280 |
-
"
|
| 281 |
-
|
| 282 |
-
f"view={permissions.view_channel} "
|
| 283 |
-
f"send={permissions.send_messages}"
|
| 284 |
)
|
| 285 |
|
| 286 |
-
|
| 287 |
-
permissions.view_channel
|
| 288 |
-
and permissions.send_messages
|
| 289 |
-
):
|
| 290 |
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
except Exception as e:
|
| 294 |
|
|
@@ -304,77 +502,263 @@ class OnlineBot(discord.Client):
|
|
| 304 |
|
| 305 |
return None
|
| 306 |
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
|
| 315 |
# ============================================================
|
| 316 |
-
#
|
| 317 |
# ============================================================
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
discord_logger.addHandler(discord_handler)
|
| 324 |
|
| 325 |
|
| 326 |
# ============================================================
|
| 327 |
-
# DISCORD
|
| 328 |
# ============================================================
|
| 329 |
|
| 330 |
-
bot_thread = None
|
| 331 |
-
|
| 332 |
-
|
| 333 |
def start_discord_bot():
|
| 334 |
|
| 335 |
global bot_thread
|
|
|
|
|
|
|
| 336 |
|
| 337 |
def runner():
|
| 338 |
|
|
|
|
|
|
|
|
|
|
| 339 |
add_log(
|
| 340 |
"SYSTEM",
|
| 341 |
"Starting Discord event loop..."
|
| 342 |
)
|
| 343 |
|
| 344 |
-
|
| 345 |
|
| 346 |
-
|
| 347 |
-
DISCORD_TOKEN,
|
| 348 |
-
log_handler=None
|
| 349 |
-
)
|
| 350 |
|
| 351 |
-
|
| 352 |
|
| 353 |
add_log(
|
| 354 |
-
"
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
| 357 |
)
|
| 358 |
|
| 359 |
-
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
)
|
| 377 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
bot_thread = threading.Thread(
|
| 379 |
target=runner,
|
| 380 |
daemon=True,
|
|
@@ -390,83 +774,158 @@ def start_discord_bot():
|
|
| 390 |
|
| 391 |
|
| 392 |
# ============================================================
|
| 393 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
# ============================================================
|
| 395 |
|
| 396 |
-
CUSTOM_HTML = """
|
| 397 |
<div class="dashboard">
|
| 398 |
|
| 399 |
-
<div class="
|
| 400 |
|
| 401 |
<div>
|
|
|
|
| 402 |
<div class="title">
|
| 403 |
Discord Bot Console
|
| 404 |
</div>
|
| 405 |
|
| 406 |
<div class="subtitle">
|
| 407 |
-
|
|
|
|
|
|
|
| 408 |
</div>
|
|
|
|
| 409 |
</div>
|
| 410 |
|
| 411 |
-
<div class="
|
| 412 |
-
|
|
|
|
|
|
|
| 413 |
LIVE
|
|
|
|
| 414 |
</div>
|
| 415 |
|
| 416 |
</div>
|
| 417 |
|
| 418 |
-
<div class="info-grid">
|
| 419 |
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
| 422 |
SERVICE
|
| 423 |
</div>
|
| 424 |
|
| 425 |
-
<div class="
|
| 426 |
-
|
| 427 |
</div>
|
|
|
|
| 428 |
</div>
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
| 432 |
RUNTIME
|
| 433 |
</div>
|
| 434 |
|
| 435 |
-
<div class="
|
| 436 |
-
|
| 437 |
</div>
|
|
|
|
| 438 |
</div>
|
| 439 |
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
| 443 |
</div>
|
| 444 |
|
| 445 |
-
<div class="
|
| 446 |
-
|
| 447 |
</div>
|
|
|
|
| 448 |
</div>
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
STATUS
|
| 453 |
</div>
|
| 454 |
|
| 455 |
-
<div class="
|
| 456 |
-
|
| 457 |
</div>
|
|
|
|
| 458 |
</div>
|
| 459 |
|
| 460 |
</div>
|
| 461 |
|
| 462 |
</div>
|
| 463 |
|
|
|
|
| 464 |
<style>
|
| 465 |
|
| 466 |
.dashboard {
|
|
|
|
| 467 |
width: 100%;
|
|
|
|
| 468 |
box-sizing: border-box;
|
| 469 |
-
|
| 470 |
font-family:
|
| 471 |
Inter,
|
| 472 |
system-ui,
|
|
@@ -474,92 +933,126 @@ CUSTOM_HTML = """
|
|
| 474 |
BlinkMacSystemFont,
|
| 475 |
"Segoe UI",
|
| 476 |
sans-serif;
|
|
|
|
| 477 |
}
|
| 478 |
|
| 479 |
-
|
|
|
|
|
|
|
| 480 |
display: flex;
|
|
|
|
| 481 |
justify-content: space-between;
|
|
|
|
| 482 |
align-items: center;
|
| 483 |
|
| 484 |
-
padding:
|
| 485 |
|
| 486 |
-
border:
|
| 487 |
|
| 488 |
-
border
|
|
|
|
|
|
|
| 489 |
|
| 490 |
background:
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
rgba(255,255,255,0.02)
|
| 495 |
-
);
|
| 496 |
|
| 497 |
-
margin-bottom: 14px;
|
| 498 |
}
|
| 499 |
|
|
|
|
| 500 |
.title {
|
| 501 |
-
|
|
|
|
|
|
|
| 502 |
font-weight: 800;
|
|
|
|
|
|
|
|
|
|
| 503 |
}
|
| 504 |
|
|
|
|
| 505 |
.subtitle {
|
| 506 |
-
margin-top: 5px;
|
| 507 |
|
| 508 |
-
|
| 509 |
|
| 510 |
font-size: 13px;
|
|
|
|
|
|
|
|
|
|
| 511 |
}
|
| 512 |
|
| 513 |
-
|
|
|
|
|
|
|
| 514 |
display: flex;
|
|
|
|
| 515 |
align-items: center;
|
| 516 |
|
| 517 |
gap: 8px;
|
| 518 |
|
| 519 |
padding:
|
| 520 |
-
|
| 521 |
-
|
| 522 |
|
| 523 |
border-radius: 999px;
|
| 524 |
|
| 525 |
-
|
| 526 |
|
| 527 |
-
|
| 528 |
|
| 529 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
|
| 531 |
-
font-weight: 800;
|
| 532 |
}
|
| 533 |
|
| 534 |
-
|
|
|
|
|
|
|
| 535 |
width: 8px;
|
|
|
|
| 536 |
height: 8px;
|
| 537 |
|
| 538 |
border-radius: 50%;
|
| 539 |
|
| 540 |
-
background:
|
|
|
|
| 541 |
|
| 542 |
box-shadow:
|
| 543 |
-
0 0
|
| 544 |
-
rgba(
|
|
|
|
| 545 |
}
|
| 546 |
|
| 547 |
-
|
|
|
|
|
|
|
| 548 |
display: grid;
|
| 549 |
|
| 550 |
grid-template-columns:
|
| 551 |
repeat(
|
| 552 |
auto-fit,
|
| 553 |
-
minmax(
|
|
|
|
|
|
|
|
|
|
| 554 |
);
|
| 555 |
|
| 556 |
gap: 10px;
|
|
|
|
| 557 |
}
|
| 558 |
|
| 559 |
-
.info-card {
|
| 560 |
-
padding: 15px;
|
| 561 |
|
| 562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
|
| 564 |
border:
|
| 565 |
1px solid
|
|
@@ -567,83 +1060,145 @@ CUSTOM_HTML = """
|
|
| 567 |
|
| 568 |
background:
|
| 569 |
rgba(255,255,255,0.025);
|
|
|
|
| 570 |
}
|
| 571 |
|
| 572 |
-
.label {
|
| 573 |
-
font-size: 10px;
|
| 574 |
|
| 575 |
-
|
|
|
|
|
|
|
| 576 |
|
| 577 |
font-weight: 700;
|
| 578 |
|
|
|
|
|
|
|
| 579 |
letter-spacing: 1px;
|
|
|
|
| 580 |
}
|
| 581 |
|
| 582 |
-
|
| 583 |
-
|
|
|
|
|
|
|
| 584 |
|
| 585 |
font-size: 15px;
|
| 586 |
|
| 587 |
-
font-weight:
|
|
|
|
| 588 |
}
|
| 589 |
|
|
|
|
| 590 |
.online {
|
| 591 |
-
|
|
|
|
|
|
|
|
|
|
| 592 |
}
|
| 593 |
|
| 594 |
</style>
|
| 595 |
"""
|
| 596 |
|
| 597 |
|
| 598 |
-
def refresh_logs():
|
| 599 |
-
return get_logs()
|
| 600 |
-
|
| 601 |
-
|
| 602 |
# ============================================================
|
| 603 |
-
#
|
| 604 |
# ============================================================
|
| 605 |
|
| 606 |
with gr.Blocks(
|
| 607 |
title="Discord Bot Console"
|
| 608 |
) as app:
|
| 609 |
|
| 610 |
-
gr.HTML(
|
|
|
|
|
|
|
| 611 |
|
| 612 |
gr.Markdown(
|
| 613 |
"## Live Console"
|
| 614 |
)
|
| 615 |
|
| 616 |
logs = gr.Textbox(
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
| 627 |
)
|
| 628 |
|
| 629 |
-
|
| 630 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
outputs=logs
|
| 632 |
)
|
| 633 |
|
| 634 |
-
|
| 635 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
value=1
|
| 637 |
)
|
| 638 |
|
| 639 |
-
|
| 640 |
-
fn=
|
| 641 |
outputs=logs
|
| 642 |
)
|
| 643 |
|
| 644 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
# ============================================================
|
| 646 |
-
# START DISCORD
|
| 647 |
# ============================================================
|
| 648 |
|
| 649 |
start_discord_bot()
|
|
@@ -658,6 +1213,11 @@ add_log(
|
|
| 658 |
"Starting Gradio web interface..."
|
| 659 |
)
|
| 660 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
app.launch(
|
| 662 |
server_name="0.0.0.0",
|
| 663 |
server_port=7860,
|
|
|
|
| 3 |
import threading
|
| 4 |
import logging
|
| 5 |
import traceback
|
| 6 |
+
import time
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
+
import aiohttp
|
| 10 |
import discord
|
| 11 |
import gradio as gr
|
| 12 |
|
|
|
|
| 20 |
if not DISCORD_TOKEN:
|
| 21 |
raise RuntimeError(
|
| 22 |
"DISCORD_TOKEN is missing. "
|
| 23 |
+
"Please add DISCORD_TOKEN to Hugging Face Space Secrets."
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
# ============================================================
|
| 28 |
+
# GLOBAL STATE
|
| 29 |
# ============================================================
|
| 30 |
|
| 31 |
+
LOG_LIMIT = 2000
|
| 32 |
|
| 33 |
log_lines = []
|
| 34 |
log_lock = threading.Lock()
|
| 35 |
|
| 36 |
+
bot_thread = None
|
| 37 |
+
|
| 38 |
+
service_started_at = datetime.now()
|
| 39 |
+
|
| 40 |
+
connection_attempts = 0
|
| 41 |
+
successful_connections = 0
|
| 42 |
+
reconnect_count = 0
|
| 43 |
+
|
| 44 |
+
last_status = "STARTING"
|
| 45 |
+
last_error = "None"
|
| 46 |
+
|
| 47 |
+
bot_instance = None
|
| 48 |
+
|
| 49 |
|
| 50 |
+
# ============================================================
|
| 51 |
+
# LOG SYSTEM
|
| 52 |
+
# ============================================================
|
| 53 |
+
|
| 54 |
+
def current_time():
|
| 55 |
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 56 |
|
| 57 |
|
| 58 |
def add_log(level, message):
|
| 59 |
+
global last_status
|
| 60 |
+
global last_error
|
| 61 |
+
|
| 62 |
+
line = (
|
| 63 |
+
f"[{current_time()}] "
|
| 64 |
+
f"[{level.upper():8}] "
|
| 65 |
+
f"{message}"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
with log_lock:
|
| 69 |
log_lines.append(line)
|
|
|
|
| 71 |
if len(log_lines) > LOG_LIMIT:
|
| 72 |
del log_lines[:-LOG_LIMIT]
|
| 73 |
|
| 74 |
+
if level.upper() == "ERROR":
|
| 75 |
+
last_error = message
|
| 76 |
+
|
| 77 |
+
if level.upper() in [
|
| 78 |
+
"SUCCESS",
|
| 79 |
+
"ONLINE"
|
| 80 |
+
]:
|
| 81 |
+
last_status = "ONLINE"
|
| 82 |
+
|
| 83 |
+
elif level.upper() == "WARNING":
|
| 84 |
+
if last_status != "ONLINE":
|
| 85 |
+
last_status = "WARNING"
|
| 86 |
+
|
| 87 |
+
elif level.upper() == "ERROR":
|
| 88 |
+
last_status = "ERROR"
|
| 89 |
+
|
| 90 |
print(line, flush=True)
|
| 91 |
|
| 92 |
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
# ============================================================
|
| 99 |
+
# STARTUP LOGS
|
| 100 |
+
# ============================================================
|
| 101 |
+
|
| 102 |
+
add_log(
|
| 103 |
+
"SYSTEM",
|
| 104 |
+
"=================================================="
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
add_log(
|
| 108 |
+
"SYSTEM",
|
| 109 |
+
"Discord Bot Service is starting"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
add_log(
|
| 113 |
+
"SYSTEM",
|
| 114 |
+
"Hugging Face Space detected"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
add_log(
|
| 118 |
+
"SYSTEM",
|
| 119 |
+
"Python runtime initialized"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
add_log(
|
| 123 |
+
"SYSTEM",
|
| 124 |
+
"Discord.py initializing"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
add_log(
|
| 128 |
+
"SYSTEM",
|
| 129 |
+
"Gradio interface initializing"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
add_log(
|
| 133 |
+
"SYSTEM",
|
| 134 |
+
"Live logging system initialized"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
add_log(
|
| 138 |
+
"SYSTEM",
|
| 139 |
+
"=================================================="
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# ============================================================
|
| 144 |
+
# DISCORD LOGGING
|
| 145 |
# ============================================================
|
| 146 |
|
| 147 |
class DiscordLogHandler(logging.Handler):
|
| 148 |
|
| 149 |
def emit(self, record):
|
| 150 |
+
|
| 151 |
try:
|
| 152 |
+
|
| 153 |
message = self.format(record)
|
| 154 |
|
| 155 |
if record.levelno >= logging.ERROR:
|
|
|
|
| 164 |
else:
|
| 165 |
level = "DEBUG"
|
| 166 |
|
| 167 |
+
add_log(
|
| 168 |
+
level,
|
| 169 |
+
message
|
| 170 |
+
)
|
| 171 |
|
| 172 |
except Exception:
|
| 173 |
pass
|
|
|
|
| 176 |
discord_handler = DiscordLogHandler()
|
| 177 |
|
| 178 |
discord_handler.setFormatter(
|
| 179 |
+
logging.Formatter(
|
| 180 |
+
"%(name)s: %(message)s"
|
| 181 |
+
)
|
| 182 |
)
|
| 183 |
|
| 184 |
|
| 185 |
+
discord_logger = logging.getLogger("discord")
|
| 186 |
+
|
| 187 |
+
discord_logger.setLevel(
|
| 188 |
+
logging.INFO
|
| 189 |
+
)
|
| 190 |
|
| 191 |
+
discord_logger.addHandler(
|
| 192 |
+
discord_handler
|
| 193 |
+
)
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
|
| 196 |
# ============================================================
|
|
|
|
| 199 |
|
| 200 |
intents = discord.Intents.default()
|
| 201 |
|
|
|
|
| 202 |
intents.guilds = True
|
| 203 |
|
| 204 |
|
| 205 |
# ============================================================
|
| 206 |
+
# DISCORD BOT
|
| 207 |
# ============================================================
|
| 208 |
|
| 209 |
class OnlineBot(discord.Client):
|
| 210 |
|
| 211 |
def __init__(self, **kwargs):
|
| 212 |
+
|
| 213 |
super().__init__(**kwargs)
|
| 214 |
|
|
|
|
| 215 |
self.ready_once = False
|
| 216 |
|
| 217 |
+
# ========================================================
|
| 218 |
+
# ON READY
|
| 219 |
+
# ========================================================
|
| 220 |
|
| 221 |
async def on_ready(self):
|
| 222 |
|
| 223 |
+
global successful_connections
|
| 224 |
+
global last_status
|
| 225 |
+
|
| 226 |
+
successful_connections += 1
|
| 227 |
+
|
| 228 |
+
last_status = "ONLINE"
|
| 229 |
+
|
| 230 |
add_log(
|
| 231 |
"SUCCESS",
|
| 232 |
+
"=================================================="
|
| 233 |
)
|
| 234 |
|
| 235 |
add_log(
|
| 236 |
+
"SUCCESS",
|
| 237 |
+
"Discord connection established"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
add_log(
|
| 241 |
+
"SUCCESS",
|
| 242 |
f"Logged in as: {self.user}"
|
| 243 |
)
|
| 244 |
|
|
|
|
| 249 |
|
| 250 |
add_log(
|
| 251 |
"INFO",
|
| 252 |
+
f"Connected servers: {len(self.guilds)}"
|
| 253 |
)
|
| 254 |
|
| 255 |
+
if self.user:
|
| 256 |
+
|
| 257 |
+
add_log(
|
| 258 |
+
"INFO",
|
| 259 |
+
f"Bot username: {self.user.name}"
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
for guild in self.guilds:
|
| 263 |
|
| 264 |
add_log(
|
| 265 |
"INFO",
|
| 266 |
+
f"Server: {guild.name} | ID: {guild.id}"
|
|
|
|
| 267 |
)
|
| 268 |
|
| 269 |
if not self.ready_once:
|
|
|
|
| 271 |
self.ready_once = True
|
| 272 |
|
| 273 |
add_log(
|
| 274 |
+
"ONLINE",
|
| 275 |
"Bot is ONLINE and ready"
|
| 276 |
)
|
| 277 |
|
| 278 |
else:
|
| 279 |
|
| 280 |
add_log(
|
| 281 |
+
"SUCCESS",
|
| 282 |
+
"Bot successfully reconnected"
|
| 283 |
)
|
| 284 |
|
| 285 |
+
add_log(
|
| 286 |
+
"SUCCESS",
|
| 287 |
+
"=================================================="
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
# ========================================================
|
| 291 |
+
# NEW SERVER
|
| 292 |
+
# ========================================================
|
| 293 |
|
| 294 |
async def on_guild_join(self, guild):
|
| 295 |
|
| 296 |
add_log(
|
| 297 |
"EVENT",
|
| 298 |
+
"=================================================="
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
add_log(
|
| 302 |
+
"EVENT",
|
| 303 |
+
"Bot was added to a new server"
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
add_log(
|
| 307 |
+
"INFO",
|
| 308 |
+
f"Server name: {guild.name}"
|
| 309 |
)
|
| 310 |
|
| 311 |
add_log(
|
|
|
|
| 315 |
|
| 316 |
add_log(
|
| 317 |
"INFO",
|
| 318 |
+
f"Server owner ID: {guild.owner_id}"
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
add_log(
|
| 322 |
+
"INFO",
|
| 323 |
+
f"Member count: {guild.member_count}"
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
add_log(
|
| 327 |
+
"INFO",
|
| 328 |
+
"Searching for the first writable text channel..."
|
| 329 |
)
|
| 330 |
|
| 331 |
+
channel = await self.find_first_writable_channel(
|
| 332 |
+
guild
|
| 333 |
+
)
|
| 334 |
|
| 335 |
if channel is None:
|
| 336 |
|
| 337 |
add_log(
|
| 338 |
"ERROR",
|
| 339 |
+
"No writable text channel was found"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
add_log(
|
| 343 |
+
"ERROR",
|
| 344 |
+
f"Server: {guild.name}"
|
| 345 |
)
|
| 346 |
|
| 347 |
return
|
| 348 |
|
| 349 |
add_log(
|
| 350 |
"SUCCESS",
|
| 351 |
+
f"First writable channel: #{channel.name}"
|
|
|
|
| 352 |
)
|
| 353 |
|
| 354 |
add_log(
|
|
|
|
| 356 |
f"Channel ID: {channel.id}"
|
| 357 |
)
|
| 358 |
|
| 359 |
+
add_log(
|
| 360 |
+
"INFO",
|
| 361 |
+
"Attempting to send online message..."
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
try:
|
| 365 |
|
| 366 |
await channel.send(
|
|
|
|
| 369 |
|
| 370 |
add_log(
|
| 371 |
"SUCCESS",
|
| 372 |
+
"Hello, I'm online"
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
add_log(
|
| 376 |
+
"SUCCESS",
|
| 377 |
+
f"Message sent to #{channel.name}"
|
| 378 |
)
|
| 379 |
|
| 380 |
except discord.Forbidden:
|
| 381 |
|
| 382 |
add_log(
|
| 383 |
"ERROR",
|
| 384 |
+
f"Permission denied in #{channel.name}"
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
except discord.NotFound:
|
| 388 |
+
|
| 389 |
+
add_log(
|
| 390 |
+
"ERROR",
|
| 391 |
+
f"Channel #{channel.name} no longer exists"
|
| 392 |
)
|
| 393 |
|
| 394 |
except discord.HTTPException as e:
|
|
|
|
| 402 |
|
| 403 |
add_log(
|
| 404 |
"ERROR",
|
| 405 |
+
f"Unexpected channel message error: {e}"
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
add_log(
|
| 409 |
+
"ERROR",
|
| 410 |
+
traceback.format_exc()
|
| 411 |
)
|
| 412 |
|
| 413 |
+
add_log(
|
| 414 |
+
"EVENT",
|
| 415 |
+
"=================================================="
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
# ========================================================
|
| 419 |
# FIND FIRST WRITABLE CHANNEL
|
| 420 |
+
# ========================================================
|
| 421 |
|
| 422 |
+
async def find_first_writable_channel(
|
| 423 |
+
self,
|
| 424 |
+
guild
|
| 425 |
+
):
|
| 426 |
|
| 427 |
try:
|
| 428 |
|
|
|
|
|
|
|
| 429 |
channels = sorted(
|
| 430 |
guild.text_channels,
|
| 431 |
+
key=lambda channel: (
|
| 432 |
+
channel.position,
|
| 433 |
+
channel.id
|
| 434 |
+
)
|
| 435 |
)
|
| 436 |
|
| 437 |
add_log(
|
|
|
|
| 439 |
f"Found {len(channels)} text channels"
|
| 440 |
)
|
| 441 |
|
| 442 |
+
me = guild.me
|
| 443 |
|
| 444 |
+
if me is None:
|
|
|
|
|
|
|
| 445 |
|
| 446 |
add_log(
|
| 447 |
+
"WARNING",
|
| 448 |
+
"guild.me is unavailable"
|
|
|
|
|
|
|
| 449 |
)
|
| 450 |
|
| 451 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
+
for index, channel in enumerate(
|
| 454 |
+
channels,
|
| 455 |
+
start=1
|
| 456 |
+
):
|
| 457 |
+
|
| 458 |
+
try:
|
| 459 |
+
|
| 460 |
+
permissions = channel.permissions_for(
|
| 461 |
+
me
|
| 462 |
+
)
|
| 463 |
+
|
| 464 |
+
add_log(
|
| 465 |
+
"DEBUG",
|
| 466 |
+
(
|
| 467 |
+
f"Channel #{channel.name} "
|
| 468 |
+
f"[{index}/{len(channels)}] | "
|
| 469 |
+
f"view={permissions.view_channel} | "
|
| 470 |
+
f"send={permissions.send_messages}"
|
| 471 |
+
)
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
if (
|
| 475 |
+
permissions.view_channel
|
| 476 |
+
and permissions.send_messages
|
| 477 |
+
):
|
| 478 |
+
|
| 479 |
+
return channel
|
| 480 |
+
|
| 481 |
+
except Exception as e:
|
| 482 |
+
|
| 483 |
+
add_log(
|
| 484 |
+
"WARNING",
|
| 485 |
+
(
|
| 486 |
+
f"Could not check "
|
| 487 |
+
f"#{channel.name}: {e}"
|
| 488 |
+
)
|
| 489 |
+
)
|
| 490 |
|
| 491 |
except Exception as e:
|
| 492 |
|
|
|
|
| 502 |
|
| 503 |
return None
|
| 504 |
|
| 505 |
+
# ========================================================
|
| 506 |
+
# GUILD REMOVE
|
| 507 |
+
# ========================================================
|
| 508 |
|
| 509 |
+
async def on_guild_remove(self, guild):
|
| 510 |
+
|
| 511 |
+
add_log(
|
| 512 |
+
"EVENT",
|
| 513 |
+
f"Bot left server: {guild.name}"
|
| 514 |
+
)
|
| 515 |
|
| 516 |
+
add_log(
|
| 517 |
+
"INFO",
|
| 518 |
+
f"Server ID: {guild.id}"
|
| 519 |
+
)
|
| 520 |
|
| 521 |
|
| 522 |
# ============================================================
|
| 523 |
+
# CREATE BOT
|
| 524 |
# ============================================================
|
| 525 |
|
| 526 |
+
bot_instance = OnlineBot(
|
| 527 |
+
intents=intents
|
| 528 |
+
)
|
|
|
|
|
|
|
| 529 |
|
| 530 |
|
| 531 |
# ============================================================
|
| 532 |
+
# DISCORD BOT RUNNER
|
| 533 |
# ============================================================
|
| 534 |
|
|
|
|
|
|
|
|
|
|
| 535 |
def start_discord_bot():
|
| 536 |
|
| 537 |
global bot_thread
|
| 538 |
+
global connection_attempts
|
| 539 |
+
global reconnect_count
|
| 540 |
|
| 541 |
def runner():
|
| 542 |
|
| 543 |
+
global connection_attempts
|
| 544 |
+
global reconnect_count
|
| 545 |
+
|
| 546 |
add_log(
|
| 547 |
"SYSTEM",
|
| 548 |
"Starting Discord event loop..."
|
| 549 |
)
|
| 550 |
|
| 551 |
+
retry_delay = 10
|
| 552 |
|
| 553 |
+
while True:
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
+
connection_attempts += 1
|
| 556 |
|
| 557 |
add_log(
|
| 558 |
+
"INFO",
|
| 559 |
+
(
|
| 560 |
+
f"Discord connection attempt "
|
| 561 |
+
f"#{connection_attempts}"
|
| 562 |
+
)
|
| 563 |
)
|
| 564 |
|
| 565 |
+
try:
|
| 566 |
|
| 567 |
+
add_log(
|
| 568 |
+
"INFO",
|
| 569 |
+
"Connecting to Discord Gateway..."
|
| 570 |
+
)
|
| 571 |
|
| 572 |
+
bot_instance.run(
|
| 573 |
+
DISCORD_TOKEN,
|
| 574 |
+
reconnect=True,
|
| 575 |
+
log_handler=None
|
| 576 |
+
)
|
| 577 |
|
| 578 |
+
add_log(
|
| 579 |
+
"WARNING",
|
| 580 |
+
"Discord bot stopped normally"
|
| 581 |
+
)
|
| 582 |
|
| 583 |
+
# ------------------------------------------------
|
| 584 |
+
# INVALID TOKEN
|
| 585 |
+
# ------------------------------------------------
|
| 586 |
+
|
| 587 |
+
except discord.LoginFailure:
|
| 588 |
+
|
| 589 |
+
add_log(
|
| 590 |
+
"ERROR",
|
| 591 |
+
"Discord login failed"
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
add_log(
|
| 595 |
+
"ERROR",
|
| 596 |
+
"DISCORD_TOKEN is invalid"
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
add_log(
|
| 600 |
+
"ERROR",
|
| 601 |
+
"Fix the token in Hugging Face Secrets"
|
| 602 |
+
)
|
| 603 |
+
|
| 604 |
+
break
|
| 605 |
+
|
| 606 |
+
# ------------------------------------------------
|
| 607 |
+
# PRIVILEGED INTENTS
|
| 608 |
+
# ------------------------------------------------
|
| 609 |
+
|
| 610 |
+
except discord.PrivilegedIntentsRequired:
|
| 611 |
+
|
| 612 |
+
add_log(
|
| 613 |
+
"ERROR",
|
| 614 |
+
"Discord requires privileged intents"
|
| 615 |
+
)
|
| 616 |
+
|
| 617 |
+
add_log(
|
| 618 |
+
"ERROR",
|
| 619 |
+
"Check Discord Developer Portal"
|
| 620 |
+
)
|
| 621 |
+
|
| 622 |
+
break
|
| 623 |
+
|
| 624 |
+
# ------------------------------------------------
|
| 625 |
+
# AIOHTTP CONNECTOR ERROR
|
| 626 |
+
# ------------------------------------------------
|
| 627 |
+
|
| 628 |
+
except aiohttp.ClientConnectorError as e:
|
| 629 |
+
|
| 630 |
+
reconnect_count += 1
|
| 631 |
+
|
| 632 |
+
add_log(
|
| 633 |
+
"ERROR",
|
| 634 |
+
(
|
| 635 |
+
"Cannot connect to Discord "
|
| 636 |
+
f"HTTPS endpoint: {e}"
|
| 637 |
+
)
|
| 638 |
+
)
|
| 639 |
+
|
| 640 |
+
add_log(
|
| 641 |
+
"WARNING",
|
| 642 |
+
"Discord network connection failed"
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
add_log(
|
| 646 |
+
"INFO",
|
| 647 |
+
(
|
| 648 |
+
f"Retrying in "
|
| 649 |
+
f"{retry_delay} seconds..."
|
| 650 |
+
)
|
| 651 |
+
)
|
| 652 |
+
|
| 653 |
+
# ------------------------------------------------
|
| 654 |
+
# CONNECTION RESET
|
| 655 |
+
# ------------------------------------------------
|
| 656 |
+
|
| 657 |
+
except ConnectionResetError as e:
|
| 658 |
+
|
| 659 |
+
reconnect_count += 1
|
| 660 |
+
|
| 661 |
+
add_log(
|
| 662 |
+
"ERROR",
|
| 663 |
+
f"Connection reset by remote host: {e}"
|
| 664 |
+
)
|
| 665 |
+
|
| 666 |
+
add_log(
|
| 667 |
+
"WARNING",
|
| 668 |
+
"Discord connection was reset"
|
| 669 |
+
)
|
| 670 |
+
|
| 671 |
+
add_log(
|
| 672 |
+
"INFO",
|
| 673 |
+
(
|
| 674 |
+
f"Retrying in "
|
| 675 |
+
f"{retry_delay} seconds..."
|
| 676 |
+
)
|
| 677 |
+
)
|
| 678 |
+
|
| 679 |
+
# ------------------------------------------------
|
| 680 |
+
# TIMEOUT
|
| 681 |
+
# ------------------------------------------------
|
| 682 |
+
|
| 683 |
+
except asyncio.TimeoutError:
|
| 684 |
+
|
| 685 |
+
reconnect_count += 1
|
| 686 |
+
|
| 687 |
+
add_log(
|
| 688 |
+
"ERROR",
|
| 689 |
+
"Discord connection timed out"
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
add_log(
|
| 693 |
+
"INFO",
|
| 694 |
+
(
|
| 695 |
+
f"Retrying in "
|
| 696 |
+
f"{retry_delay} seconds..."
|
| 697 |
+
)
|
| 698 |
+
)
|
| 699 |
+
|
| 700 |
+
# ------------------------------------------------
|
| 701 |
+
# NETWORK ERROR
|
| 702 |
+
# ------------------------------------------------
|
| 703 |
+
|
| 704 |
+
except ConnectionError as e:
|
| 705 |
+
|
| 706 |
+
reconnect_count += 1
|
| 707 |
+
|
| 708 |
+
add_log(
|
| 709 |
+
"ERROR",
|
| 710 |
+
f"Network error: {e}"
|
| 711 |
+
)
|
| 712 |
+
|
| 713 |
+
add_log(
|
| 714 |
+
"INFO",
|
| 715 |
+
(
|
| 716 |
+
f"Retrying in "
|
| 717 |
+
f"{retry_delay} seconds..."
|
| 718 |
+
)
|
| 719 |
+
)
|
| 720 |
+
|
| 721 |
+
# ------------------------------------------------
|
| 722 |
+
# GENERAL ERROR
|
| 723 |
+
# ------------------------------------------------
|
| 724 |
+
|
| 725 |
+
except Exception as e:
|
| 726 |
+
|
| 727 |
+
reconnect_count += 1
|
| 728 |
+
|
| 729 |
+
add_log(
|
| 730 |
+
"ERROR",
|
| 731 |
+
f"Discord bot stopped with error: {e}"
|
| 732 |
+
)
|
| 733 |
+
|
| 734 |
+
add_log(
|
| 735 |
+
"ERROR",
|
| 736 |
+
traceback.format_exc()
|
| 737 |
+
)
|
| 738 |
+
|
| 739 |
+
add_log(
|
| 740 |
+
"INFO",
|
| 741 |
+
(
|
| 742 |
+
f"Retrying in "
|
| 743 |
+
f"{retry_delay} seconds..."
|
| 744 |
+
)
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
# ------------------------------------------------
|
| 748 |
+
# RETRY
|
| 749 |
+
# ------------------------------------------------
|
| 750 |
+
|
| 751 |
+
time.sleep(
|
| 752 |
+
retry_delay
|
| 753 |
)
|
| 754 |
|
| 755 |
+
if retry_delay < 60:
|
| 756 |
+
|
| 757 |
+
retry_delay = min(
|
| 758 |
+
retry_delay + 5,
|
| 759 |
+
60
|
| 760 |
+
)
|
| 761 |
+
|
| 762 |
bot_thread = threading.Thread(
|
| 763 |
target=runner,
|
| 764 |
daemon=True,
|
|
|
|
| 774 |
|
| 775 |
|
| 776 |
# ============================================================
|
| 777 |
+
# SERVICE INFORMATION
|
| 778 |
+
# ============================================================
|
| 779 |
+
|
| 780 |
+
def get_status():
|
| 781 |
+
|
| 782 |
+
uptime = datetime.now() - service_started_at
|
| 783 |
+
|
| 784 |
+
total_seconds = int(
|
| 785 |
+
uptime.total_seconds()
|
| 786 |
+
)
|
| 787 |
+
|
| 788 |
+
days = total_seconds // 86400
|
| 789 |
+
|
| 790 |
+
hours = (
|
| 791 |
+
total_seconds % 86400
|
| 792 |
+
) // 3600
|
| 793 |
+
|
| 794 |
+
minutes = (
|
| 795 |
+
total_seconds % 3600
|
| 796 |
+
) // 60
|
| 797 |
+
|
| 798 |
+
seconds = (
|
| 799 |
+
total_seconds % 60
|
| 800 |
+
)
|
| 801 |
+
|
| 802 |
+
if bot_instance.is_ready():
|
| 803 |
+
|
| 804 |
+
status = "🟢 ONLINE"
|
| 805 |
+
|
| 806 |
+
elif bot_instance.is_closed():
|
| 807 |
+
|
| 808 |
+
status = "🔴 DISCONNECTED"
|
| 809 |
+
|
| 810 |
+
else:
|
| 811 |
+
|
| 812 |
+
status = "🟡 CONNECTING"
|
| 813 |
+
|
| 814 |
+
return (
|
| 815 |
+
f"Service status: {status}\n"
|
| 816 |
+
f"Service uptime: "
|
| 817 |
+
f"{days}d {hours}h {minutes}m {seconds}s\n"
|
| 818 |
+
f"Connection attempts: {connection_attempts}\n"
|
| 819 |
+
f"Successful connections: {successful_connections}\n"
|
| 820 |
+
f"Reconnect attempts: {reconnect_count}\n"
|
| 821 |
+
f"Discord servers: {len(bot_instance.guilds)}\n"
|
| 822 |
+
f"Bot user: {bot_instance.user}\n"
|
| 823 |
+
f"Bot ID: "
|
| 824 |
+
f"{bot_instance.user.id if bot_instance.user else 'N/A'}\n"
|
| 825 |
+
f"Last error: {last_error}"
|
| 826 |
+
)
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
# ============================================================
|
| 830 |
+
# CUSTOM HTML
|
| 831 |
# ============================================================
|
| 832 |
|
| 833 |
+
CUSTOM_HTML = r"""
|
| 834 |
<div class="dashboard">
|
| 835 |
|
| 836 |
+
<div class="top">
|
| 837 |
|
| 838 |
<div>
|
| 839 |
+
|
| 840 |
<div class="title">
|
| 841 |
Discord Bot Console
|
| 842 |
</div>
|
| 843 |
|
| 844 |
<div class="subtitle">
|
| 845 |
+
Lightweight Discord Service
|
| 846 |
+
• Hugging Face
|
| 847 |
+
• Gradio
|
| 848 |
</div>
|
| 849 |
+
|
| 850 |
</div>
|
| 851 |
|
| 852 |
+
<div class="live">
|
| 853 |
+
|
| 854 |
+
<span class="pulse"></span>
|
| 855 |
+
|
| 856 |
LIVE
|
| 857 |
+
|
| 858 |
</div>
|
| 859 |
|
| 860 |
</div>
|
| 861 |
|
|
|
|
| 862 |
|
| 863 |
+
<div class="cards">
|
| 864 |
+
|
| 865 |
+
<div class="card">
|
| 866 |
+
|
| 867 |
+
<div class="small">
|
| 868 |
SERVICE
|
| 869 |
</div>
|
| 870 |
|
| 871 |
+
<div class="big">
|
| 872 |
+
DISCORD BOT
|
| 873 |
</div>
|
| 874 |
+
|
| 875 |
</div>
|
| 876 |
|
| 877 |
+
|
| 878 |
+
<div class="card">
|
| 879 |
+
|
| 880 |
+
<div class="small">
|
| 881 |
RUNTIME
|
| 882 |
</div>
|
| 883 |
|
| 884 |
+
<div class="big">
|
| 885 |
+
PYTHON
|
| 886 |
</div>
|
| 887 |
+
|
| 888 |
</div>
|
| 889 |
|
| 890 |
+
|
| 891 |
+
<div class="card">
|
| 892 |
+
|
| 893 |
+
<div class="small">
|
| 894 |
+
WEB UI
|
| 895 |
</div>
|
| 896 |
|
| 897 |
+
<div class="big">
|
| 898 |
+
GRADIO
|
| 899 |
</div>
|
| 900 |
+
|
| 901 |
</div>
|
| 902 |
|
| 903 |
+
|
| 904 |
+
<div class="card">
|
| 905 |
+
|
| 906 |
+
<div class="small">
|
| 907 |
STATUS
|
| 908 |
</div>
|
| 909 |
|
| 910 |
+
<div class="big online">
|
| 911 |
+
LIVE
|
| 912 |
</div>
|
| 913 |
+
|
| 914 |
</div>
|
| 915 |
|
| 916 |
</div>
|
| 917 |
|
| 918 |
</div>
|
| 919 |
|
| 920 |
+
|
| 921 |
<style>
|
| 922 |
|
| 923 |
.dashboard {
|
| 924 |
+
|
| 925 |
width: 100%;
|
| 926 |
+
|
| 927 |
box-sizing: border-box;
|
| 928 |
+
|
| 929 |
font-family:
|
| 930 |
Inter,
|
| 931 |
system-ui,
|
|
|
|
| 933 |
BlinkMacSystemFont,
|
| 934 |
"Segoe UI",
|
| 935 |
sans-serif;
|
| 936 |
+
|
| 937 |
}
|
| 938 |
|
| 939 |
+
|
| 940 |
+
.top {
|
| 941 |
+
|
| 942 |
display: flex;
|
| 943 |
+
|
| 944 |
justify-content: space-between;
|
| 945 |
+
|
| 946 |
align-items: center;
|
| 947 |
|
| 948 |
+
padding: 24px;
|
| 949 |
|
| 950 |
+
border-radius: 20px;
|
| 951 |
|
| 952 |
+
border:
|
| 953 |
+
1px solid
|
| 954 |
+
rgba(255,255,255,0.08);
|
| 955 |
|
| 956 |
background:
|
| 957 |
+
rgba(255,255,255,0.035);
|
| 958 |
+
|
| 959 |
+
margin-bottom: 12px;
|
|
|
|
|
|
|
| 960 |
|
|
|
|
| 961 |
}
|
| 962 |
|
| 963 |
+
|
| 964 |
.title {
|
| 965 |
+
|
| 966 |
+
font-size: 27px;
|
| 967 |
+
|
| 968 |
font-weight: 800;
|
| 969 |
+
|
| 970 |
+
letter-spacing: -0.5px;
|
| 971 |
+
|
| 972 |
}
|
| 973 |
|
| 974 |
+
|
| 975 |
.subtitle {
|
|
|
|
| 976 |
|
| 977 |
+
margin-top: 6px;
|
| 978 |
|
| 979 |
font-size: 13px;
|
| 980 |
+
|
| 981 |
+
opacity: 0.55;
|
| 982 |
+
|
| 983 |
}
|
| 984 |
|
| 985 |
+
|
| 986 |
+
.live {
|
| 987 |
+
|
| 988 |
display: flex;
|
| 989 |
+
|
| 990 |
align-items: center;
|
| 991 |
|
| 992 |
gap: 8px;
|
| 993 |
|
| 994 |
padding:
|
| 995 |
+
8px
|
| 996 |
+
13px;
|
| 997 |
|
| 998 |
border-radius: 999px;
|
| 999 |
|
| 1000 |
+
font-size: 11px;
|
| 1001 |
|
| 1002 |
+
font-weight: 800;
|
| 1003 |
|
| 1004 |
+
letter-spacing: 1px;
|
| 1005 |
+
|
| 1006 |
+
background:
|
| 1007 |
+
rgba(0,255,120,0.08);
|
| 1008 |
+
|
| 1009 |
+
color:
|
| 1010 |
+
#55ff9b;
|
| 1011 |
|
|
|
|
| 1012 |
}
|
| 1013 |
|
| 1014 |
+
|
| 1015 |
+
.pulse {
|
| 1016 |
+
|
| 1017 |
width: 8px;
|
| 1018 |
+
|
| 1019 |
height: 8px;
|
| 1020 |
|
| 1021 |
border-radius: 50%;
|
| 1022 |
|
| 1023 |
+
background:
|
| 1024 |
+
#55ff9b;
|
| 1025 |
|
| 1026 |
box-shadow:
|
| 1027 |
+
0 0 14px
|
| 1028 |
+
rgba(85,255,155,0.8);
|
| 1029 |
+
|
| 1030 |
}
|
| 1031 |
|
| 1032 |
+
|
| 1033 |
+
.cards {
|
| 1034 |
+
|
| 1035 |
display: grid;
|
| 1036 |
|
| 1037 |
grid-template-columns:
|
| 1038 |
repeat(
|
| 1039 |
auto-fit,
|
| 1040 |
+
minmax(
|
| 1041 |
+
150px,
|
| 1042 |
+
1fr
|
| 1043 |
+
)
|
| 1044 |
);
|
| 1045 |
|
| 1046 |
gap: 10px;
|
| 1047 |
+
|
| 1048 |
}
|
| 1049 |
|
|
|
|
|
|
|
| 1050 |
|
| 1051 |
+
.card {
|
| 1052 |
+
|
| 1053 |
+
padding: 17px;
|
| 1054 |
+
|
| 1055 |
+
border-radius: 16px;
|
| 1056 |
|
| 1057 |
border:
|
| 1058 |
1px solid
|
|
|
|
| 1060 |
|
| 1061 |
background:
|
| 1062 |
rgba(255,255,255,0.025);
|
| 1063 |
+
|
| 1064 |
}
|
| 1065 |
|
|
|
|
|
|
|
| 1066 |
|
| 1067 |
+
.small {
|
| 1068 |
+
|
| 1069 |
+
font-size: 10px;
|
| 1070 |
|
| 1071 |
font-weight: 700;
|
| 1072 |
|
| 1073 |
+
opacity: 0.45;
|
| 1074 |
+
|
| 1075 |
letter-spacing: 1px;
|
| 1076 |
+
|
| 1077 |
}
|
| 1078 |
|
| 1079 |
+
|
| 1080 |
+
.big {
|
| 1081 |
+
|
| 1082 |
+
margin-top: 7px;
|
| 1083 |
|
| 1084 |
font-size: 15px;
|
| 1085 |
|
| 1086 |
+
font-weight: 800;
|
| 1087 |
+
|
| 1088 |
}
|
| 1089 |
|
| 1090 |
+
|
| 1091 |
.online {
|
| 1092 |
+
|
| 1093 |
+
color:
|
| 1094 |
+
#55ff9b;
|
| 1095 |
+
|
| 1096 |
}
|
| 1097 |
|
| 1098 |
</style>
|
| 1099 |
"""
|
| 1100 |
|
| 1101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1102 |
# ============================================================
|
| 1103 |
+
# GRADIO UI
|
| 1104 |
# ============================================================
|
| 1105 |
|
| 1106 |
with gr.Blocks(
|
| 1107 |
title="Discord Bot Console"
|
| 1108 |
) as app:
|
| 1109 |
|
| 1110 |
+
gr.HTML(
|
| 1111 |
+
CUSTOM_HTML
|
| 1112 |
+
)
|
| 1113 |
|
| 1114 |
gr.Markdown(
|
| 1115 |
"## Live Console"
|
| 1116 |
)
|
| 1117 |
|
| 1118 |
logs = gr.Textbox(
|
| 1119 |
+
value=get_logs(),
|
| 1120 |
+
label=(
|
| 1121 |
+
"Log from when the service started "
|
| 1122 |
+
"to the live log"
|
| 1123 |
+
),
|
| 1124 |
+
lines=30,
|
| 1125 |
+
max_lines=50,
|
| 1126 |
+
interactive=False,
|
| 1127 |
+
autoscroll=True
|
| 1128 |
+
)
|
| 1129 |
+
|
| 1130 |
+
gr.Markdown(
|
| 1131 |
+
"## Service Information"
|
| 1132 |
+
)
|
| 1133 |
|
| 1134 |
+
status_box = gr.Textbox(
|
| 1135 |
+
value=get_status(),
|
| 1136 |
+
label="Bot Status",
|
| 1137 |
+
lines=8,
|
| 1138 |
+
interactive=False
|
| 1139 |
)
|
| 1140 |
|
| 1141 |
+
with gr.Row():
|
| 1142 |
+
|
| 1143 |
+
refresh_logs_button = gr.Button(
|
| 1144 |
+
"Refresh Logs"
|
| 1145 |
+
)
|
| 1146 |
+
|
| 1147 |
+
refresh_status_button = gr.Button(
|
| 1148 |
+
"Refresh Status"
|
| 1149 |
+
)
|
| 1150 |
+
|
| 1151 |
+
|
| 1152 |
+
# ========================================================
|
| 1153 |
+
# REFRESH LOGS
|
| 1154 |
+
# ========================================================
|
| 1155 |
+
|
| 1156 |
+
refresh_logs_button.click(
|
| 1157 |
+
fn=get_logs,
|
| 1158 |
outputs=logs
|
| 1159 |
)
|
| 1160 |
|
| 1161 |
+
|
| 1162 |
+
# ========================================================
|
| 1163 |
+
# REFRESH STATUS
|
| 1164 |
+
# ========================================================
|
| 1165 |
+
|
| 1166 |
+
refresh_status_button.click(
|
| 1167 |
+
fn=get_status,
|
| 1168 |
+
outputs=status_box
|
| 1169 |
+
)
|
| 1170 |
+
|
| 1171 |
+
|
| 1172 |
+
# ========================================================
|
| 1173 |
+
# LIVE LOG TIMER
|
| 1174 |
+
# ========================================================
|
| 1175 |
+
|
| 1176 |
+
log_timer = gr.Timer(
|
| 1177 |
value=1
|
| 1178 |
)
|
| 1179 |
|
| 1180 |
+
log_timer.tick(
|
| 1181 |
+
fn=get_logs,
|
| 1182 |
outputs=logs
|
| 1183 |
)
|
| 1184 |
|
| 1185 |
|
| 1186 |
+
# ========================================================
|
| 1187 |
+
# LIVE STATUS TIMER
|
| 1188 |
+
# ========================================================
|
| 1189 |
+
|
| 1190 |
+
status_timer = gr.Timer(
|
| 1191 |
+
value=2
|
| 1192 |
+
)
|
| 1193 |
+
|
| 1194 |
+
status_timer.tick(
|
| 1195 |
+
fn=get_status,
|
| 1196 |
+
outputs=status_box
|
| 1197 |
+
)
|
| 1198 |
+
|
| 1199 |
+
|
| 1200 |
# ============================================================
|
| 1201 |
+
# START DISCORD BOT
|
| 1202 |
# ============================================================
|
| 1203 |
|
| 1204 |
start_discord_bot()
|
|
|
|
| 1213 |
"Starting Gradio web interface..."
|
| 1214 |
)
|
| 1215 |
|
| 1216 |
+
add_log(
|
| 1217 |
+
"SYSTEM",
|
| 1218 |
+
"Service is ready"
|
| 1219 |
+
)
|
| 1220 |
+
|
| 1221 |
app.launch(
|
| 1222 |
server_name="0.0.0.0",
|
| 1223 |
server_port=7860,
|