-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
312 lines (270 loc) · 10.5 KB
/
app.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { config } from "dotenv";
config();
import express from "express";
import {
InteractionType,
InteractionResponseType,
InteractionResponseFlags,
MessageComponentTypes,
ButtonStyleTypes,
} from "discord-interactions";
import {
VerifyDiscordRequest,
getRandomEmoji,
DiscordRequest,
} from "./utils.js";
import { getShuffledOptions, getResult } from "./game.js";
import { acceptBet, placeBet, fundWallet, withdraw, send } from "./back.js";
import multer from "multer";
console.log(`Hello ${process.env.HELLO}`);
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
const activeGames = {};
app.post("/interactions", async function (req, res) {
const { type, id, data } = req.body;
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
}
if (type === InteractionType.APPLICATION_COMMAND) {
const { name } = data;
if (name === "test") {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: "hello world " + getRandomEmoji(),
},
});
}
if (name === "fundwallet" && id) {
const userId = req.body.member.user.id;
// User's object choice
const amount = parseFloat(req.body.data.options[0].value);
const currency = req.body.data.options[1].value;
const url = await fundWallet(userId, amount, currency);
res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content:
"Click the link and the QR code with your Solana wallet to fund your account",
components: [
{
type: MessageComponentTypes.ACTION_ROW,
components: [
{
type: MessageComponentTypes.BUTTON,
label: "Fund Wallet",
style: ButtonStyleTypes.LINK,
url: url,
},
],
},
],
},
});
// const recieved = await waitFunds(userId);
// if (recieved) {
// axios.post(
// `https://discord.com/api/v8/interactions/${req.body.id}/${req.body.token}/callback`,
// {
// type: 4,
// data: {
// content: "Funds received!",
// },
// }
// );
// } else {
// axios.post(
// `https://discord.com/api/v8/interactions/${req.body.id}/${req.body.token}/callback`,
// {
// type: 4,
// data: {
// content: "Funds not received.",
// },
// }
// );
// }
} else if (name === "create_bet" && id) {
const userId = req.body.member.user.id;
// User's object choice
const amount = parseFloat(req.body.data.options[0].value);
const currency = req.body.data.options[1].value;
const my_winner = req.body.data.options[2].value;
const will_destroy = req.body.data.options[3].value;
const challenger = req.body.data.options[4]?.value;
const { success, id } = await placeBet(
userId,
amount,
currency,
my_winner,
will_destroy,
challenger
);
const message = `<@${userId}> thinks that ${my_winner} will destroy ${will_destroy} in their next game. He's betting ${amount} ${currency}! Will you accept the challenge${
challenger ? " <@" + challenger + ">" : ""
} ?`;
const custom_id = `accept_button_${id}`;
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: message,
components: [
{
type: MessageComponentTypes.ACTION_ROW,
components: [
{
type: MessageComponentTypes.BUTTON,
// Append the game ID to use later on
custom_id: custom_id,
label: "Bet against",
style: ButtonStyleTypes.SUCCESS,
},
],
},
],
},
});
} else if (name === "withdraw" && id) {
const userId = req.body.member.user.id;
const amount = parseFloat(req.body.data.options[0].value);
const currency = req.body.data.options[1].value.toUpperCase();
const recipient = req.body.data.options[2].value;
const { success, msg } = await withdraw(
recipient,
amount,
userId,
currency
);
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: msg,
flags: InteractionResponseFlags.EPHEMERAL,
},
});
} else if (name === "send" && id) {
const userId = req.body.member.user.id;
const amount = parseFloat(req.body.data.options[0].value);
const currency = req.body.data.options[1].value.toUpperCase();
const recipient = req.body.data.options[2].value;
const { success, msg } = await send(
userId,
currency,
amount,
recipient
);
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: msg,
flags: InteractionResponseFlags.EPHEMERAL,
},
});
}
}
if (type === InteractionType.MESSAGE_COMPONENT) {
// custom_id set in payload when sending message component
const componentId = data.custom_id;
if (componentId.startsWith("accept_button_")) {
// get the associated game ID
const gameId = componentId.replace("accept_button_", "");
try {
const { success, msg } = await acceptBet(
req.body.member.user.id,
gameId
);
if (!success) {
return res.send({
type:
InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: msg,
flags: InteractionResponseFlags.EPHEMERAL,
},
});
}
// disable the button
return res.send({
type: InteractionResponseType.UPDATE_MESSAGE,
data: {
content: `Bet #${gameId} is accepted!`,
// flags: InteractionResponseFlags.EPHEMERAL,
},
});
// await res.send({
// type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
// data: {
// content: `Bet #${id} is settled!`,
// },
// });
// // Delete previous message
} catch (err) {
console.error("Error sending message:", err);
}
}
}
});
// Set up multer for file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "soplay/"); // Set the destination directory
},
filename: function (req, file, cb) {
cb(null, file.originalname); // Keep the original filename
},
});
const upload = multer({ storage: storage });
// Route to handle file uploads
app.post("/solpay", upload.single("image"), (req, res) => {
res.send("File uploaded successfully");
});
// Route to serve the uploaded file externally
app.get("/solpay/qrcode.png", (req, res) => {
const filename = req.params.filename;
const filePath = path.join(__dirname, "uploads", filename);
res.sendFile(filePath);
});
app.listen(PORT, () => {
console.log("Listening on port", PORT);
});
/*
const riotApiKey = process.env.RIOT_API_KEY;
const bot = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
const RIOT_API_KEY = riotApiKey;
bot.once('ready', () => {
console.log('Bot is online!');
});
bot.on('message', async (message) => {
if (message.content.startsWith('!gameResult')) {
const args = message.content.split(' ');
if (args.length < 3) {
message.channel.send('Please provide a region and a summoner name.');
return;
}
const REGION = args[1];
const SUMMONER_NAME = args.slice(2).join(' ');
try {
const summonerResponse = await axios.get(
`https://${REGION}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${SUMMONER_NAME}`,
{ headers: { 'X-Riot-Token': RIOT_API_KEY } }
);
const puuid = summonerResponse.data.puuid;
const matchlistResponse = await axios.get(
`https://${REGION}.api.riotgames.com/lol/match/v5/matches/by-puuid/${puuid}/ids`,
{ headers: { 'X-Riot-Token': RIOT_API_KEY } }
);
const mostRecentMatchId = matchlistResponse.data[0];
const matchDetails = await axios.get(
`https://${REGION}.api.riotgames.com/lol/match/v5/matches/${mostRecentMatchId}`,
{ headers: { 'X-Riot-Token': RIOT_API_KEY } }
);
const winningTeam = matchDetails.data.info.teams.find((team) => team.win).teamId == 100 ? 'Blue' : 'Red';
message.channel.send(`The winning team is ${winningTeam}`);
} catch (error) {
console.error(error);
message.channel.send('There was an error fetching the game results.');
}
}
}); */