Skip to content

Commit 5c6ce52

Browse files
committed
Improve 'addteam' command
- Support Pokemon Showdown team links. - Display team after adding it.
1 parent 98d2be8 commit 5c6ce52

File tree

2 files changed

+128
-16
lines changed

2 files changed

+128
-16
lines changed

src/bot-modules/battle/commands-teams.js

Lines changed: 124 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,112 @@ function wget(url, callback) {
5757
});
5858
}
5959

60+
function parseTeamLinkPokePast(link) {
61+
let linkRegex = /^https:\/\/pokepast\.es\/([a-f0-9]+)/;
62+
let linkRes = linkRegex.exec(link);
63+
64+
if (!linkRes) {
65+
return null;
66+
}
67+
68+
return {
69+
service: "pokepast",
70+
id: linkRes[1],
71+
};
72+
}
73+
74+
function parseTeamLinkPokemonShowdown(link) {
75+
let linkRegex1 = /^https:\/\/psim\.us\/t\/([0-9]+)/;
76+
77+
let linkRes = linkRegex1.exec(link);
78+
79+
if (!linkRes) {
80+
let linkRegex2 = /^https:\/\/teams\.pokemonshowdown\.com\/view\/([0-9]+)/;
81+
82+
linkRes = linkRegex2.exec(link);
83+
84+
if (!linkRes) {
85+
return null;
86+
}
87+
}
88+
89+
return {
90+
service: "psim",
91+
id: linkRes[1],
92+
};
93+
}
94+
95+
function parseTeamLink(link) {
96+
const parseFuncs = [
97+
parseTeamLinkPokePast,
98+
parseTeamLinkPokemonShowdown,
99+
];
100+
101+
for (const fn of parseFuncs) {
102+
const parsed = fn(link);
103+
if (parsed) {
104+
return parsed;
105+
}
106+
}
107+
108+
return null;
109+
}
110+
111+
const INVALID_TEAM_FORMAT_ERROR_MESSAGE = "Invalid team format";
112+
113+
function fetchTeamPokePast(Teams, teamId, callback) {
114+
wget("https://pokepast.es/" + encodeURIComponent(teamId) + "/raw", function (data, err) {
115+
if (err) {
116+
return callback(null, err);
117+
}
118+
119+
let packed;
120+
121+
try {
122+
let team = Teams.teamToJSON(data);
123+
packed = Teams.packTeam(team);
124+
} catch (ex) {
125+
return callback(null, new Error(INVALID_TEAM_FORMAT_ERROR_MESSAGE));
126+
}
127+
128+
return callback(packed);
129+
});
130+
}
131+
132+
function fetchTeamPokemonShowdown(Teams, teamId, callback) {
133+
wget("https://teams.pokemonshowdown.com/api/getteam?teamid=" + encodeURIComponent(teamId) + "&full=1", function (data, err) {
134+
if (err) {
135+
return callback(null, err);
136+
}
137+
138+
let packed;
139+
140+
try {
141+
let team = JSON.parse(data.substring(1).trim()).team;
142+
if (!team) {
143+
return callback(null, new Error("Team not found"));
144+
}
145+
team = Teams.fastUnpackTeam(team + "");
146+
packed = Teams.packTeam(team);
147+
} catch (ex) {
148+
return callback(null, new Error(INVALID_TEAM_FORMAT_ERROR_MESSAGE));
149+
}
150+
151+
return callback(packed);
152+
});
153+
}
154+
155+
function fetchTeam(Teams, parsedLink, callback) {
156+
switch (parsedLink.service) {
157+
case "pokepast":
158+
return fetchTeamPokePast(Teams, parsedLink.id, callback);
159+
case "psim":
160+
return fetchTeamPokemonShowdown(Teams, parsedLink.id, callback);
161+
default:
162+
return callback(null, new Error("Unsupported service"));
163+
}
164+
}
165+
60166
module.exports = {
61167
addteam: function (App) {
62168
this.setLangFile(Lang_File);
@@ -93,24 +199,23 @@ module.exports = {
93199
}
94200

95201
let link = (this.args[2] + "").toLowerCase().trim();
96-
let linkRegex = /^https:\/\/pokepast\.es\/([a-f0-9]+)/;
97-
let linkRes = linkRegex.exec(link);
98202

99-
if (!linkRes) {
203+
let parsedLink = parseTeamLink(link);
204+
205+
if (!parsedLink) {
100206
return this.errorReply(this.mlt(6));
101207
}
102208

103-
wget("https://pokepast.es/" + linkRes[1] + "/raw", function (data, err) {
209+
fetchTeam(Teams, parsedLink, (packed, err) => {
104210
if (err) {
105-
return this.errorReply(this.mlt(7) + " " + err.message);
211+
if (err.message === INVALID_TEAM_FORMAT_ERROR_MESSAGE) {
212+
return this.errorReply(this.mlt(8));
213+
} else {
214+
return this.errorReply(this.mlt(7) + " " + err.message);
215+
}
106216
}
107217

108-
let packed;
109-
110-
try {
111-
let team = Teams.teamToJSON(data);
112-
packed = Teams.packTeam(team);
113-
} catch (ex) {
218+
if (!packed) {
114219
return this.errorReply(this.mlt(8));
115220
}
116221

@@ -125,7 +230,14 @@ module.exports = {
125230

126231
this.reply(this.mlt(9) + " " + Chat.italics(teamName) + " " + this.mlt(10) + " " + Chat.italics(formatName));
127232
this.addToSecurityLog();
128-
}.bind(this));
233+
234+
// Display the added team
235+
236+
this.cmd = 'getteam';
237+
this.arg = teamId;
238+
this.args = [teamId];
239+
this.parser.exec(this);
240+
});
129241
},
130242

131243
deleteteam: function (App) {

src/bot-modules/battle/commands-teams.translations

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
$0 = Team name
88
$1 = Format
9-
$2 = PokePast Link
9+
$2 = Team Link
1010

1111
$3 = The format
1212
$4 = does not exist or is not available
1313

1414
$5 = I already have a team with the same name, please choose another one.
1515

16-
$6 = The link to PokePast is invalid
16+
$6 = The link to the team is invalid
1717

1818
$7 = Could not obtain the team from the link. Error:
1919

@@ -41,14 +41,14 @@ $pokemon = Pokemon
4141

4242
$0 = Nombre del equipo
4343
$1 = Formato
44-
$2 = Link a PokePast
44+
$2 = Link del equipo
4545

4646
$3 = El formato
4747
$4 = no existe o no está disponible
4848

4949
$5 = Ya tengo un equipo con el mismo nombre, por favor elija otro.
5050

51-
$6 = El enlace a PokePast no es correcto
51+
$6 = El enlace del equipo no es correcto
5252

5353
$7 = No se pudo obtener el equipo desde el enlace. Error:
5454

0 commit comments

Comments
 (0)