Repository navigation
Expand file tree
/
Copy pathsquad.js
More file actions
195 lines (179 loc) · 5.82 KB
/
Copy pathsquad.js
File metadata and controls
195 lines (179 loc) · 5.82 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
const {
Client,
Events,
GatewayIntentBits,
IntentsBitField,
EmbedBuilder
} = require("discord.js");
const WebSocket = require("ws");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { SlashCommandBuilder } = require("@discordjs/builders");
const token = "";
const clientId = "";
const guildId = "";
const socketUrl = "wss://matchmaking.narrow-one.com";
const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
]
});
const commands = [
new SlashCommandBuilder()
.setName('createsquad')
.setDescription('Create N1 squad code from server')
.addStringOption(option =>
option.setName('setting')
.setDescription('Choose the squad setting')
.setRequired(true)
.addChoices(
{ name: 'Public', value: 'public' },
{ name: 'Private', value: 'private' }
)
),
new SlashCommandBuilder()
.setName("seasonalgear")
.setDescription("Get information about events")
].map((command) => command.toJSON());
const rest = new REST({ version: "9" }).setToken(token);
const versionFetch = async () => {
try {
const response = await fetch("https://narrow.one/js/sw.js");
if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
const rawtext = await response.text();
const VERSION = rawtext.substring(rawtext.search("narrowClient") + 12, rawtext.search("narrowClient") + 22);
//console.log(VERSION);
return VERSION;
} catch (error) {
console.error('Error fetching version:', error);
return null;
}
};
const squadcodepublic = async (VERSION) => {
return new Promise((resolve, reject) => {
const ws = new WebSocket(socketUrl);
ws.on("open", () => {
ws.send(JSON.stringify({ op: "myClientVersion", version: VERSION }));
setTimeout(() => {
ws.send(JSON.stringify({ op: "elo", elo: 100 }));
setTimeout(() => {
ws.send(JSON.stringify({ op: "requestSquadId" }));
setTimeout(() => {
ws.send(JSON.stringify({ op: "username", username: "Guest 2133" }));
ws.send(JSON.stringify({ op: "startSquad" }));
}, 500);
}, 800);
}, 500);
});
ws.on("message", (data) => {
try {
let message = JSON.parse(data);
if (message.op === "squadId") {
console.log(message.squadId);
resolve(message.squadId);
}
} catch (error) {
console.error("Error parsing JSON:", error);
reject(error);
}
});
ws.on("error", (error) => {
console.error('WebSocket error:', error);
reject(error);
});
ws.on("close", () => {
console.log('WebSocket connection closed');
resolve();
});
});
};
const squadcodeprivate = async (VERSION) => {
return new Promise((resolve, reject) => {
const ws = new WebSocket(socketUrl);
ws.on("open", () => {
ws.send(JSON.stringify({ op: "myClientVersion", version: VERSION }));
delay(500).then(() => {
ws.send(JSON.stringify({ op: "elo", elo: 100 }));
delay(800).then(() => {
ws.send(JSON.stringify({ op: "requestSquadId" }));
delay(500).then(() => {
ws.send(JSON.stringify({ op: "username", username: "Guest 2133" }));
ws.send(JSON.stringify({ op: "changeSquadSetting", setting: "privateSquad", value: true }));
ws.send(JSON.stringify({ op: "startSquad" }));
});
});
});
});
ws.on("message", (data) => {
try {
let message = JSON.parse(data);
if (message.op === "squadId") {
console.log(message.squadId);
resolve(message.squadId);
}
} catch (error) {
console.error("Error parsing JSON:", error);
reject(error);
}
});
ws.on("error", (error) => {
console.error('WebSocket error:', error);
reject(error);
});
ws.on("close", () => {
console.log('WebSocket connection closed');
resolve();
});
});
};
const main = async () => {
try {
console.log("Started refreshing application (/) commands.");
await rest.put(
Routes.applicationCommands(clientId),
{ body: commands }
);
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
// const VERSION = await versionFetch();
client.login(token);
client.on(Events.ClientReady, () => {
console.log(`Ready Logged in as ${client.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === "createsquad") {
const setting = interaction.options.getString('setting');
if (setting === 'public') {
try {
const VERSION = await versionFetch();
const squadId = await squadcodepublic(VERSION);
interaction.reply(`Less Go Guy's Join publicSquad \n https://narrow.one/#${squadId}`);
} catch (error) {
console.error("Error:", error);
await interaction.reply({ content: 'Error generating squad code', ephemeral: true });
}
} else if (setting === 'private') {
try {
const VERSION = await versionFetch();
const squadId = await squadcodeprivate(VERSION);
interaction.reply(`Less Go Guy's Join privateSquad \n https://narrow.one/#${squadId}`);
} catch (error) {
console.error("Error:", error);
await interaction.reply({ content: 'Error generating squad code', ephemeral: true });
}
}
}
});
};
main();