|
| 1 | +import Discord from 'discord.js'; |
| 2 | +import fetch from 'node-fetch'; |
| 3 | + |
| 4 | +import type { Command } from '../types'; |
| 5 | + |
| 6 | +const ICON_URL = |
| 7 | + 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Stack_Overflow_icon.svg/768px-Stack_Overflow_icon.svg.png'; |
| 8 | + |
| 9 | +const isApiError = (data: ApiResponse): data is ApiResponseError => 'error_id' in data; |
| 10 | + |
| 11 | +const formatTitle = (length: number) => |
| 12 | + length === 1 |
| 13 | + ? 'najlepsza odpowiedź' |
| 14 | + : length < 5 |
| 15 | + ? 'najlepsze odpowiedzi' |
| 16 | + : 'najlepszych odpowiedzi'; |
| 17 | + |
| 18 | +const stackoverflow: Command = { |
| 19 | + name: 'stackoverflow', |
| 20 | + description: 'Wyszukaj swój problem na stackoverflow', |
| 21 | + args: 'required', |
| 22 | + async execute(msg, args) { |
| 23 | + const query = encodeURIComponent(args.join(' ').toLocaleLowerCase()); |
| 24 | + const result = await fetch( |
| 25 | + `https://api.stackexchange.com/2.3/search/advanced?pagesize=5&order=desc&sort=activity&q=${query}&site=stackoverflow`, |
| 26 | + ); |
| 27 | + const response = (await result.json()) as ApiResponse; |
| 28 | + |
| 29 | + if (!result.ok || isApiError(response)) { |
| 30 | + return msg.channel.send('Przepraszam, ale coś poszło nie tak 😭'); |
| 31 | + } |
| 32 | + |
| 33 | + const fields = response.items.map(({ title, link }) => ({ |
| 34 | + name: title, |
| 35 | + value: link, |
| 36 | + })); |
| 37 | + |
| 38 | + if (fields.length === 0) { |
| 39 | + return msg.channel.send('Niestety nic nie znalazłem 😭'); |
| 40 | + } |
| 41 | + |
| 42 | + const embed = new Discord.MessageEmbed() |
| 43 | + .setAuthor('Stack Overflow', ICON_URL, 'https://stackoverflow.com/') |
| 44 | + .setTitle(`${fields.length} ${formatTitle(fields.length)}:`) |
| 45 | + .addFields(fields) |
| 46 | + .setColor('f4811e'); |
| 47 | + |
| 48 | + return msg.channel.send(embed); |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +export default stackoverflow; |
| 53 | + |
| 54 | +interface ApiOwner { |
| 55 | + readonly account_id: number; |
| 56 | + readonly reputation: number; |
| 57 | + readonly user_id: number; |
| 58 | + readonly user_type: string; |
| 59 | + readonly profile_image: string; |
| 60 | + readonly display_name: string; |
| 61 | + readonly link: string; |
| 62 | +} |
| 63 | + |
| 64 | +interface ApiItem { |
| 65 | + readonly tags: ReadonlyArray<string>; |
| 66 | + readonly owner: ApiOwner; |
| 67 | + readonly is_answered: boolean; |
| 68 | + readonly view_count: number; |
| 69 | + readonly answer_count: number; |
| 70 | + readonly score: number; |
| 71 | + readonly last_activity_date: number; |
| 72 | + readonly creation_date: number; |
| 73 | + readonly question_id: number; |
| 74 | + readonly content_license: string; |
| 75 | + readonly link: string; |
| 76 | + readonly title: string; |
| 77 | +} |
| 78 | + |
| 79 | +interface ApiResponseSuccess { |
| 80 | + readonly items: ReadonlyArray<ApiItem>; |
| 81 | + readonly has_more: boolean; |
| 82 | + readonly quota_max: number; |
| 83 | + readonly quota_remaining: number; |
| 84 | +} |
| 85 | + |
| 86 | +interface ApiResponseError { |
| 87 | + readonly error_id: number; |
| 88 | + readonly error_message: string; |
| 89 | + readonly error_name: string; |
| 90 | +} |
| 91 | + |
| 92 | +type ApiResponse = ApiResponseSuccess | ApiResponseError; |
0 commit comments