Upgrade tip command to allow multiple recipients
This commit is contained in:
@@ -16,5 +16,6 @@ return [
|
||||
|
||||
'MIN_TIP_AMOUNT' => 0.2,
|
||||
'MIN_WITHDRAWAL_AMOUNT' => 0.4,
|
||||
'WITHDRAWAL_FEE' => 0.1
|
||||
'WITHDRAWAL_FEE' => 0.1,
|
||||
'MAX_MULTI_TIPS' => 5
|
||||
];
|
||||
|
||||
@@ -101,22 +101,50 @@ class SalviumTipBotCommands {
|
||||
|
||||
|
||||
private function cmd_tip(array $args, array $ctx): string {
|
||||
if (count($args) < 3) return "Usage: /tip <username> <amount>";
|
||||
if (count($args) < 3) {
|
||||
return "Usage: /tip <user1> [user2 ...] <amount>";
|
||||
}
|
||||
|
||||
list(, $targetUsername, $amount) = $args;
|
||||
$amount = (float)$amount;
|
||||
$rawAmount = $args[count($args) - 1];
|
||||
$amount = (float)$rawAmount;
|
||||
|
||||
if (!is_numeric($rawAmount) || $amount <= 0) {
|
||||
return "Invalid tip amount.";
|
||||
}
|
||||
|
||||
if ($amount < $this->config['MIN_TIP_AMOUNT']) {
|
||||
return "Tip {$amount} for {$targetUsername} too small. Minimum tip is {$this->config['MIN_TIP_AMOUNT']} SAL.";
|
||||
return "Each tip must be at least {$this->config['MIN_TIP_AMOUNT']} SAL.";
|
||||
}
|
||||
|
||||
|
||||
$usernames = array_filter(array_slice($args, 1, -1), function($u) {
|
||||
$u = trim($u);
|
||||
return $u !== '' && $u !== '@';
|
||||
});
|
||||
|
||||
$maxRecipients = $this->config['MAX_MULTI_TIPS'] ?? 1;
|
||||
|
||||
if (count($usernames) > $maxRecipients) {
|
||||
return "You can tip up to {$maxRecipients} users at once.";
|
||||
}
|
||||
|
||||
$sender = $this->db->getUserByTelegramId($ctx['user_id']);
|
||||
if (!$sender || $sender['tip_balance'] < $amount) {
|
||||
return "Insufficient funds or invalid sender.";
|
||||
$total = $amount * count($usernames);
|
||||
|
||||
if (!$sender || $sender['tip_balance'] < $total) {
|
||||
return "Insufficient funds. You need at least {$total} SAL to tip these users.";
|
||||
}
|
||||
|
||||
$cleanUsername = ltrim($targetUsername, '@');
|
||||
$successful = [];
|
||||
|
||||
foreach ($usernames as $targetUsername) {
|
||||
$cleanUsername = trim(ltrim($targetUsername, '@'));
|
||||
|
||||
if ($cleanUsername === '' || !preg_match('/^[a-zA-Z0-9_]{5,32}$/', $cleanUsername)) {
|
||||
continue; // skip invalid usernames
|
||||
}
|
||||
|
||||
try {
|
||||
$recipient = $this->db->ensureUserExists(
|
||||
0,
|
||||
$cleanUsername,
|
||||
@@ -124,12 +152,11 @@ class SalviumTipBotCommands {
|
||||
true
|
||||
);
|
||||
|
||||
if (!$recipient) {
|
||||
return "Failed to ensure recipient exists.";
|
||||
}
|
||||
if (!$recipient) continue;
|
||||
|
||||
$this->db->updateUserTipBalance($sender['id'], $amount, 'subtract');
|
||||
$this->db->addTip($sender['id'], $recipient['id'], $amount, $ctx['chat_id']);
|
||||
$successful[] = $cleanUsername;
|
||||
|
||||
if (!empty($recipient['telegram_user_id']) && $recipient['telegram_user_id'] > 0 && $recipient['telegram_user_id'] !== (100_000 + (crc32($cleanUsername) % 900_000))) {
|
||||
sendMessage($recipient['telegram_user_id'], "You received a tip of {$amount} SAL! Use /balance to check.");
|
||||
@@ -142,8 +169,18 @@ class SalviumTipBotCommands {
|
||||
);
|
||||
}
|
||||
|
||||
return "Tipped {$targetUsername} {$amount} SAL successfully!";
|
||||
} catch (Throwable $e) {
|
||||
// Silently skip
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($successful)) {
|
||||
return "Tip failed — no valid recipients.";
|
||||
}
|
||||
|
||||
return "Tipped " . implode(', ', $successful) . " {$amount} SAL each!";
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function cmd_claim(array $args, array $ctx): string {
|
||||
|
||||
@@ -127,6 +127,11 @@ class SalviumTipBotDB {
|
||||
callable $subaddressGenerator,
|
||||
bool $allowSynthetic = false
|
||||
): array {
|
||||
|
||||
if (!$username || trim($username) === '' || $telegramId === 0 && !$allowSynthetic) {
|
||||
throw new RuntimeException("Invalid username or telegram ID.");
|
||||
}
|
||||
|
||||
// 1. Try exact match by Telegram ID
|
||||
$user = $this->getUserByTelegramId($telegramId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user