Modernize Discord bot to v14 and Node.js 22
Major upgrades and architectural improvements: - Upgrade Discord.js from v12 to v14.21.0 - Upgrade Node.js from 14 to 22 LTS - Switch to pnpm package manager - Complete rewrite with modern Discord API patterns New Features: - Hybrid command system: prefix commands + slash commands - /sfx slash command with autocomplete for sound discovery - Modern @discordjs/voice integration for audio - Improved voice connection management - Enhanced logging for SFX commands - Multi-stage Docker build for optimized images Technical Improvements: - Modular architecture with services and command handlers - Proper intent management for Discord gateway - Better error handling and logging - Hot-reload capability maintained - Environment variable support - Optimized Docker container with Alpine Linux Breaking Changes: - Moved main entry from index.js to src/index.js - Updated configuration structure for v14 compatibility - Replaced deprecated voice APIs with @discordjs/voice - Updated audio dependencies (opus, ffmpeg) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
113
src/services/sfxManager.js
Normal file
113
src/services/sfxManager.js
Normal file
@@ -0,0 +1,113 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
class SFXManager {
|
||||
constructor() {
|
||||
this.sfxPath = path.join(__dirname, '..', '..', 'sfx');
|
||||
this.sfxList = [];
|
||||
|
||||
// Load SFX list initially
|
||||
this.loadSFXList();
|
||||
|
||||
// Watch for changes
|
||||
this.watchSFXDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the list of available SFX files
|
||||
*/
|
||||
loadSFXList() {
|
||||
try {
|
||||
if (!fs.existsSync(this.sfxPath)) {
|
||||
console.log('SFX directory not found, creating...');
|
||||
fs.mkdirSync(this.sfxPath, { recursive: true });
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(this.sfxPath);
|
||||
this.sfxList = files
|
||||
.filter(file => file.endsWith('.mp3') || file.endsWith('.wav'))
|
||||
.map(file => {
|
||||
const ext = path.extname(file);
|
||||
return {
|
||||
name: file.replace(ext, ''),
|
||||
filename: file,
|
||||
path: path.join(this.sfxPath, file)
|
||||
};
|
||||
});
|
||||
|
||||
console.log(`Loaded ${this.sfxList.length} sound effects`);
|
||||
} catch (error) {
|
||||
console.error('Error loading SFX list:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch the SFX directory for changes
|
||||
*/
|
||||
watchSFXDirectory() {
|
||||
fs.watch(this.sfxPath, (eventType, filename) => {
|
||||
if (eventType === 'rename') {
|
||||
console.log('SFX directory changed, reloading...');
|
||||
this.loadSFXList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available SFX
|
||||
* @returns {Array} List of SFX objects
|
||||
*/
|
||||
getAllSFX() {
|
||||
return this.sfxList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SFX names for autocomplete
|
||||
* @returns {Array} List of SFX names
|
||||
*/
|
||||
getSFXNames() {
|
||||
return this.sfxList.map(sfx => sfx.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an SFX by name
|
||||
* @param {string} name
|
||||
* @returns {Object|undefined} SFX object or undefined
|
||||
*/
|
||||
findSFX(name) {
|
||||
return this.sfxList.find(sfx => sfx.name.toLowerCase() === name.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an SFX exists
|
||||
* @param {string} name
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasSFX(name) {
|
||||
return this.findSFX(name) !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path for an SFX
|
||||
* @param {string} name
|
||||
* @returns {string|null}
|
||||
*/
|
||||
getSFXPath(name) {
|
||||
const sfx = this.findSFX(name);
|
||||
return sfx ? sfx.path : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search SFX names (for autocomplete)
|
||||
* @param {string} query
|
||||
* @returns {Array} Matching SFX names
|
||||
*/
|
||||
searchSFX(query) {
|
||||
const lowerQuery = query.toLowerCase();
|
||||
return this.sfxList
|
||||
.filter(sfx => sfx.name.toLowerCase().includes(lowerQuery))
|
||||
.map(sfx => sfx.name);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SFXManager();
|
||||
Reference in New Issue
Block a user