|
| 1 | +const { |
| 2 | + Client, |
| 3 | + SlashCommandBuilder, |
| 4 | + GatewayIntentBits, |
| 5 | + EmbedBuilder, |
| 6 | +} = require("discord.js"); |
| 7 | +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); |
| 8 | +const fetch = (...args) => |
| 9 | + import("node-fetch").then(({ default: fetch }) => fetch(...args)); |
| 10 | +const iccAPI = process.env.ICC_API; |
| 11 | + |
| 12 | +module.exports = { |
| 13 | + data: new SlashCommandBuilder() |
| 14 | + .setName("score") |
| 15 | + .setDescription("Get Live Score") |
| 16 | + .addStringOption((option) => |
| 17 | + option |
| 18 | + .setName("matchid") |
| 19 | + .setDescription("Enter the match id") |
| 20 | + .setRequired(true) |
| 21 | + ), |
| 22 | + async execute(interaction) { |
| 23 | + const matchid = interaction.options.getString("matchid"); |
| 24 | + const url = `${iccAPI}/fixtures/${matchid}/scoring`; |
| 25 | + |
| 26 | + const getScore = await fetch(url, { |
| 27 | + headers: { |
| 28 | + accept: "*/*", |
| 29 | + "accept-language": "en-US,en;q=0.9", |
| 30 | + account: "ICC", |
| 31 | + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", |
| 32 | + "sec-ch-ua": |
| 33 | + '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"', |
| 34 | + "sec-ch-ua-mobile": "?0", |
| 35 | + "sec-ch-ua-platform": '"Windows"', |
| 36 | + "sec-fetch-dest": "empty", |
| 37 | + "sec-fetch-mode": "cors", |
| 38 | + "sec-fetch-site": "cross-site", |
| 39 | + }, |
| 40 | + referrer: "https://www.icc-cricket.com/", |
| 41 | + referrerPolicy: "strict-origin-when-cross-origin", |
| 42 | + body: null, |
| 43 | + method: "GET", |
| 44 | + mode: "cors", |
| 45 | + credentials: "omit", |
| 46 | + }); |
| 47 | + const scoreData = await getScore.json(); |
| 48 | + |
| 49 | + const innings = Object.keys(scoreData.innings).length; |
| 50 | + |
| 51 | + const { |
| 52 | + runs, |
| 53 | + wickets, |
| 54 | + tournamentLabel, |
| 55 | + over, |
| 56 | + facingBatter, |
| 57 | + nonFacingBatter, |
| 58 | + bowler, |
| 59 | + runRate, |
| 60 | + battingTeam, |
| 61 | + bowlingTeam, |
| 62 | + } = { |
| 63 | + runs: scoreData.innings[innings - 1].scorecard.runs, |
| 64 | + wickets: scoreData.innings[innings - 1].scorecard.wkts, |
| 65 | + tournamentLabel: scoreData.matchInfo.tournamentLabel, |
| 66 | + over: scoreData.innings[innings - 1].overProgress, |
| 67 | + facingBatter: scoreData.currentState.facingBatsman, |
| 68 | + nonFacingBatter: scoreData.currentState.nonFacingBatsman, |
| 69 | + bowler: scoreData.currentState.currentBowler, |
| 70 | + runRate: scoreData.innings[innings - 1].runRate, |
| 71 | + battingTeam: scoreData.innings[innings - 1].battingTeamId, |
| 72 | + bowlingTeam: scoreData.innings[innings - 1].bowlingTeamId, |
| 73 | + }; |
| 74 | + // Batsman and Bowler Name and score |
| 75 | + const battingStat = scoreData.innings[innings - 1].scorecard.battingStats; |
| 76 | + const bowlingStat = scoreData.innings[innings - 1].scorecard.bowlingStats; |
| 77 | + const facingBMScore = battingStat.find((o) => o.playerId === facingBatter); |
| 78 | + const nonFacingBMScore = battingStat.find( |
| 79 | + (o) => o.playerId === nonFacingBatter |
| 80 | + ); |
| 81 | + const bowlerScore = bowlingStat.find((o) => o.playerId === bowler); |
| 82 | + |
| 83 | + const { fr, fb, fsr, nfr, nfb, nfsr, br, bo, bw, be } = { |
| 84 | + fr: facingBMScore.r, |
| 85 | + fb: facingBMScore.b, |
| 86 | + fsr: facingBMScore.sr, |
| 87 | + nfr: nonFacingBMScore.r, |
| 88 | + nfb: nonFacingBMScore.b, |
| 89 | + nfsr: nonFacingBMScore.sr, |
| 90 | + br: bowlerScore.r, |
| 91 | + bo: bowlerScore.ov, |
| 92 | + bw: bowlerScore.w, |
| 93 | + be: bowlerScore.e, |
| 94 | + }; |
| 95 | + |
| 96 | + const fBatUrl = `${iccAPI}/players/${facingBatter}/`; |
| 97 | + const nfBatUrl = `${iccAPI}/players/${nonFacingBatter}/`; |
| 98 | + const bowlerUrl = `${iccAPI}/players/${bowler}/`; |
| 99 | + const batTeamUrl = `${iccAPI}/teams/${battingTeam}/`; |
| 100 | + const bowlTeamUrl = `${iccAPI}/teams/${bowlingTeam}/`; |
| 101 | + |
| 102 | + const getfacingBatter = await fetch(fBatUrl); |
| 103 | + const fBatterData = await getfacingBatter.json(); |
| 104 | + |
| 105 | + const getNonFacingBatter = await fetch(nfBatUrl); |
| 106 | + const nfBatterData = await getNonFacingBatter.json(); |
| 107 | + |
| 108 | + const getcurrentBowler = await fetch(bowlerUrl); |
| 109 | + const currentBowlerData = await getcurrentBowler.json(); |
| 110 | + |
| 111 | + const getBattingTeam = await fetch(batTeamUrl); |
| 112 | + const battingTeamData = await getBattingTeam.json(); |
| 113 | + |
| 114 | + const getBowlingTeam = await fetch(bowlTeamUrl); |
| 115 | + const bowlingTeamData = await getBowlingTeam.json(); |
| 116 | + |
| 117 | + const { |
| 118 | + facingBatsman, |
| 119 | + nonFacingBatsman, |
| 120 | + currentBowler, |
| 121 | + currentBattingTeam, |
| 122 | + currentBowlingTeam, |
| 123 | + } = { |
| 124 | + facingBatsman: fBatterData.fullName, |
| 125 | + nonFacingBatsman: nfBatterData.fullName, |
| 126 | + currentBowler: currentBowlerData.fullName, |
| 127 | + currentBattingTeam: battingTeamData.fullName, |
| 128 | + currentBowlingTeam: bowlingTeamData.fullName, |
| 129 | + }; |
| 130 | + |
| 131 | + const generateRandomHexColor = () => |
| 132 | + `#${Math.floor(Math.random() * 0xffffff).toString(16)}`; |
| 133 | + |
| 134 | + // const movieRuntimeInt = parseInt(movieData.Runtime) |
| 135 | + // const movieRuntime = `${Math.floor(movieRuntimeInt/60)} Hrs, ${movieRuntimeInt%60} Mins` |
| 136 | + const scoreEmbed = new EmbedBuilder() |
| 137 | + .setColor(generateRandomHexColor()) |
| 138 | + .setThumbnail( |
| 139 | + "https://cdn.discordapp.com/attachments/690148635375435825/1054266142283284510/Slash.png" |
| 140 | + ) |
| 141 | + .setTitle(tournamentLabel) |
| 142 | + .addFields( |
| 143 | + { name: "Batting Team", value: `${currentBattingTeam}`, inline: true }, |
| 144 | + { name: "Bowling Team", value: `${currentBowlingTeam}`, inline: true }, |
| 145 | + { name: "Innings", value: `${innings}`, inline: true }, |
| 146 | + { name: "Score", value: `R/W: ${runs}/${wickets}`, inline: true }, |
| 147 | + { name: "Overs", value: `${over}`, inline: true }, |
| 148 | + { name: "Run Rate", value: `${runRate}`, inline: true }, |
| 149 | + { |
| 150 | + name: "Striker", |
| 151 | + value: `${facingBatsman} (${fr}/${fb}) \n St.Rate: ${fsr}`, |
| 152 | + inline: true, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "Non-Striker", |
| 156 | + value: `${nonFacingBatsman} (${nfr}/${nfb}) \n St.Rate: ${nfsr}`, |
| 157 | + inline: true, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "Bowler", |
| 161 | + value: `${currentBowler} \n ${br}/${bw} (${bo}) Econ: ${be}`, |
| 162 | + inline: true, |
| 163 | + }, |
| 164 | + ) |
| 165 | + // .setDescription(movieData.Plot) |
| 166 | + .setTimestamp() |
| 167 | + .setFooter({ |
| 168 | + text: "Slash", |
| 169 | + iconURL: |
| 170 | + "https://cdn.discordapp.com/attachments/690148635375435825/1054266142283284510/Slash.png", |
| 171 | + }); |
| 172 | + |
| 173 | + await interaction.reply({ embeds: [scoreEmbed] }); |
| 174 | + }, |
| 175 | +}; |
0 commit comments