const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const axios = require('axios'); const cheerio = require('cheerio'); module.exports = { data: new SlashCommandBuilder() .setName('lawcodes') .setDescription('Fetch a California law code section and display it in an embed.') .addStringOption(option => option.setName('code') .setDescription('The law code to look up (e.g., BPC, PEN, etc.).') .setRequired(true) .addChoices( { name: 'BPC', value: 'BPC' }, { name: 'CIV', value: 'CIV' }, { name: 'CCP', value: 'CCP' }, { name: 'COM', value: 'COM' }, { name: 'CORP', value: 'CORP' }, { name: 'EDC', value: 'EDC' }, { name: 'ELEC', value: 'ELEC' }, { name: 'EVID', value: 'EVID' }, { name: 'FAM', value: 'FAM' }, { name: 'FIN', value: 'FIN' }, { name: 'FGC', value: 'FGC' }, { name: 'GOV', value: 'GOV' }, { name: 'HNC', value: 'HNC' }, { name: 'HSC', value: 'HSC' }, { name: 'INS', value: 'INS' }, { name: 'LAB', value: 'LAB' }, { name: 'MVC', value: 'MVC' }, { name: 'PEN', value: 'PEN' }, { name: 'PRC', value: 'PRC' }, { name: 'PUC', value: 'PUC' }, { name: 'RTC', value: 'RTC' }, { name: 'SHC', value: 'SHC' }, { name: 'UIC', value: 'UIC' }, { name: 'VEH', value: 'VEH' }, { name: 'WIC', value: 'WIC' } ) ) .addStringOption(option => option.setName('section') .setDescription('The section number to look up.') .setRequired(true) ), async execute(interaction) { const code = interaction.options.getString('code'); const section = interaction.options.getString('section'); // Clean URL without spaces or bad syntax const url = `https://leginfo.legislature.ca.gov/faces/codes_displaySection.xhtml?sectionNum=${section}.&lawCode=${code}`; try { const response = await axios.get(url); const html = response.data; const $ = cheerio.load(html); // Try to find the main content area let content = ''; // Target the div with id="codeDisplayFrBody" which contains the law text const rawContent = $('#codeDisplayFrBody').text().trim(); if (rawContent && rawContent.length > 50) { // Success! We found the law content. content = rawContent; } else { // Fallback: Look for any div with substantial content $('div').each((i, el) => { const text = $(el).text().trim(); if (text.length > 200) { content = text; return false; // Break loop } }); } // If still no content, give meaningful error if (!content) { content = 'No law content was found for this section. It may not exist or the website structure has changed.'; } // Truncate to fit within Discord's limit const description = content.substring(0, 4096); // Build the embed const lawEmbed = new EmbedBuilder() .setColor(0x0099FF) .setTitle(`California ${code} ยง${section}`) .setURL(url) .setAuthor({ name: 'California Law Code', iconURL: interaction.client.user.displayAvatarURL(), url: 'https://leginfo.legislature.ca.gov/ ', }) .setDescription(description) .addFields( { name: '\u200B', value: '\u200B' }, // Empty field for spacing { name: 'Law Code', value: code, inline: true }, { name: 'Section', value: section, inline: true } ) .setTimestamp() .setFooter({ text: 'Gas Station Support', iconURL: interaction.client.user.displayAvatarURL(), }); await interaction.reply({ embeds: [lawEmbed] }); } catch (error) { console.error(`Error fetching law from ${url}:`, error.message); await interaction.reply({ content: 'An error occurred while fetching the law section. Please try again later.', ephemeral: true, }); } }, };