-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add ;man command #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { | ||
| ActionRowBuilder, | ||
| ButtonBuilder, | ||
| ButtonStyle, | ||
| EmbedBuilder, | ||
| } from "discord.js"; | ||
| import type { CommandCallbackOpts } from "../../types/command.ts"; | ||
| import { man } from "../../utils/man.ts"; | ||
|
|
||
| export default { | ||
| name: "man", | ||
| description: "Search man pages", | ||
| aliases: ["manual"], | ||
| usage: "man <term>", | ||
| react: "📖", | ||
| async callback({ message, args }: CommandCallbackOpts) { | ||
| try { | ||
| // returns return { | ||
| // title, | ||
| // section, | ||
| // url, | ||
| // raw, | ||
| // sections, | ||
| // }; | ||
| const page = await man(args.join(" ")); | ||
| if (!page) { | ||
| return message.reply({ | ||
| embeds: [ | ||
| new EmbedBuilder() | ||
| .setTitle("Man Page Not Found") | ||
| .setDescription( | ||
| `No man page found for the term "${args.join(" ")}".`, | ||
| ) | ||
| .setColor(0xff0000), | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| const embed = new EmbedBuilder() | ||
| .setTitle(`${page.title}`) | ||
| .setDescription(page.description || "No description available.") | ||
| .setColor(0x7289da); | ||
|
|
||
| const button = new ButtonBuilder() | ||
| .setLabel("Read Full Man Page") | ||
| .setStyle(ButtonStyle.Link) | ||
| .setURL(page.url); | ||
| const row = new ActionRowBuilder().addComponents(button); | ||
| return message.reply({ embeds: [embed], components: [row.toJSON()] }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| return message.reply("Failed to fetch man page."); | ||
| } | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| export interface ManPage { | ||
| title: string; | ||
| url: string; | ||
| raw: string; | ||
| description: string; | ||
| } | ||
|
|
||
| export async function man(term: string): Promise<ManPage | null> { | ||
| term = term.trim(); | ||
|
|
||
| if (!/^[a-zA-Z0-9._+-]+$/.test(term)) { | ||
| throw new Error("Invalid man page."); | ||
| } | ||
|
|
||
| const url = `https://man.archlinux.org/man/${encodeURIComponent(term)}.txt`; | ||
|
|
||
| const res = await fetch(url); | ||
|
|
||
| if (!res.ok) return null; | ||
|
|
||
| const raw = (await res.text()).replace(/\r\n/g, "\n").trim(); | ||
|
|
||
| if (!raw) return null; | ||
|
|
||
| const firstLine = raw.split("\n")[0]?.trim(); | ||
|
|
||
| const match = firstLine?.match(/^([^(]+)\(([^)]+)\)$/); | ||
|
|
||
| const sections: Record<string, string> = {}; | ||
|
|
||
| const headings = [ | ||
| "NAME", | ||
| "SYNOPSIS", | ||
| "DESCRIPTION", | ||
| "OPTIONS", | ||
| "COMMANDS", | ||
| "ARGUMENTS", | ||
| "OPERANDS", | ||
| "EXIT STATUS", | ||
| "RETURN VALUE", | ||
| "ERRORS", | ||
| "ENVIRONMENT", | ||
| "FILES", | ||
| "ATTRIBUTES", | ||
| "VERSIONS", | ||
| "STANDARDS", | ||
| "NOTES", | ||
| "BUGS", | ||
| "EXAMPLES", | ||
| "AUTHORS", | ||
| "AUTHOR", | ||
| "COPYRIGHT", | ||
| "SEE ALSO", | ||
| "HISTORY", | ||
| "DIAGNOSTICS", | ||
| "CAVEATS", | ||
| "SECURITY", | ||
| ]; | ||
|
|
||
| const lines = raw.split("\n"); | ||
|
|
||
| let current: string | null = null; | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
|
|
||
| if (headings.includes(trimmed)) { | ||
| current = trimmed; | ||
| sections[current] = ""; | ||
| continue; | ||
| } | ||
|
|
||
| if (current) { | ||
| sections[current] += line + "\n"; | ||
| } | ||
| } | ||
|
|
||
| for (const key of Object.keys(sections)) { | ||
| sections[key] = sections[key]?.trimEnd() || ""; | ||
| } | ||
|
|
||
| const title = match?.[1]?.trim()?.toLowerCase() ?? term; | ||
| const section = match?.[2] ?? "?"; | ||
| const description = | ||
| sections["DESCRIPTION"]?.split("\n\n")[0]?.replace(/\s+/g, " ").trim() || | ||
| ""; | ||
|
|
||
| return { | ||
| title, | ||
| description, | ||
| url, | ||
| raw, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| interface VersionSection { | ||
| title: string; | ||
| content: string; | ||
| } | ||
|
|
||
| export function parseChangelog(content: string): VersionSection[] { | ||
| if (!content?.trim()) return []; | ||
|
|
||
| const parts = content.split(/^## /m); | ||
| const versions: VersionSection[] = []; | ||
|
|
||
| for (let i = 1; i < parts.length; i++) { | ||
| const section = parts[i]?.trim(); | ||
| if (!section) continue; | ||
|
|
||
| const firstNewline = section.indexOf("\n"); | ||
| const title = ( | ||
| firstNewline === -1 ? section : section.slice(0, firstNewline) | ||
| ).trim(); | ||
| const body = (firstNewline === -1 ? "" : section.slice(firstNewline)) | ||
| .trim() | ||
| .replace(/\r?\n\r?\n/g, "\n"); | ||
|
|
||
| if (title) { | ||
| versions.push({ | ||
| title, | ||
| content: `## ${title}\n\n${body}`, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return versions; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
fetchcall has no timeout or error handling, which can cause the Discord bot to hang indefinitely if the archlinux.org server is slow or unresponsive. This could lead to resource exhaustion and bot unresponsiveness.Confidence: 5/5
Suggested Fix
Add a 5-second timeout using AbortController to prevent indefinite hangs. Wrap the fetch in try-catch to handle network errors gracefully. This prevents the bot from becoming unresponsive when the external API is slow or down.
Prompt for AI
Copy this prompt to your AI IDE to fix this issue locally: