Skip to content

Commit 4f86887

Browse files
authored
Merge pull request #13 from EliusHHimel/glitch
live cricket score
2 parents c35a885 + 0daaa05 commit 4f86887

File tree

5 files changed

+269
-3
lines changed

5 files changed

+269
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.glitchdotcom.json
22
.node-gyp
3-
node_modules
3+
node_modules

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Slash Bot
22

33
This project is an open-source Discord Bot which let it's users do a lot of cool things.
4+
5+
[![Discord Bots](https://top.gg/api/widget/1053590161897816114.svg)(https://top.gg/bot/1053590161897816114)
6+
47
# Invitation Link
58
Invite this bot in your server to use the cool features developed by cool people around the globe.
69

commands/cricscore.js

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
};

commands/match.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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("match")
15+
.setDescription("Get ICC match list"),
16+
async execute(interaction) {
17+
// Get date
18+
const today = new Date();
19+
const currentDate = JSON.stringify(today).slice(1, 11);
20+
const url = `${iccAPI}/fixtures?tournamentTypes=I%2CWI&startDate=${currentDate}&endDate=${currentDate}&pageSize=100`;
21+
const getMatch = await fetch(url, {
22+
headers: {
23+
accept: "*/*",
24+
"accept-language": "en-US,en;q=0.9",
25+
account: "ICC",
26+
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
27+
"sec-ch-ua":
28+
'"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
29+
"sec-ch-ua-mobile": "?0",
30+
"sec-ch-ua-platform": '"Windows"',
31+
"sec-fetch-dest": "empty",
32+
"sec-fetch-mode": "cors",
33+
"sec-fetch-site": "cross-site",
34+
},
35+
referrer: "https://www.icc-cricket.com/",
36+
referrerPolicy: "strict-origin-when-cross-origin",
37+
body: null,
38+
method: "GET",
39+
mode: "cors",
40+
credentials: "omit",
41+
});
42+
const matchData = await getMatch.json();
43+
// console.log(matchData.content[0].scheduleEntry);
44+
const embeds = [];
45+
for (let match in matchData.content) {
46+
let singleMatchData = matchData.content[match];
47+
console.log(singleMatchData.scheduleEntry.matchId.id);
48+
const { tournamentLabel, matchLabel, matchID, venue, date, time } = {
49+
tournamentLabel: singleMatchData.tournamentLabel,
50+
matchLabel: singleMatchData.label,
51+
matchID: singleMatchData.scheduleEntry.matchId.id,
52+
venue: singleMatchData.scheduleEntry.venue.fullName,
53+
date: singleMatchData.timestamp.slice(0, 10),
54+
time: singleMatchData.timestamp.slice(11).slice(0, 8),
55+
};
56+
const generateRandomHexColor = () =>
57+
`#${Math.floor(Math.random() * 0xffffff).toString(16)}`;
58+
59+
const matchEmbed = new EmbedBuilder()
60+
.setColor(generateRandomHexColor())
61+
.setThumbnail(
62+
"https://cdn.discordapp.com/attachments/690148635375435825/1054266142283284510/Slash.png"
63+
)
64+
.setTitle(tournamentLabel)
65+
.addFields(
66+
{ name: "Label", value: matchLabel, inline: true },
67+
{ name: "Venue", value: venue, inline: true },
68+
{ name: "Match ID", value: `${matchID}`, inline: true },
69+
{ name: "Date", value: `${date}`, inline: true },
70+
{ name: "Time", value: `${time}`, inline: true }
71+
)
72+
// .setDescription(movieData.Plot)
73+
.setTimestamp()
74+
.setFooter({
75+
text: "Slash",
76+
iconURL:
77+
"https://cdn.discordapp.com/attachments/690148635375435825/1054266142283284510/Slash.png",
78+
});
79+
embeds.push(matchEmbed);
80+
}
81+
82+
await interaction.reply({ embeds: embeds });
83+
},
84+
};

server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ client.on(Events.InteractionCreate, async interaction => {
5050
// When the client is ready, run this code (only once)
5151
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
5252
client.once(Events.ClientReady, c => {
53+
54+
let jsonData = JSON.stringify(client.guilds.cache);
55+
// console.log(jsonData)
56+
5357
client.user.setActivity(`/help on ${client.guilds.cache.size} Servers with ${client.guilds.cache.reduce((a, g) => a + g.memberCount, 0)} People`, { type: ActivityType.Playing});
5458
console.log(`Ready! Logged in as ${c.user.tag}`);
5559
});
@@ -65,7 +69,7 @@ app.get("/wakeup", function(request, response) {
6569
app.get('/', (req, res) => {
6670
res.send('Slash Server Running')
6771
})
68-
72+
6973
app.listen(5000, () => {
7074
console.log('Running server on port', 5000)
71-
})
75+
})

0 commit comments

Comments
 (0)