Code Part 1
Thu Jul 11 2024 10:27:13 GMT+0000 (Coordinated Universal Time)
Saved by @FRTZ
import { Client, GatewayIntentBits, Partials, PermissionsBitField, ChannelType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import dotenv from 'dotenv'; dotenv.config(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const prefix = process.env.DISCORD_PREFIX || '!'; const dataFilePath = path.join(__dirname, 'data.json'); console.log(`Data file path: ${dataFilePath}`); let data; let matches = {}; let matchCounter = 1; let playersELO = {}; let playerWarnings = {}; let playerStats = {}; let karma = {}; let queue = []; const maps = ['BW', 'SB', 'PORT', 'COMP', 'ANK', 'MEX', 'EE']; const saveMatchData = () => { const dataToSave = { matches, matchCounter, playersELO, playerWarnings, karma, playerStats }; fs.writeFileSync(dataFilePath, JSON.stringify(dataToSave, null, 2), 'utf-8'); }; const loadMatchData = () => { try { const fileContents = fs.readFileSync(dataFilePath, 'utf8'); const data = JSON.parse(fileContents); matches = data.matches || {}; matchCounter = data.matchCounter || 1; playersELO = data.playersELO || {}; playerWarnings = data.playerWarnings || {}; karma = data.karma || {}; playerStats = data.playerStats || {}; } catch (error) { console.error(`Error reading file at path: ${dataFilePath}`); console.error(error); } }; // Call loadMatchData at the start to initialize loadMatchData(); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions ], partials: [Partials.Channel, Partials.Message, Partials.Reaction] }); client.once('ready', async () => { console.log(`Bot is ready! Logged in as ${client.user.tag}`); await setupQueueMessage(); }); client.on('messageCreate', async (message) => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); try { switch (command) { case 'report': await reportCommand(message, args); break; case 'clearqueue': await clearQueueCommand(message); break; case 'resetelo': await resetEloCommand(message); break; case 'giveelo': await giveEloCommand(message, args); break; case 'setmatchlogchannel': await setMatchLogChannelCommand(message, args); break; case 'profile': await profileCommand(message, args); break; case 'lb': case 'leaderboard': await leaderboardCommand(message); break; case 'elo': await eloCommand(message, args); break; case 'pick': await pickCommand(message, args); break; case 'needsub': await needSubCommand(message, args); break; case 'replace': await replaceCommand(message, args); break; case 'pt': await ptCommand(message, args); break; case 'cooldown': await suspendUserCommand(message, args); break; case 'removecd': await unSuspendUserCommand(message, args); break; case 'cancel': await cancelMatchCommand(message, args); break; case 'warn': await warnCommand(message, args); break; case 'warns': await warnsCommand(message, args); break; case 'warningsreset': await warningResetCommand(message); break; } } catch (error) { console.error(`Error executing command ${command}:`, error); message.reply('There was an error executing that command.'); } }); client.on('interactionCreate', async interaction => { try { if (interaction.isButton()) { await handleButtonInteraction(interaction); } else if (interaction.isCommand()) { const command = interaction.commandName; switch (command) { case 'setupreaction': await setupQueueMessage(interaction); break; case 'eloreset': await eloresetCommand(interaction); break; case 'warningreset': await warningresetCommand(interaction); break; case 'leaderboard': await leaderboardCommand(interaction); break; default: await interaction.reply({ content: 'Unknown command interaction.', ephemeral: true }); break; } } } catch (error) { console.error('Error handling interaction:', error); if (interaction.isCommand() || interaction.isButton()) { await interaction.reply({ content: 'There was an error handling this interaction.', ephemeral: true }); } } }); const setupQueueMessage = async () => { try { const channel = await client.channels.fetch(process.env.QUEUE_CHANNEL_ID); const embed = new EmbedBuilder() .setColor('#00ff26') .setTitle('🎮 CSL West Competitive Queue 🎮') .setDescription('Use the buttons below to join or leave the queue.\n\n**Current Queue:**\nNo one in the queue (0/10)') .setFooter({ text: 'Powered by Maverick', iconURL: 'https://i.imgur.com/wTF3RuJ.png' }); const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('join_queue') .setLabel('Join Queue') .setStyle(ButtonStyle.Success) .setEmoji('✅'), new ButtonBuilder() .setCustomId('leave_queue') .setLabel('Leave Queue') .setStyle(ButtonStyle.Danger) .setEmoji('❌') ); await channel.send({ embeds: [embed], components: [row] }); } catch (error) { console.error('Error setting up queue message:', error); } }; const joinQueue = async (interaction) => { try { await interaction.deferReply({ ephemeral: true }); const user = interaction.user; const member = await interaction.guild.members.fetch(user.id); if (!queue.includes(user.id) && member.roles.cache.some(role => ['Pug Approved', 'Admin', 'Score Reporter'].includes(role.name))) { queue.push(user.id); await updateQueueMessage(); if (queue.length === 10) { await createMatch(interaction.guild); } await interaction.editReply({ content: 'You have joined the queue!' }); } else { await interaction.editReply({ content: 'You are already in the queue or do not have permission to join the queue.' }); } } catch (error) { console.error('Error joining queue:', error); if (!interaction.replied) { await interaction.editReply({ content: 'There was an error joining the queue.' }); } } }; const leaveQueue = async (interaction) => { try { await interaction.deferReply({ ephemeral: true }); const user = interaction.user; const index = queue.indexOf(user.id); if (index > -1) { queue.splice(index, 1); await updateQueueMessage(); await interaction.editReply({ content: 'You have left the queue!' }); } else { await interaction.editReply({ content: 'You are not in the queue!' }); } } catch (error) { console.error('Error leaving queue:', error); if (!interaction.replied) { await interaction.editReply({ content: 'There was an error leaving the queue.' }); } } }; const updateQueueMessage = async () => { try { const channel = await client.channels.fetch(process.env.QUEUE_CHANNEL_ID); const messages = await channel.messages.fetch({ limit: 10 }); const queueMessage = messages.find(msg => msg.embeds[0]?.title === '🎮 CSL West Competitive Queue 🎮'); if (queueMessage) { const description = queue.length > 0 ? `${queue.map(id => `<@${id}>`).join('\n')}\n\n**(${queue.length}/10)**` : 'No one in the queue (0/10)'; const embed = new EmbedBuilder() .setColor('#00ff26') .setTitle('🎮 CSL West Competitive Queue 🎮') .setDescription(`Use the buttons below to join or leave the queue.\n\n**Current Queue:**\n${description}`) .setFooter({ text: 'Powered by Maverick', iconURL: 'https://i.imgur.com/wTF3RuJ.png' }); const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('join_queue') .setLabel('Join Queue') .setStyle(ButtonStyle.Success) .setEmoji('✅'), new ButtonBuilder() .setCustomId('leave_queue') .setLabel('Leave Queue') .setStyle(ButtonStyle.Danger) .setEmoji('❌') ); await queueMessage.edit({ embeds: [embed], components: [row] }); } } catch (error) { console.error('Error updating queue message:', error); } }; const createMatch = async (guild) => { try { const matchCategory = guild.channels.cache.get(process.env.MATCH_CATEGORY_ID); const team1Voice = await guild.channels.create({ name: `Match ${matchCounter} - Team 1`, type: ChannelType.GuildVoice, parent: matchCategory, userLimit: 6, permissionOverwrites: [ { id: guild.roles.everyone.id, allow: [PermissionsBitField.Flags.Connect], }, ], }); const team2Voice = await guild.channels.create({ name: `Match ${matchCounter} - Team 2`, type: ChannelType.GuildVoice, parent: matchCategory, userLimit: 6, permissionOverwrites: [ { id: guild.roles.everyone.id, allow: [PermissionsBitField.Flags.Connect], }, ], }); const matchText = await guild.channels.create({ name: `match-${matchCounter}`, type: ChannelType.GuildText, parent: matchCategory, permissionOverwrites: [ { id: guild.roles.everyone.id, allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages], }, ], }); const team1Captain = queue.shift(); const team2Captain = queue.shift(); const remainingPlayers = queue.splice(0, 8); const randomMap = maps[Math.floor(Math.random() * maps.length)]; matches[matchCounter] = { team1: [team1Captain], team2: [team2Captain], remaining: remainingPlayers, pickingOrder: ['team1', 'team2', 'team2', 'team1', 'team1', 'team2', 'team1'], currentPick: 0, mapVotes: {}, chosenMap: randomMap, textChannelId: matchText.id }; saveMatchData(); console.log(`Match ${matchCounter} created with map ${randomMap}`); await updateQueueMessage(); await updateMatchMessage(matchCounter); setTimeout(async () => { if (!matches[matchCounter]) return; const mostVotedMap = Object.entries(matches[matchCounter].mapVotes) .reduce((max, entry) => entry[1] > max[1] ? entry : max, ['', 0])[0]; if (mostVotedMap) { matches[matchCounter].chosenMap = mostVotedMap; } await updateMatchMessage(matchCounter); }, 3 * 60 * 1000); matchCounter++; } catch (error) { console.error('Error creating match:', error); } }; const updateMatchMessage = async (matchNumber) => { try { const match = matches[matchNumber]; const matchText = client.channels.cache.get(match.textChannelId); if (!matchText) return; const team1 = match.team1.map(id => `<@${id}>`).join('\n') || 'No players yet'; const team2 = match.team2.map(id => `<@${id}>`).join('\n') || 'No players yet'; const embed = new EmbedBuilder() .setTitle(`🎮 Match ${matchNumber} 🎮`) .setColor('#ff4500') .setDescription('**Match Details**') .addFields( { name: '🏆 Team 1 Captain', value: `<@${match.team1[0]}>`, inline: true }, { name: '🏆 Team 2 Captain', value: `<@${match.team2[0]}>`, inline: true }, { name: '\u200B', value: '\u200B', inline: true }, { name: '👥 Team 1', value: team1, inline: true }, { name: '👥 Team 2', value: team2, inline: true }, { name: '🗺️ Map', value: `**${match.chosenMap}**`, inline: true } ) .setThumbnail('https://i.imgur.com/xfe96CL.png') .setImage('https://i.imgur.com/jyuglDa.png') .setFooter({ text: 'Powered by Maverick', iconURL: 'https://i.imgur.com/wTF3RuJ.png' }) .setTimestamp(); const playerPickRows = []; for (let i = 0; i < match.remaining.length; i += 5) { const row = new ActionRowBuilder() .addComponents( await Promise.all( match.remaining.slice(i, i + 5).map(async playerId => { const member = await client.guilds.cache.get(matchText.guild.id).members.fetch(playerId); const nickname = member.nickname || member.user.username; return new ButtonBuilder() .setCustomId(`pick_${matchNumber}_${playerId}`) .setLabel(`Pick ${nickname}`) .setStyle(ButtonStyle.Primary); }) ) ); playerPickRows.push(row); } const mapVoteRows = []; for (let i = 0; i < maps.length; i += 5) { const row = new ActionRowBuilder() .addComponents( maps.slice(i, i + 5).map(map => new ButtonBuilder() .setCustomId(`map_vote_${matchNumber}_${map}`) .setLabel(`${map} (${match.mapVotes[map] || 0})`) .setStyle(ButtonStyle.Secondary) ) ); mapVoteRows.push(row); } const messages = await matchText.messages.fetch({ limit: 10 }); const matchMessage = messages.find(msg => msg.embeds[0]?.title === `🎮 Match ${matchNumber} 🎮`); if (matchMessage) { try { await matchMessage.delete(); } catch (error) { console.error(`Failed to delete message: ${error.message}`); } } await matchText.send({ embeds: [embed], components: [...playerPickRows, ...mapVoteRows] }); } catch (error) { console.error('Error updating match message:', error); } }; const handleButtonInteraction = async (interaction) => { try { const customId = interaction.customId; if (customId.startsWith('map_vote_')) { await voteMap(interaction); } else if (customId.startsWith('join_queue')) { await joinQueue(interaction); } else if (customId.startsWith('leave_queue')) { await leaveQueue(interaction); } else if (customId.startsWith('pick_')) { await handlePick(interaction); } else { await interaction.reply({ content: 'Unknown button interaction.', ephemeral: true }); } } catch (error) { console.error('Error handling button interaction:', error); await interaction.reply({ content: 'There was an error handling this button interaction.', ephemeral: true }); } }; const handlePick = async (interaction) => { try { const [_, matchNumber, playerId] = interaction.customId.split('_'); const match = matches[matchNumber]; if (!match) { await interaction.reply({ content: `Match ${matchNumber} not found.`, ephemeral: true }); return; } const user = interaction.user; const currentPickTeam = match.pickingOrder[match.currentPick]; const isCaptain = (user.id === match.team1[0] && currentPickTeam === 'team1') || (user.id === match.team2[0] && currentPickTeam === 'team2'); if (!isCaptain) { await interaction.reply({ content: 'Only the respective captain can pick for their team on their turn.', ephemeral: true }); return; } if (!match.remaining.includes(playerId)) { await interaction.reply({ content: 'Invalid player ID for picking.', ephemeral: true }); return; } const team = match.pickingOrder[match.currentPick]; match[team].push(playerId); match.remaining = match.remaining.filter(id => id !== playerId); match.currentPick++; if (match.currentPick === match.pickingOrder.length && match.remaining.length === 1) { match.team2.push(match.remaining[0]); match.remaining = []; } await updateMatchMessage(matchNumber); await interaction.reply({ content: `Player <@${playerId}> has been picked for ${team === 'team1' ? 'Team 1' : 'Team 2'}.`, ephemeral: true }); } catch (error) { console.error('Error handling pick:', error); await interaction.reply({ content: 'There was an error handling the pick.', ephemeral: true }); } }; const voteMap = async (interaction) => { try { const [_, matchNumber, map] = interaction.customId.split('_'); const userId = interaction.user.id; if (!matchNumber || isNaN(matchNumber) || matchNumber < 1) { console.error(`Invalid matchNumber: ${matchNumber}`); await interaction.reply({ content: 'Invalid match number.', ephemeral: true }); return; } const match = matches[matchNumber]; if (!match) { console.error(`Match ${matchNumber} not found.`); await interaction.reply({ content: `Match ${matchNumber} not found.`, ephemeral: true }); return; } if (!match.mapVotes) match.mapVotes = {}; if (match.mapVotes[map] && match.mapVotes[map].includes(userId)) { await interaction.reply({ content: 'You have already voted for this map.', ephemeral: true }); return; } for (const m in match.mapVotes) { match.mapVotes[m] = match.mapVotes[m].filter(id => id !== userId); } if (!match.mapVotes[map]) match.mapVotes[map] = []; match.mapVotes[map].push(userId); const mostVotedMap = Object.entries(match.mapVotes).reduce((max, entry) => entry[1].length > max[1].length ? entry : max, ['', []])[0]; match.chosenMap = mostVotedMap; await interaction.reply({ content: `Your vote for ${map} has been recorded!`, ephemeral: true }); await updateMatchMessage(matchNumber); } catch (error) { console.error('Error voting map:', error); await interaction.reply({ content: 'There was an error voting for the map.', ephemeral: true }); } }; const findUserMatch = (userId) => { for (const [id, match] of Object.entries(matches)) { if (match.team1.includes(userId) || match.team2.includes(userId) || match.remaining.includes(userId)) { return id; } } return null; }; const reportCommand = async (message, args) => { const roleIds = ['1075756565023445022', '1258877265870192673']; // Array of role IDs const hasAdminPermission = message.member.permissions.has(PermissionsBitField.Flags.Administrator); const hasRequiredRole = roleIds.some(roleId => message.member.roles.cache.has(roleId)); const hasPermission = hasAdminPermission || hasRequiredRole; // Check for permissions let matchId = Object.keys(matches).find(id => matches[id].textChannelId === message.channel.id); if (!matchId && hasPermission) { matchId = args[0]; } if (!matchId || !matches[matchId]) { return message.reply('Invalid match ID or no active match found.'); } try { const winningTeamNumber = hasPermission ? args[1] : args[0]; const match = matches[matchId]; const team1 = match.team1; const team2 = match.team2; let winningTeam, losingTeam; if (winningTeamNumber === '1') { winningTeam = team1; losingTeam = team2; updateElo(team1, team2); updateStats(team1, team2, match.chosenMap, true); } else if (winningTeamNumber === '2') { winningTeam = team2; losingTeam = team1; updateElo(team2, team1); updateStats(team2, team1, match.chosenMap, true); } else { return message.reply('Invalid winning team.'); } await message.reply(`Match ${matchId} has been reported and the channels will be deleted.`); const guild = message.guild; const matchCategory = await guild.channels.fetch(process.env.MATCH_CATEGORY_ID); const team1Voice = matchCategory.children.cache.find(c => c.name === `Match ${matchId} - Team 1`); const team2Voice = matchCategory.children.cache.find(c => c.name === `Match ${matchId} - Team 2`); const matchText = matchCategory.children.cache.find(c => c.name === `match-${matchId}`); if (team1Voice) await team1Voice.delete(); if (team2Voice) await team2Voice.delete(); if (matchText) await matchText.delete(); delete matches[matchId]; saveMatchData(); const reporter = message.author; logMatch(matchId, winningTeam, losingTeam, `Team ${winningTeamNumber}`, reporter); } catch (error) { console.error('Error reporting match:', error); message.reply('There was an error reporting the match.'); } }; const updateStats = (winningTeam, losingTeam, map, isWin) => { [...winningTeam, ...losingTeam].forEach(player => { if (!playerStats[player]) playerStats[player] = { BW: { wins: 0, losses: 0 }, PORT: { wins: 0, losses: 0 }, ANK: { wins: 0, losses: 0 }, COMP: { wins: 0, losses: 0 }, SB: { wins: 0, losses: 0 }, MEX: { wins: 0, losses: 0 }, EE: { wins: 0, losses: 0 } }; if (winningTeam.includes(player)) { playerStats[player][map].wins++; } else { playerStats[player][map].losses++; } }); saveData(); }; const clearQueueCommand = async (message) => { if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) { return message.reply('You do not have permission to use this command.'); } try { queue = []; await updateQueueMessage(); message.reply('The queue has been cleared.'); } catch (error) { console.error('Error clearing queue:', error); message.reply('There was an error clearing the queue.'); } }; const resetEloCommand = async (message) => { if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) { return message.reply('You do not have permission to use this command.'); } try { playersELO = {}; saveData(); message.reply('All ELO has been reset.'); } catch (error) { console.error('Error resetting ELO:', error); message.reply('There was an error resetting the ELO.'); } }; const warningResetCommand = async (message) => { if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) { return message.reply('You do not have permission to use this command.'); } try { playerWarnings = {}; saveData(); message.reply('All warnings have been reset.'); } catch (error) { console.error('Error resetting warnings:', error); message.reply('There was an error resetting the warnings.'); } }; const setMatchLogChannelCommand = async (message, args) => { if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) { return message.reply('You do not have permission to use this command.'); } try { const channelId = args[0]; process.env.MATCH_LOG_CHANNEL_ID = channelId; message.reply('Match log channel set.'); } catch (error) { console.error('Error setting match log channel:', error); message.reply('There was an error setting the match log channel.'); } }; const giveEloCommand = async (message, args) => { if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) { return message.reply('You do not have permission to use this command.'); } try { const userId = args[0].replace('<@', '').replace('>', ''); const eloChange = parseInt(args[1], 10); if (!playersELO[userId]) playersELO[userId] = 0; playersELO[userId] += eloChange; saveData(); message.reply(`ELO for <@${userId}> has been changed by ${eloChange}. New ELO: ${playersELO[userId]}`); } catch (error) { console.error('Error giving ELO:', error); message.reply('There was an error changing the ELO.'); } };
Comments