Skip to content

Commit 24f9337

Browse files
committed
✨ :: MSG 멤버 검색 기능 추가
1 parent 9364a5b commit 24f9337

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/MSGSaltBot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MySaltCommand from "./commands/MySaltCommand";
77
import SaltRankCommand from "./commands/SaltRankCommand";
88
import HelpCommand from "./commands/HelpCommand";
99
import DMCommand from "./commands/DMCommand";
10+
import SearchMSGMemberCommand from "./commands/SearchMSGMemberCommand";
1011

1112
export class MSGSaltBot {
1213
private slashCommandMap = new Map<string, Command>();
@@ -37,7 +38,8 @@ export class MSGSaltBot {
3738
HelpCommand,
3839
MySaltCommand,
3940
SaltRankCommand,
40-
DMCommand
41+
DMCommand,
42+
SearchMSGMemberCommand
4143
];
4244

4345
this.slashCommandMap = slashCommands.reduce((map, command) => {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import {
2+
ActionRowBuilder,
3+
ButtonBuilder,
4+
ButtonStyle,
5+
ChatInputCommandInteraction,
6+
EmbedBuilder,
7+
GuildMember,
8+
SlashCommandBuilder
9+
} from "discord.js";
10+
import { Command } from "src/interfaces/Command";
11+
12+
export default {
13+
data: new SlashCommandBuilder()
14+
.setName("members")
15+
.setDescription("MSG에 있는 팀원들의 유저 정보를 가져와요!")
16+
.addRoleOption((option) =>
17+
option
18+
.setName("role")
19+
.setDescription("특정 Role을 가진 팀원들의 유저정보만 가져올 수 있어요.")
20+
.setRequired(false)
21+
),
22+
async execute(interaction: ChatInputCommandInteraction) {
23+
if (!interaction.guild) return;
24+
const roleOption = interaction.options.getRole("role");
25+
try {
26+
let members: GuildMember[];
27+
if (roleOption) {
28+
const allMembers = await interaction.guild.members.fetch();
29+
members = Array.from(
30+
allMembers.filter((user) => {
31+
return user.roles.cache.has(roleOption.id);
32+
})
33+
).map((u) => u[1]);
34+
} else {
35+
members = Array.from(await interaction.guild.members.fetch()).map((u) => u[1]);
36+
}
37+
members = members.filter((m) => !m.user.bot);
38+
39+
const prevID = "PREV";
40+
const prevButton = new ButtonBuilder()
41+
.setStyle(ButtonStyle.Secondary)
42+
.setLabel("Prev")
43+
.setEmoji("⬅️")
44+
.setCustomId(prevID);
45+
46+
const nextID = "NEXT";
47+
const nextButton = new ButtonBuilder()
48+
.setStyle(ButtonStyle.Secondary)
49+
.setLabel("Next")
50+
.setEmoji("➡️")
51+
.setCustomId(nextID);
52+
53+
const generateEmbed: (index: number) => EmbedBuilder = (index) => {
54+
const current = members.slice(index, index + 10);
55+
return new EmbedBuilder()
56+
.setTitle(`유저 정보 Page ${index / 10 + 1}`)
57+
.setDescription("MSG 팀원들의 role, username을 알려줘요")
58+
.setColor(0x70a1cd)
59+
.setTimestamp()
60+
.setFields(
61+
current.map((member) => {
62+
return {
63+
name: member.user.displayName,
64+
value: `
65+
username: ${member.user.username}
66+
roles: ${member.roles.cache.map((r) => r.name).join(", ")}
67+
`
68+
};
69+
})
70+
);
71+
};
72+
73+
const canFitOnOnePage = members.length <= 10;
74+
const row: ActionRowBuilder<ButtonBuilder> = new ActionRowBuilder({
75+
components: canFitOnOnePage ? [] : [nextButton]
76+
});
77+
78+
const embedMessage = await interaction.reply({
79+
content: `usernames: ${members.map((u) => `\`${u.user.username}\``).join(", ")}`,
80+
embeds: [generateEmbed(0)],
81+
components: [row]
82+
});
83+
if (canFitOnOnePage) return;
84+
85+
const collector = embedMessage.createMessageComponentCollector({
86+
filter: ({ user }) => user.id === interaction.user.id
87+
});
88+
let currentIndex = 0;
89+
collector.on("collect", async (collectInteraction) => {
90+
collectInteraction.customId === prevID ? (currentIndex -= 10) : (currentIndex += 10);
91+
await collectInteraction.update({
92+
content: `usernames: ${members.map((u) => `\`${u.user.username}\``).join(", ")}`,
93+
embeds: [generateEmbed(currentIndex)],
94+
components: [
95+
new ActionRowBuilder<ButtonBuilder>({
96+
components: [
97+
...(currentIndex ? [prevButton] : []),
98+
...(currentIndex + 10 < members.length ? [nextButton] : [])
99+
]
100+
})
101+
]
102+
});
103+
});
104+
} catch (error) {
105+
await interaction.reply({
106+
content: `${error}`
107+
});
108+
}
109+
}
110+
} as Command;

0 commit comments

Comments
 (0)