diff --git a/index.js b/index.js index 50f3369..a4795f0 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,7 @@ const { createClient } = require('@supabase/supabase-js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, - GatewayIntentBits.GuildMessages, - GatewayIntentBits.MessageContent, + GatewayIntentBits.GuildMembers, ], }); @@ -28,112 +27,107 @@ const commands = [ .setName('nodeid') .setDescription('Your Node ID') .setRequired(true) - ), + ) + .setDefaultMemberPermissions('0') // Make command private + .setDMPermission(false), // Disable DM usage ]; // Register commands when bot starts client.once('ready', async () => { try { console.log('Started refreshing application (/) commands.'); - const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN); - await rest.put( - Routes.applicationCommands(client.user.id), - { body: commands }, - ); - - console.log('Successfully reloaded application (/) commands.'); - console.log(`Logged in as ${client.user.tag}!`); + // Register commands for the first guild the bot is in + const guild = client.guilds.cache.first(); + if (guild) { + await rest.put( + Routes.applicationGuildCommands(client.user.id, guild.id), + { body: commands }, + ); + console.log(`Bot is ready as ${client.user.tag} in guild ${guild.name}!`); + } } catch (error) { - console.error('Error refreshing commands:', error); + console.error('Error:', error); } }); // Handle slash commands client.on('interactionCreate', async interaction => { if (!interaction.isChatInputCommand()) return; + if (interaction.commandName !== 'node') return; - if (interaction.commandName === 'node') { - await handleNodeVerification(interaction); - } -}); - -async function handleNodeVerification(interaction) { try { - // Defer reply as the verification might take a moment - await interaction.deferReply(); - - const nodeId = interaction.options.getString('nodeid'); - - // Check if nodeId is valid format - if (!/^[a-zA-Z0-9-_]{1,64}$/.test(nodeId)) { - return await interaction.editReply({ - content: '❌ Invalid Node ID format. Please provide a valid Node ID.', + // Initial reply is ephemeral (only visible to the command user) + await interaction.deferReply({ ephemeral: true }); + + // Check bot permissions first + if (!interaction.guild.members.me.permissions.has('ManageRoles')) { + await interaction.editReply({ + content: '❌ Bot is missing permissions. Please give the bot "Manage Roles" permission and make sure its role is above the "Altruistic Mode" role.', ephemeral: true }); + return; } - - // Check if node exists in database and is active (within last 24 hours) - const { data: node, error } = await supabase + + const nodeId = interaction.options.getString('nodeid'); + + // Check database for node + const { data: node } = await supabase .from('node_records') .select('*') .eq('node_id', nodeId) - .gte('timestamp', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()) .single(); - if (error || !node) { - return await interaction.editReply({ - content: '❌ No active node found with this ID. Make sure your node is running and try again.', + if (!node) { + await interaction.editReply({ + content: '❌ No node found with this ID.', ephemeral: true }); + return; } - // Get the role + // Get and assign role const role = interaction.guild.roles.cache.find(r => r.name === 'Altruistic Mode'); - if (!role) { - return await interaction.editReply({ - content: '❌ Error: ALTRUISTIC MODE role not found in the server. Please contact an administrator.', + await interaction.editReply({ + content: '❌ Could not find the "Altruistic Mode" role.', + ephemeral: true + }); + return; + } + + // Check if bot's role is higher than the role it's trying to assign + if (interaction.guild.members.me.roles.highest.position <= role.position) { + await interaction.editReply({ + content: '❌ Bot\'s role must be higher than the "Altruistic Mode" role in the server settings.', + ephemeral: true + }); + return; + } + + // Try to add the role + try { + await interaction.member.roles.add(role); + await interaction.editReply({ + content: '✅ Role granted successfully!', + ephemeral: true + }); + } catch (roleError) { + console.error('Role assignment error:', roleError); + await interaction.editReply({ + content: '❌ Failed to assign role. Please check bot permissions.', ephemeral: true }); } - // Check if user already has the role - if (interaction.member.roles.cache.has(role.id)) { - return await interaction.editReply({ - content: '✨ You already have the ALTRUISTIC MODE role!', - ephemeral: true - }); - } - - // Add role to user - await interaction.member.roles.add(role); - - // Send success message - await interaction.editReply({ - content: `🎉 Congratulations! Your node has been verified and you've been granted the ALTRUISTIC MODE role!\n\n` + - `Node Details:\n` + - `• Node ID: ${node.node_id}\n` + - `• Version: ${node.version}\n` + - `• Peer Count: ${node.peer_count}\n` + - `• Last Active: ${new Date(node.timestamp).toLocaleString()}`, - ephemeral: true - }); - } catch (error) { - console.error('Error in node verification:', error); + console.error('Error:', error); await interaction.editReply({ - content: '❌ An error occurred while verifying your node. Please try again later.', + content: '❌ An error occurred.', ephemeral: true }); } -} - -// Error handling -client.on('error', error => { - console.error('Discord client error:', error); }); -// Login to Discord client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file