返回提交历史
Modified
projects/discord-bot/bot.py
+83
-1
XFEstudio/gpt4free
Update bot
d527a280
代码差异
1 个文件
+83
-1
@@ -366,13 +366,95 @@ async def models(interaction: discord.Interaction):
366
366
)
367
367
return
368
368
if isinstance(available, dict):
369
lines = [f"• `{n.get('label', k)}` ({n.get('requests', 0)})" for k, n in available.items() if not "requests" in n or n.get("requests") >= 5]
369
lines = [f"• {n.get('owned_by', '') } `{n.get('model', k)}` ({n.get('requests', 0)})" for k, n in available.items() if not "requests" in n or n.get("requests") >= 5]
370
370
else:
371
371
lines = [f"• `{n}`" for n in available]
372
372
msg = "**Available models:**\n" + "\n".join(lines)
373
373
await interaction.followup.send(_truncate(msg, 1900), ephemeral=True)
374
374
375
375
376
@bot.tree.command(
377
name="searchmodel",
378
description="Search available models (best effort) by keyword.",
379
)
380
@app_commands.describe(
381
query="Keyword to search in model names",
382
limit="Max matches to return",
383
)
384
async def searchmodel(
385
interaction: discord.Interaction,
386
query: str,
387
limit: Optional[int] = 10,
388
):
389
await interaction.response.defer(thinking=True, ephemeral=True)
390
391
query = (query or "").strip().lower()
392
if not query:
393
await interaction.followup.send("❌ Provide a non-empty `query`.", ephemeral=True)
394
return
395
396
try:
397
available = client.models.get_all()
398
except Exception as e:
399
log.exception(e)
400
await interaction.followup.send(
401
"⚠️ Unable to fetch a model catalog from the current provider. "
402
"Use `/setmodel` with a model id that works for your provider.",
403
ephemeral=True,
404
)
405
return
406
407
try:
408
limit_val = int(limit) if limit is not None else 10
409
except (TypeError, ValueError):
410
limit_val = 10
411
limit_val = max(1, min(limit_val, 30))
412
413
matches: List[str] = []
414
if isinstance(available, dict):
415
items = list(available.items())
416
# Try to match against common fields in the dict payload.
417
for k, n in items:
418
text = " ".join(
419
str(x).lower()
420
for x in (
421
n.get("model", k),
422
n.get("owned_by", ""),
423
n.get("id", ""),
424
n.get("object", ""),
425
)
426
if x is not None
427
)
428
if query in text:
429
model_name = n.get("model", k)
430
owned_by = n.get("owned_by", "") or ""
431
reqs = n.get("requests", 0)
432
matches.append(
433
f"• `{model_name}`"
434
+ (f" ({owned_by})" if owned_by else "")
435
+ (f" [{reqs}]" if isinstance(reqs, int) else "")
436
)
437
else:
438
# If provider returns a list-like model catalog.
439
for n in available:
440
s = str(n).lower()
441
if query in s:
442
matches.append(f"• `{n}`")
443
444
if not matches:
445
await interaction.followup.send(
446
f"No model matches found for `{query}`.",
447
ephemeral=True,
448
)
449
return
450
451
shown = matches[:limit_val]
452
msg = f"**Matches for `{query}`:**\n" + "\n".join(shown)
453
if len(matches) > limit_val:
454
msg += f"\n…(showing first {limit_val} of {len(matches)})"
455
await interaction.followup.send(_truncate(msg, 1900), ephemeral=True)
456
457
376
458
@bot.tree.command(name="image", description="Generate an image from a prompt.")
377
459
@app_commands.describe(
378
460
prompt="What to generate",