-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
141 lines (129 loc) · 4.02 KB
/
controller.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
require('dotenv').config();
const helpers = require('./helpers.js');
const model = require('./model.js');
const FlagsView = require('./views/flagsView.js');
const Discord = require('discord.js');
const client = new Discord.Client();
const token = process.env.DISCORD_TOKEN_TEST;
const streetViewToken = process.env.GOOGLE_API_KEY;
const { prefix, quizTimeout, mTimeout } = require('./config.json');
const flagsView = require('./views/flagsView.js');
const { default: fetch } = require('node-fetch');
let numRounds = 5;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'flags') {
//reset score
model.reset();
await flagRound(msg, args);
// for (i = 0; i < 5; i++) await flagGame(msg, args, i + 1);
}
if (command === 'world') {
const imageURL = await model.getStreetViewImage(streetViewToken);
await msg.channel.send(imageURL);
}
if (command === 'hint') {
if (!model.state.inProgress) {
await msg.channel.send(
"You can only ask for hints when there's a game in progress!"
);
return;
}
const message = getHint();
const m = await msg.reply(message);
setTimeout(() => {
msg.delete();
m.delete();
}, 5000);
}
if (msg.content === 'ping') {
msg.reply('pong');
}
});
// const flagGame = async function (msg, args) {};
const countryGuess = async function (msg, args) {};
const flagRound = async function (msg, args, prev = null) {
try {
const country = model.getRandomCountry();
await model.loadCountryData(country);
} catch {
await flagRound(msg, args);
return;
}
const mcList =
args[0] === 'hard'
? []
: model.getListOfRandomCountries(model.state.country.name, 3);
const params = [prev ? prev : msg, mcList, model.state];
if (!prev) {
prev = await FlagsView.renderQuestionList(...params);
} else {
try {
console.log(prev.author, msg.author);
prev = await FlagsView.updateQuestionList(...params);
} catch (err) {
console.error('Couldnt update embed message: ', err);
}
}
const filter = m =>
m.author.id === msg.author.id &&
((args[0] === 'hard' && m.content != prefix + 'hint') || !isNaN(m.content));
msg.channel
.awaitMessages(filter, {
max: 1,
time: quizTimeout,
errors: ['time'],
})
.then(collected =>
handleResult(
msg,
collected,
(args[0] === 'hard'
? collected.first().content
: mcList[collected.first().content - 1]) === model.state.country.name
)
)
.then(async () => {
if (model.state.round < numRounds) {
model.state.round++;
setTimeout(async () => await flagRound(msg, args, prev), mTimeout);
} else {
flagsView.renderCredits(msg, model.state);
model.state.inProgress = false;
}
})
.catch(err => {
console.error(err);
msg.channel.send('Times up!');
model.state.inProgress = false;
});
};
const handleResult = function (msg, collected, isCorrect) {
msg.channel
.send(
`${isCorrect ? 'Correct!' : 'Wrong answer!'} That flag belongs to ${
model.state.country.name
}!`
)
.then(m => setTimeout(() => m.delete(), mTimeout));
collected.first().react(isCorrect ? '✅' : '❌');
model.state.score = model.state.score + isCorrect ? 1 : 0;
setTimeout(() => collected.first().delete(), mTimeout);
model.addToRecord(model.state.country, isCorrect);
};
const getHint = function () {
const country = model.state.country;
console.log(country.currency);
const hints = [
`the currency of the country is the ${country.currency}`,
`the language of the country is ${country.language}`,
`the region of the country is ${country.region}`,
];
return hints[Math.floor(Math.random() * hints.length)];
};
client.login(token);