Skip to content

Commit

Permalink
refactor: change to advanced handlers (#36)
Browse files Browse the repository at this point in the history
Since simple handlers are getting deprecated and soon removed im making
this PR to update the bot.

There are some "issues" regarding message commands, given that im not
sure yet if advanced handlers will ever have message commands, i will
update on the matter when i can, so for now we still use simple handlers
for that.
  • Loading branch information
Didas-git authored Jul 19, 2024
1 parent 53f730b commit b0438f2
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 265 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"bun-types": "^1.1.6"
},
"dependencies": {
"@lilybird/handlers": "^0.4.0",
"@lilybird/jsx": "0.2.0",
"@lilybird/transformers": "^0.2.0",
"@lilybird/handlers": "^0.6.0-beta.9",
"@lilybird/jsx": "0.3.0",
"@lilybird/transformers": "^0.4.1",
"@paperdave/logger": "^3.0.1",
"@purplet/serialize": "^2.0.0",
"@wolfram-alpha/wolfram-alpha-api": "^23.1004.144821-RELEASE",
"algoliasearch": "^4.23.2",
"bun-tracestrings": "github:oven-sh/bun.report",
"gray-matter": "^4.0.3",
"lilybird": "^0.7.1-alpha.0"
"lilybird": "^0.7.3"
}
}
}
50 changes: 24 additions & 26 deletions src/commands/docs.tsx → src/commands/docs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
ApplicationCommand as JSXApplicationCommand,
StringOption,
UserOption,
} from "@lilybird/jsx";
import { ApplicationCommand } from "@lilybird/handlers";
import { AllowedMentionType, ApplicationCommandOptionType } from "lilybird";
import { $applicationCommand } from "@lilybird/handlers/advanced";
import algoliasearch from "algoliasearch";
import { safeSlice } from "src/util.ts";

Expand All @@ -14,19 +10,22 @@ const algoliaClient = algoliasearch(
);
const algoliaIndex = algoliaClient.initIndex("bun");

export default {
post: "GLOBAL",
data: (
<JSXApplicationCommand name="docs" description="Search at docs">
<StringOption
name="query"
description="Select query"
required
autocomplete
/>
<UserOption name="target" description="User to mention" />
</JSXApplicationCommand>
),
$applicationCommand({
name: "docs",
description: "Search at docs",
options: [
{
type: ApplicationCommandOptionType.STRING,
name: "query",
description: "Select query",
required: true,
autocomplete: true,
}, {
type: ApplicationCommandOptionType.USER,
name: "target",
description: "User to mention"
}
],
autocomplete: async (interaction) => {
const query = interaction.data.getFocused<string>().value;
const result = await algoliaIndex.search(query, {
Expand All @@ -44,7 +43,7 @@ export default {
})
);
},
run: async (interaction) => {
handle: async (interaction) => {
await interaction.deferReply();

const query = interaction.data.getString("query");
Expand All @@ -69,21 +68,20 @@ export default {
snippetContent ? snippetContent : "",
notice
? notice
.split("\n")
.map((s: any) => `> ${s}`)
.join("\n")
.split("\n")
.map((s: any) => `> ${s}`)
.join("\n")
: "",
].join("\n");

// @ts-expect-error allowed_mentions
await interaction.editReply({
content,
allowed_mentions: {
parse: target ? ["users"] : [],
parse: target ? [AllowedMentionType.UserMentions] : [],
},
});
},
} satisfies ApplicationCommand;
});

function getHitName(hit: any) {
const type = hit.hierarchy.lvl0 === "Documentation" ? "📖" : "🗺️";
Expand Down
103 changes: 48 additions & 55 deletions src/commands/github.tsx → src/commands/github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
ApplicationCommand as JSXApplicationCommand,
BooleanOption,
CommandOptions,
StringOption,
} from "@lilybird/jsx";
import { ApplicationCommand } from "@lilybird/handlers";

import { $applicationCommand } from "@lilybird/handlers/advanced";
import { ApplicationCommandOptionType } from "lilybird";
import { safeSlice, silently } from "../util.ts";

type State =
Expand Down Expand Up @@ -37,56 +33,57 @@ interface Item {
};
}

export default {
post: "GLOBAL",
data: (
<JSXApplicationCommand
name="github"
description="Query an issue, pull request or direct link to issue, pull request"
>
<StringOption
name="query"
description="Issue/Pull request number or name"
autocomplete
required
max_length={100}
/>
<StringOption name="state" description="Issue or Pull request state">
<CommandOptions name="🔴🟠 Open" value="open" />
<CommandOptions
name="🟢 Closed as completed"
value="closed_as_completed"
/>
<CommandOptions
name="⚪️ Closed as not planned"
value="closed_as_not_planned"
/>
<CommandOptions name="⚫️ Closed" value="closed" />
<CommandOptions name="🟣 Merged" value="merged" />
<CommandOptions name="📝 Draft" value="draft" />
<CommandOptions name="🌍 All" value="all" />
</StringOption>
<StringOption name="type" description="Issue/Pull request number or name">
<CommandOptions name="🐛 Issues" value="issues" />
<CommandOptions name="🔨 Pull Requests" value="pull_requests" />
<CommandOptions name="🌍 Both" value="both" />
</StringOption>
<BooleanOption name="hide" description="Show this message only for you" />
</JSXApplicationCommand>
),
run: async (interaction) => {
$applicationCommand({
name: "github",
description: "Query an issue, pull request or direct link to issue, pull request",
options: [
{
type: ApplicationCommandOptionType.STRING,
name: "query",
description: "Issue/Pull request number or name",
autocomplete: true,
required: true,
max_length: 100
}, {
type: ApplicationCommandOptionType.STRING,
name: "state",
description: "Issue or Pull request state",
choices: [
{ name: "🔴🟠 Open", value: "open" },
{ name: "🟢 Closed as completed", value: "closed_as_completed" },
{ name: "⚪️ Closed as not planned", value: "closed_as_not_planned" },
{ name: "⚫️ Closed", value: "closed" },
{ name: "🟣 Merged", value: "merged" },
{ name: "📝 Draft", value: "draft" },
{ name: "🌍 All", value: "all" }
]
}, {
type: ApplicationCommandOptionType.STRING,
name: "type",
description: "Issue/Pull request number or name",
choices: [
{ name: "🐛 Issues", value: "issues" },
{ name: "🔨 Pull Requests", value: "pull_requests" },
{ name: "🌍 Both", value: "both" }
],
},
{
type: ApplicationCommandOptionType.BOOLEAN,
name: "hide",
description: "Show this message only for you"
}
],
handle: async (interaction) => {
const hide = interaction.data.getBoolean("hide") ?? false;

await interaction.deferReply(hide);

const query = interaction.data.getString("query", true);
const state: State =
(interaction.data.getString("state") as State) || "all";
const state: State = (interaction.data.getString("state") as State) || "all";
const type: Type = (interaction.data.getString("type") as Type) || "both";

const result = (await search(query, state, type))[0];
if (!result) {
// @ts-expect-error allowed_mentions
interaction.editReply({
content: `❌ Couldn't find issue or pull request \`${query}\``,
allowed_mentions: {
Expand All @@ -96,13 +93,10 @@ export default {
return;
}

// @ts-expect-error allowed_mentions
interaction.editReply({
content: [
`${result.emoji.type} ${result.emoji.state} [#${
result.number
} in oven-sh/bun](<${result.html_url}>) by [${result.user.login}](<${
result.user.html_url
`${result.emoji.type} ${result.emoji.state} [#${result.number
} in oven-sh/bun](<${result.html_url}>) by [${result.user.login}](<${result.user.html_url
}>) ${stateToText(result)} ${stateToTimestamp(result)}`,
result.title,
].join("\n"),
Expand All @@ -111,7 +105,6 @@ export default {
},
});
},

autocomplete: async (interaction) => {
const query = interaction.data.getFocused<string>().value;
const state: State =
Expand All @@ -132,7 +125,7 @@ export default {
)
);
},
} satisfies ApplicationCommand;
});

function stateToText(item: Item) {
switch (item.emoji.state) {
Expand Down
57 changes: 27 additions & 30 deletions src/commands/mdn.tsx → src/commands/mdn.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// https://github.com/discordjs/discord-utils-bot/blob/main/src/functions/autocomplete/mdnAutoComplete.ts#L23-L47 thanks
// https://github.com/discordjs/discord-utils-bot/blob/main/src/functions/mdn.ts#L59C1-L78C3 thanks

import {
BooleanOption,
ApplicationCommand as JSXApplicationCommand,
StringOption,
UserOption,
} from "@lilybird/jsx";
import { ApplicationCommand } from "@lilybird/handlers";
import { ApplicationCommandOptionType, AllowedMentionType } from "lilybird";
import { $applicationCommand } from "@lilybird/handlers/advanced";
import { MDN_API, MDN_DISCORD_EMOJI } from "src/constants.ts";
import { safeSlice, silently } from "src/util.ts";

Expand Down Expand Up @@ -43,25 +37,29 @@ const MDN_DATA = (await (

const cache = new Map<string, Document>();

export default {
post: "GLOBAL",
data: (
<JSXApplicationCommand
name="mdn"
description="Search the Mozilla Developer Network documentation"
>
<StringOption
name="query"
description="Class or method to search for"
required
autocomplete
max_length={100}
/>
<UserOption name="target" description="User to mention" />
<BooleanOption name="hide" description="Show this message only for you" />
</JSXApplicationCommand>
),
run: async (interaction) => {
$applicationCommand({

name: "mdn",
description: "Search the Mozilla Developer Network documentation",
options: [
{
type: ApplicationCommandOptionType.STRING,
name: "query",
description: "Class or method to search for",
required: true,
autocomplete: true,
max_length: 100
}, {
type: ApplicationCommandOptionType.USER,
name: "target",
description: "User to mention"
}, {
type: ApplicationCommandOptionType.BOOLEAN,
name: "hide",
description: "Show this message only for you"
}
],
handle: async (interaction) => {
const hide = interaction.data.getBoolean("hide") ?? false;

await interaction.deferReply(hide);
Expand Down Expand Up @@ -101,14 +99,13 @@ export default {
intro,
];

// @ts-expect-error allowed_mentions
await interaction.editReply({
content: [
target ? `*Suggestion for <@${target}>:*\n` : "",
parts.join("\n"),
].join("\n"),
allowed_mentions: {
parse: target ? ["users"] : [],
parse: target ? [AllowedMentionType.UserMentions] : [],
},
});
},
Expand Down Expand Up @@ -151,7 +148,7 @@ export default {
)
);
},
} satisfies ApplicationCommand;
});

function escape(text: string) {
return text.replaceAll("||", "|\u200B|").replaceAll("*", "\\*");
Expand Down
Loading

0 comments on commit b0438f2

Please sign in to comment.