1 | import { BOT, Command, CommandBase, CommandParser, Event, Logger } from '@autobot/common';
|
2 | import { RichEmbed } from "discord.js";
|
3 | import { filter, first } from 'rxjs/operators';
|
4 | import { SpotifyClient } from './SpotifyClient';
|
5 |
|
6 | /**
|
7 | * Search the HelpDesk questions.
|
8 | *
|
9 | * Example: !search #javascript #js
|
10 | *
|
11 | */
|
12 |
|
13 | export class SearchCommand extends CommandBase {
|
14 |
|
15 | public constructor() {
|
16 |
|
17 | //
|
18 | // Set this commands configuration.
|
19 | //
|
20 | super({
|
21 |
|
22 | event: Event.MESSAGE,
|
23 | name: `${ process.env.SPOTIFY_SEARCH_PREFIX }`,
|
24 | group: 'music',
|
25 | description: 'Search',
|
26 | params: [
|
27 |
|
28 | { name: 'album' },
|
29 | { name: 'artist' },
|
30 | { name: 'playlist' },
|
31 | { name: 'track' }
|
32 |
|
33 | ]
|
34 |
|
35 | });
|
36 |
|
37 | }
|
38 |
|
39 | //
|
40 | // Called when a command matches config.name.
|
41 | //
|
42 | public async run(command: CommandParser) {
|
43 |
|
44 | console.log(command);
|
45 |
|
46 | BOT.events$.pipe(filter(event => event.name === 'refreshToken'), first()).subscribe(async (config: any) => {
|
47 |
|
48 | const results: any = await SpotifyClient.search(config.token || config.payload.token, 'track', command.namedarguments.track);
|
49 |
|
50 | if (results.tracks.items.length === 0) {
|
51 |
|
52 | command.obj.reply(new RichEmbed().setTitle(process.env.DJBOT_NAME)
|
53 | .setColor(15158332)
|
54 | .setDescription(`No results found for "${ command.namedarguments.track }"`));
|
55 |
|
56 | } else {
|
57 |
|
58 | console.log(results);
|
59 | console.log(results.tracks);
|
60 |
|
61 | results.tracks.forEach((track: any) => {
|
62 |
|
63 | console.log(track.artists);
|
64 |
|
65 |
|
66 | });
|
67 |
|
68 | }
|
69 |
|
70 | });
|
71 | //
|
72 | // const tags = command.obj.content.match(/#([a-z0-9]+)/gi);
|
73 | //
|
74 | // if (results.length > 0) {
|
75 | //
|
76 | // const embed = new RichEmbed().setTitle('Search Results');
|
77 | //
|
78 | // results.forEach(result => {
|
79 | //
|
80 | // embed.addField(`#${ result.id } (${ result.status })`, result.question);
|
81 | //
|
82 | // });
|
83 | //
|
84 | // command.obj.reply(embed);
|
85 | //
|
86 | // } else {
|
87 | //
|
88 | // command.obj.reply(new RichEmbed().setTitle('Search Results').setDescription(`No results found.`));
|
89 | //
|
90 | // }
|
91 |
|
92 | Logger.log(`AskCommand.search: ${ command.obj.content }`);
|
93 |
|
94 | }
|
95 |
|
96 | }
|