Fix: deleteLearn's item id

This commit is contained in:
Siwoo Jeon 2024-09-25 19:57:15 +09:00
parent bbf0b0cd08
commit 1af47ed911
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 17 additions and 14 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "muffinbot", "name": "muffinbot",
"version": "3.1.0-cake.d240924a", "version": "3.1.0-cake.d240925a",
"main": "dist/index.js", "main": "dist/index.js",
"private": true, "private": true,
"dependencies": { "dependencies": {

View file

@ -28,7 +28,8 @@ class DeleteLearnCommand extends Command {
const command = await args.rest('string').catch(() => null) const command = await args.rest('string').catch(() => null)
const options: SelectMenuComponentOptionData[] = [] const options: SelectMenuComponentOptionData[] = []
const db = this.container.database const db = this.container.database
const [datas] = await db.database.execute<LearnData[]>( const deleteDataList: string[] = []
const [deleteDatas] = await db.database.execute<LearnData[]>(
'SELECT * FROM learn WHERE command = ? AND user_id = ?;', 'SELECT * FROM learn WHERE command = ? AND user_id = ?;',
[command, msg.author.id], [command, msg.author.id],
) )
@ -39,27 +40,24 @@ class DeleteLearnCommand extends Command {
) )
} }
if (!datas) { if (!deleteDatas) {
return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.') return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.')
} }
datas.forEach(data => { for (let i = 1; i <= deleteDatas.length; i++) {
console.log(data) deleteDataList.push(`${i}. ${deleteDatas[i - 1].result}`)
options.push({ options.push({
label: `${data.id}`, label: `${i}번 지식`,
value: `${CUSTOM_ID}-${data.id}`, value: `${CUSTOM_ID}-${deleteDatas[i - 1].id}`,
description: data.result.slice(0, 100), description: deleteDatas[i - 1].result.slice(0, 100),
})
}) })
}
await msg.reply({ await msg.reply({
embeds: [ embeds: [
{ {
title: '삭제', title: '삭제',
description: `${codeBlock( description: `${codeBlock('md', deleteDataList.join('\n'))}`,
'md',
datas.map(data => `${data.id}. ${data.result}`).join('\n'),
)}`,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}, },
], ],

View file

@ -30,6 +30,11 @@ class DeleteLearnHandler extends InteractionHandler {
const id = interaction.values[0].slice(`${this._CUSTOM_ID}-`.length) const id = interaction.values[0].slice(`${this._CUSTOM_ID}-`.length)
const db = this.container.database const db = this.container.database
const decimalRegexp = /^[0-9]/g
const itemId = interaction.component.options.map(item =>
item.value.endsWith(id) ? item.label.match(decimalRegexp)![0] : null,
)
await db.learn.delete(id) await db.learn.delete(id)
@ -37,7 +42,7 @@ class DeleteLearnHandler extends InteractionHandler {
embeds: [ embeds: [
{ {
title: '삭제', title: '삭제',
description: `${id}번을 정상적으로 삭제하ㅇ였어요.`, description: `${Number(itemId!)}번을 정상적으로 삭제하ㅇ였어요.`,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}, },
], ],