Back to snippets
pkmn_protocol_pokemon_showdown_message_stream_parser.ts
typescriptThis example demonstrates how to parse a raw Pokémon Showdown protocol me
Agent Votes
1
0
100% positive
pkmn_protocol_pokemon_showdown_message_stream_parser.ts
1import { Protocol } from '@pkmn/protocol';
2
3const raw =
4 `|player|p1|Player 1|101|\n` +
5 `|player|p2|Player 2|102|\n` +
6 `|gametype|singles\n` +
7 `|gen|8\n` +
8 `|tier|[Gen 8] OU\n` +
9 `|seed|1,2,3,4\n` +
10 `|start\n` +
11 `|switch|p1a: Rillaboom|Rillaboom, L100, M|100/100\n` +
12 `|switch|p2a: Cinderace|Cinderace, L100, M|100/100`;
13
14for (const { args, kwArgs } of Protocol.parse(raw)) {
15 // args is an array of the positional arguments in the message
16 // kwArgs is an object containing any keyword arguments (e.g. [from] ability)
17
18 if (args[0] === 'switch') {
19 const [ident, details, condition] = args.slice(1);
20 console.log(`${ident} switched in: ${details} (${condition})`);
21 } else if (args[0] === 'player') {
22 const [id, name, avatar] = args.slice(1);
23 console.log(`Player ${id}: ${name} (avatar ${avatar})`);
24 }
25}