Skip to content

Commit

Permalink
ボタンの文字数制限に対策
Browse files Browse the repository at this point in the history
  • Loading branch information
minarin0179 committed Oct 25, 2023
1 parent c0e2e24 commit 4d0c3c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/components/buttons/select.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { ButtonBuilder, ButtonStyle, Guild, Role } from "discord.js";
import { Button } from "../../structures/Button";
import { LimitLength } from "../../utils/LimitLength";

export default new Button({
customId: "select",
build: ({ choices }: { choices: (string | Role)[]; guild: Guild }) =>
choices.map((choice, index) => {
if (choice instanceof Role)
return new ButtonBuilder()
.setCustomId(`select:${index},${choice.name},${choice.id}`)
.setLabel(choice.name)
.setStyle(ButtonStyle.Primary);
else {
return new ButtonBuilder()
.setCustomId(`select:${index},${choice}`)
.setLabel(choice)
.setStyle(ButtonStyle.Primary);
}
const isRole = choice instanceof Role;
const label = LimitLength(isRole ? choice.name : choice, 16);

return new ButtonBuilder()
.setCustomId(`select:${index},${label},${isRole ? choice.id : ""}`)
.setLabel(label)
.setStyle(ButtonStyle.Primary);
}),
execute: async () => {
/* Collectorで処理するのでこちらからは何もしない */
Expand Down
3 changes: 2 additions & 1 deletion src/components/buttons/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { Button } from "../../structures/Button";
import { fetchAllMessages } from "../../utils/FetchAllMessages";
import { reply } from "../../utils/Reply";
import { transferMessage } from "../../utils/transferMessage";
import { LimitLength } from "../../utils/LimitLength";

export default new Button({
customId: "transfer",
build: ({ destination }: { destination: GuildTextBasedChannel }) => [
new ButtonBuilder()
.setCustomId(`transfer:${destination.id}`)
.setLabel(`「#${destination.name}」へ転送`)
.setLabel(`「#${LimitLength(destination.name, 32)}」へ転送`)
.setStyle(ButtonStyle.Primary),
],
execute: async ({ interaction, args }) => {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/LimitLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LimitLength = (str: string, length: number): string => {
if (str.length > length) return str.slice(0, length - 1) + "…";
return str;
};

0 comments on commit 4d0c3c4

Please sign in to comment.