forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopen.ts
More file actions
256 lines (228 loc) · 7.21 KB
/
open.ts
File metadata and controls
256 lines (228 loc) · 7.21 KB
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
import {
SlashCommandBuilder,
ChatInputCommandInteraction,
EmbedBuilder,
MessageFlags,
} from 'discord.js';
import { userManager } from '../database/userManager';
import { auditManager } from '../database/auditManager';
import IdleChampionsApi from '../api/idleChampionsApi';
enum ChestType {
Copper = 1,
Iron = 2,
Steel = 3,
Gold = 4,
Sapphire = 5,
Emerald = 6,
Ruby = 7,
Diamond = 8,
Platinum = 9,
}
export const data = new SlashCommandBuilder()
.setName('open')
.setDescription('Open chests in Idle Champions')
.addStringOption((option) =>
option
.setName('chest_type')
.setDescription('Type of chest to open')
.setRequired(true)
.addChoices(
{ name: 'Copper', value: '1' },
{ name: 'Iron', value: '2' },
{ name: 'Steel', value: '3' },
{ name: 'Gold', value: '4' },
{ name: 'Sapphire', value: '5' },
{ name: 'Emerald', value: '6' },
{ name: 'Ruby', value: '7' },
{ name: 'Diamond', value: '8' },
{ name: 'Platinum', value: '9' }
)
)
.addIntegerOption((option) =>
option
.setName('count')
.setDescription('Number of chests to open (1-1000)')
.setRequired(true)
.setMinValue(1)
.setMaxValue(1000)
);
export async function execute(interaction: ChatInputCommandInteraction) {
try {
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
// Check if user has credentials
const credentials = await userManager.getCredentials(interaction.user.id);
if (!credentials) {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle('❌ No Credentials Found')
.setDescription('Please set up your Idle Champions credentials first using `/setup`');
await interaction.editReply({ embeds: [embed] });
return;
}
const chestTypeId = parseInt(interaction.options.getString('chest_type', true));
const count = interaction.options.getInteger('count', true);
// Get server
let server = credentials.server;
if (!server) {
server = await IdleChampionsApi.getServer();
if (!server) {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle('❌ Error')
.setDescription('Could not determine game server.');
await interaction.editReply({ embeds: [embed] });
return;
}
await userManager.updateServer(interaction.user.id, server);
}
// Get fresh user details to get current instance ID
let userResult = await IdleChampionsApi.getUserDetails({
server,
user_id: credentials.userId,
hash: credentials.userHash,
});
// Handle server switch
if (
userResult instanceof Object &&
'status' in userResult &&
(userResult as any).status === 4
) {
server = (userResult as any).newServer;
if (!server) {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle('❌ Error')
.setDescription('Server switch failed.');
await interaction.editReply({ embeds: [embed] });
return;
}
await userManager.updateServer(interaction.user.id, server);
userResult = await IdleChampionsApi.getUserDetails({
server,
user_id: credentials.userId,
hash: credentials.userHash,
});
}
const userData = userResult as any;
if (!userData || !userData.details) {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle('❌ Error')
.setDescription('Could not retrieve user data.');
await interaction.editReply({ embeds: [embed] });
return;
}
// Get instance ID from user details (not from game_instances)
const instanceId = userData.details.instance_id || '0';
if (!instanceId || instanceId === '0') {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle('❌ Error')
.setDescription('Could not retrieve valid instance ID from server.');
await interaction.editReply({ embeds: [embed] });
return;
}
const chestName = getChestName(chestTypeId);
// Show processing message
const processingEmbed = new EmbedBuilder()
.setColor(0xffaa00)
.setTitle('⏳ Opening Chests...')
.setDescription(`Opening ${count} ${chestName}(s)...`);
await interaction.editReply({ embeds: [processingEmbed] });
// Open chests
const response = await IdleChampionsApi.openChests({
server,
user_id: credentials.userId,
hash: credentials.userHash,
chestTypeId: chestTypeId as any,
count,
instanceId,
});
// Log action
await auditManager.logAction(interaction.user.id, 'CHESTS_OPENED', {
chestType: chestName,
count,
});
// Build response embed
const embed = new EmbedBuilder()
.setColor(0x00ff00)
.setTitle('✅ Chests Opened Successfully')
.addFields({
name: 'Chest Type',
value: chestName,
inline: true,
})
.addFields({
name: 'Opened',
value: count.toString(),
inline: true,
});
// Add response data if available
if (response instanceof Object && 'chests_remaining' in response) {
const responseData = response as any;
if (responseData.chests_remaining !== undefined) {
embed.addFields({
name: 'Remaining',
value: responseData.chests_remaining.toString(),
inline: true,
});
}
}
if (response instanceof Object && 'lootDetail' in response) {
const openResponse = response as any;
if (
openResponse.lootDetail &&
Array.isArray(openResponse.lootDetail) &&
openResponse.lootDetail.length > 0
) {
// Group loot by type for summary
const lootSummary: { [key: string]: number } = {};
for (const loot of openResponse.lootDetail) {
const description = loot.description || JSON.stringify(loot);
lootSummary[description] = (lootSummary[description] || 0) + 1;
}
const lootLines = Object.entries(lootSummary)
.map(([item, amount]) => `• ${item}${amount > 1 ? ` x${amount}` : ''}`)
.join('\n')
.substring(0, 1024);
embed.addFields({
name: 'Equipment Found',
value: lootLines || 'Unknown loot',
inline: false,
});
} else {
embed.addFields({
name: '📦 Loot',
value: 'No equipment found in these chests.',
inline: false,
});
}
} else {
embed.addFields({
name: '📦 Loot',
value: 'No equipment found in these chests.',
inline: false,
});
}
await interaction.editReply({ embeds: [embed] });
} catch (error) {
console.error('[OPEN COMMAND] Error:', error);
await interaction.editReply({
content: '❌ An error occurred while opening chests.',
});
}
}
function getChestName(chestId: number): string {
const chests: { [key: number]: string } = {
1: 'Copper Chest',
2: 'Iron Chest',
3: 'Steel Chest',
4: 'Gold Chest',
5: 'Sapphire Chest',
6: 'Emerald Chest',
7: 'Ruby Chest',
8: 'Diamond Chest',
9: 'Platinum Chest',
};
return chests[chestId] || `Chest ${chestId}`;
}