38550-vm/bot/register_commands.php
2026-02-18 05:42:57 +00:00

65 lines
2.0 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
function register_commands() {
$settings = db()->query("SELECT discord_token, discord_app_id FROM bot_settings LIMIT 1")->fetch();
if (!$settings || empty($settings['discord_token']) || empty($settings['discord_app_id'])) {
return "Error: Discord credentials not set in settings.";
}
$token = $settings['discord_token'];
$appId = $settings['discord_app_id'];
$url = "https://discord.com/api/v10/applications/$appId/commands";
$commands = [
[
"name" => "play",
"description" => "Play a song from a URL or search term",
"options" => [
[
"name" => "query",
"description" => "The song URL or name to search for",
"type" => 3, // STRING
"required" => true
]
]
],
[
"name" => "queue",
"description" => "Show the current music queue"
],
[
"name" => "skip",
"description" => "Skip the currently playing song"
],
[
"name" => "stop",
"description" => "Stop the music and clear the queue"
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($commands));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bot $token",
"Content-Type: application/json"
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status >= 200 && $status < 300) {
return "Successfully registered " . count($commands) . " slash commands.";
} else {
return "Failed to register commands. Status: $status. Response: $response";
}
}
if (php_sapi_name() === 'cli') {
echo register_commands() . PHP_EOL;
}