forked from iqeq1945/discordbot-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
153 lines (129 loc) · 4.08 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
Client,
GatewayIntentBits,
ActionRowBuilder,
ButtonBuilder,
MessageActionRowComponentBuilder,
} from "discord.js";
import * as sheet from "./sheet";
import * as dotenv from "dotenv";
import { searchModal } from "./modal";
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent, // 추가
],
});
const PREFIX = process.env.PREFIX || "!";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
if (!process.env.TOKEN) {
console.log("An discord token is empty.");
sleep(60000).then(() =>
console.log("Service is getting stopped automatically")
);
}
const discordLogin = async () => {
try {
await client.login(process.env.TOKEN);
} catch (error) {
console.log("An invalid token was provided");
sleep(60000).then(() =>
console.log("Service is getting stopped automatically")
);
}
};
discordLogin();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}.`);
});
client.on("messageCreate", async (msg) => {
if (msg.author.bot) return;
try {
if (msg.content === PREFIX + "server") {
msg.channel.send(
`현재 서버의 이름은 ${msg.guild.name} 입니다.\n총 멤버 수는 ${msg.guild.memberCount} 명 입니다.`
);
}
if (msg.content === PREFIX + "get/count") {
const data = await sheet.list();
msg.channel.send(`총 ${data.length} 개가 등록되어있어요!`);
}
if (msg.content === PREFIX + "search") {
const button = new ButtonBuilder()
.setCustomId("openModal")
.setLabel("모달 오픈!")
.setStyle(1);
const actionRow =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
button
);
await msg.reply({
content: "버튼을 눌러 모달을 열어보세요.",
components: [actionRow],
});
}
if (msg.content === PREFIX + "find") {
// find 명령어 구현
}
if (msg.content === PREFIX + "info") {
console.log(msg);
}
} catch (e) {
console.log(e);
}
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isButton() && interaction.customId === "openModal") {
await interaction.showModal(searchModal());
}
if (interaction.isModalSubmit() && interaction.customId === "searchModal") {
const keyword = interaction.fields.getTextInputValue("keywordInput");
if (!keyword) return interaction.reply("단어를 입력하세요!");
// 여기서 기본 응답을 지연
await interaction.deferReply();
try {
const _keyword = keyword.split("").join("");
const data = await sheet.list();
const results = data.filter((item) => item.keyword === _keyword);
if (results.length > 0) {
let replyMessage = `검색결과 : ${results.length}개\n\n`;
results.forEach((result, index) => {
replyMessage += `
단어: ${result.keyword}
뜻: ${result.mean}
정보: ${result.info}
태그: ${result.tag.join(", ")}
URL: ${result.url ? result.url.join(", ") : "No URL"}
BGM: ${result.bgm ? voiceURL(result.bgm) : "No URL"}
Type: ${result.type}\n\n`;
});
if (replyMessage.length > 2000) {
replyMessage =
"Too many results to display in one message. Please refine your search.";
}
await interaction.editReply(replyMessage);
} else {
await interaction.editReply(`해당하는 단어가 없어유: ${keyword}`);
}
} catch (error) {
console.error("Error fetching data:", error);
await interaction.editReply("There was an error fetching the data.");
}
}
});
const voiceURL = (bgm) => {
const type = bgm.split("-")[0];
const map = new Map([
["gomem", "gomem/"],
["woo", "woo/"],
["i", "ine/"],
["l", "lilpa/"],
["ju", "jururu/"],
["ji", "jingburger/"],
["go", "gosegu/"],
["v", "viichan/"],
]);
return "https://r2.wakttu.kr/assets/voice/" + map.get(type) + bgm + ".webm";
};