forked from Neeraj-x0/X-Asena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun.js
207 lines (184 loc) · 5.28 KB
/
fun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const { command, isPrivate, XKCDComic, getJson } = require("../../lib");
const axios = require("axios");
let triviaGames = {};
command(
{
pattern: "trivia",
fromMe: isPrivate,
desc: "Start a trivia game.",
type: "game",
},
async (message, match, m) => {
const userId = message.sender;
if (triviaGames[userId]) {
return message.reply("You already have a trivia game in progress.");
}
const triviaQuestion = await fetchTriviaQuestion();
triviaGames[userId] = {
currentQuestion: triviaQuestion,
correctAnswers: 0,
initiator: userId,
chatId: message.key.remoteJid,
};
return sendTriviaQuestion(message, userId);
}
);
command(
{
on: "text",
fromMe: isPrivate,
pattern: false,
dontAddCommandList: true,
},
async (message, match, m) => {
const userId = message.sender;
if (triviaGames[userId]) {
const userTriviaGame = triviaGames[userId];
const userAnswer = message.text ? message.text.trim() : "";
if (
userId === userTriviaGame.initiator &&
message.key.remoteJid === userTriviaGame.chatId
) {
if (isOptionNumber(userAnswer)) {
const selectedOption = parseInt(userAnswer);
const correctAnswerIndex =
userTriviaGame.currentQuestion.options.indexOf(
userTriviaGame.currentQuestion.correctAnswer
) + 1;
if (selectedOption === correctAnswerIndex) {
userTriviaGame.correctAnswers++;
message.reply(
`Correct answer \n\n Your Points : ${userTriviaGame.correctAnswers}`
);
userTriviaGame.currentQuestion = await fetchTriviaQuestion();
return sendTriviaQuestion(message, userId);
} else {
message.reply(
`Incorrect answer. The correct answer is option ${correctAnswerIndex} ${userTriviaGame.currentQuestion.correctAnswer}.`
);
return await endTriviaGame(message, userId);
}
}
}
}
}
);
function isOptionNumber(answer) {
const selectedOption = parseInt(answer);
return (
!isNaN(selectedOption) &&
answer.length === 1 &&
selectedOption >= 1 &&
selectedOption <= 4
);
}
async function fetchTriviaQuestion() {
try {
const response = await axios.get("https://the-trivia-api.com/v2/questions");
const questions = response.data;
const randomQuestion =
questions[Math.floor(Math.random() * questions.length)];
const shuffledOptions = [
...randomQuestion.incorrectAnswers,
randomQuestion.correctAnswer,
];
for (let i = shuffledOptions.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledOptions[i], shuffledOptions[j]] = [
shuffledOptions[j],
shuffledOptions[i],
];
}
const formattedQuestion = {
text: randomQuestion.question.text,
options: shuffledOptions,
correctAnswer: randomQuestion.correctAnswer,
};
return formattedQuestion;
} catch (error) {
console.error("Error fetching trivia question:", error.message);
throw error;
}
}
function sendTriviaQuestion(message, userId) {
const userTriviaGame = triviaGames[userId];
const currentQuestion = userTriviaGame.currentQuestion;
const optionsString = currentQuestion.options
.map((option, index) => `${index + 1}. ${option}`)
.join("\n");
message.reply(
`Question: ${currentQuestion.text}\nOptions:\n${optionsString}`
);
}
async function endTriviaGame(message, userId) {
const userTriviaGame = triviaGames[userId];
await message.reply(
`Trivia game ended. You answered ${userTriviaGame.correctAnswers} questions correctly.`
);
delete triviaGames[userId];
}
/**
*
*/
command(
{
pattern: "xkcd",
fromMe: isPrivate,
desc: "Send a random XKCD comic.",
type: "misc",
},
async (message, match, m) => {
try {
const result = await XKCDComic();
message.sendMessage(
message.jid,
result.imageUrl,
{ quoted: message.data },
"image"
);
} catch (error) {
console.error("Error:", error.message);
message.reply("Error fetching XKCD comic.");
}
}
);
/**
*
*
*/
command(
{
pattern: "joke",
fromMe: isPrivate,
desc: "Fetch a random joke",
dontAddCommandList: false,
},
async (message, match) => {
try {
let jokeData;
if (match && match.toLowerCase() == "dark") {
jokeData = await getJson(
"https://v2.jokeapi.dev/joke/Dark?type=twopart"
);
} else if (match && match.toLowerCase() == "pun") {
jokeData = await getJson(
"https://v2.jokeapi.dev/joke/Pun?type=twopart"
);
} else {
jokeData = await getJson(
"https://v2.jokeapi.dev/joke/Any?type=twopart"
);
}
if (jokeData && !jokeData.error) {
const jokeMessage = jokeData.setup + "\n" + jokeData.delivery;
message.sendMessage(message.jid, jokeMessage);
} else {
console.error("Error fetching joke:", jokeData);
message.reply("Failed to fetch a joke. Please try again later.");
}
} catch (error) {
console.error("Error fetching joke:", error);
message.reply("Failed to fetch a joke. Please try again later.");
}
}
);