Skip to content

Instantly share code, notes, and snippets.

@adriangalilea
Created April 1, 2025 21:13
Show Gist options
  • Save adriangalilea/cbe0101c97ca8190424e1900933853a9 to your computer and use it in GitHub Desktop.
Save adriangalilea/cbe0101c97ca8190424e1900933853a9 to your computer and use it in GitHub Desktop.
Nuke all free-storage grammyjs data
#!/usr/bin/env ts-node
import { freeStorage } from '@grammyjs/storage-free';
import { config } from '../src/config/config';
import readline from 'readline';
const storage = freeStorage(config.BOT_TOKEN);
// Create readline interface for user confirmation
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function listAllKeys() {
console.log('🔍 Attempting to list all stored keys...');
const data = await storage.readAllKeys();
const result =
typeof data === 'object' &&
data &&
'keys' in data &&
Array.isArray(data.keys) &&
data.keys.every(k => typeof k === 'string')
? data.keys
: [];
return result;
}
function askForConfirmation(message: string): Promise<boolean> {
return new Promise(resolve => {
rl.question(`${message} (y/N): `, answer => {
resolve(answer.toLowerCase() === 'y');
});
});
}
/**
* NUCLEAR: Completely wipes all session data with multiple approaches
* This will erase EVERYTHING without mercy
*/
async function nukeAllData(): Promise<void> {
// Allow console logs as this is a debug utility script
console.log('☢️ NUCLEAR OPTION: About to COMPLETELY DESTROY all session data');
try {
// 1. List all keys from all storages
const keys = await listAllKeys();
// Display all found keys
if (keys.length > 0) {
console.log('\n📋 Found keys in storage:');
keys.forEach((key, index) => {
console.log(` 📁 ${index + 1}. ${key}`);
});
} else {
console.log('\n⚠️ Could not identify specific keys in any storage.');
console.log(' Will attempt to delete using patterns and direct database access.');
}
// Ask for confirmation
console.log('\n⚠️ WARNING: This operation cannot be undone!');
const confirmDelete = await askForConfirmation(
'Are you absolutely sure you want to delete ALL data?'
);
if (!confirmDelete) {
console.log('❌ Operation cancelled by user');
rl.close();
return;
}
console.log('\n💥 CONFIRMED! Proceeding with complete data destruction...\n');
// 3. Process each storage instance
for (const key of keys) {
console.log(`\n🧨 Clearing ${key}...`);
// First delete any found keys
try {
await storage.delete(key);
console.log(` ✅ Deleted key: ${key}`);
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);
console.log(` ❌ Failed to delete key ${key}: ${errorMessage}`);
}
}
} catch (error) {
console.error('Failed to nuke data:', error);
process.exit(1);
}
}
// Run if this file is executed directly
if (require.main === module) {
nukeAllData()
.then(() => {
console.log('💥 DATA COMPLETELY NUKED. Your bot will start with a clean slate.');
process.exit(0);
})
.catch(error => {
console.error('Failed to nuke data:', error);
process.exit(1);
});
}
// Export for programmatic use
export { nukeAllData };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment