feat: Add learnedDataList command
This commit is contained in:
parent
0779a4d321
commit
1e1c5ce561
4 changed files with 135 additions and 2 deletions
|
@ -39,20 +39,22 @@ func new() *DiscommandStruct {
|
|||
go discommand.loadCommands(HelpCommand)
|
||||
go discommand.loadCommands(DataLengthCommand)
|
||||
go discommand.loadCommands(LearnCommand)
|
||||
go discommand.loadCommands(LearnedDataListCommand)
|
||||
|
||||
go discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun)
|
||||
go discommand.addMessageRun(DataLengthCommand.Name, DataLengthCommand.dataLengthMessageRun)
|
||||
go discommand.addMessageRun(LearnCommand.Name, LearnCommand.learnMessageRun)
|
||||
go discommand.addMessageRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListMessageRun)
|
||||
|
||||
go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun)
|
||||
go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun)
|
||||
go discommand.addChatInputRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListChatInputRun)
|
||||
return &discommand
|
||||
}
|
||||
|
||||
func (d *DiscommandStruct) loadCommands(command *Command) {
|
||||
d.Commands[command.Name] = command
|
||||
d.Aliases[command.Name] = command.Name
|
||||
// fmt.Println(command.Name)
|
||||
command.discommand = d
|
||||
|
||||
for _, alias := range command.Aliases {
|
||||
|
|
123
commands/learnedDataList.go
Normal file
123
commands/learnedDataList.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/databases"
|
||||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
var LearnedDataListCommand *Command = &Command{
|
||||
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||
Type: discordgo.ChatApplicationCommand,
|
||||
Name: "리스트",
|
||||
Description: "당신이 가ㄹ르쳐준 단어를 나열해요.",
|
||||
},
|
||||
Aliases: []string{"list", "목록", "지식목록"},
|
||||
DetailedDescription: &DetailedDescription{
|
||||
Usage: "머핀아 리스트",
|
||||
},
|
||||
}
|
||||
|
||||
func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
|
||||
for _, data := range *datas {
|
||||
descriptions = append(descriptions, "- "+data.Command+": "+data.Result)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListRun(s *discordgo.Session, m any) {
|
||||
var userId, globalName, avatarUrl string
|
||||
var datas []databases.Learn
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
userId = m.Author.ID
|
||||
globalName = m.Author.GlobalName
|
||||
avatarUrl = m.Author.AvatarURL("512")
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
userId = m.Member.User.ID
|
||||
globalName = m.Member.User.GlobalName
|
||||
avatarUrl = m.User.AvatarURL("512")
|
||||
}
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "당신은 지식ㅇ을 가르쳐준 적이 없어요!",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(err)
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "데이터를 가져오는데 실패했어요.",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
defer cur.Close(context.TODO())
|
||||
|
||||
cur.All(context.TODO(), &datas)
|
||||
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: globalName + "님이 알려주신 지식",
|
||||
Description: utils.CodeBlockWithLanguage("md", "# 총 "+strconv.Itoa(len(datas))+"개에요.\n"+strings.Join(getDescriptions(&datas), "\n")),
|
||||
Color: int(utils.EDefault),
|
||||
Thumbnail: &discordgo.MessageEmbedThumbnail{
|
||||
URL: avatarUrl,
|
||||
},
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
c.learnedDataListRun(s, m)
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
c.learnedDataListRun(s, i)
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
package configs
|
||||
|
||||
var MUFFIN_VERSION = "0.0.0-gopher_canary.250328a"
|
||||
var MUFFIN_VERSION = "0.0.0-gopher_canary.250329a"
|
||||
|
|
|
@ -3,3 +3,11 @@ package utils
|
|||
func InlineCode(str string) string {
|
||||
return "`" + str + "`"
|
||||
}
|
||||
|
||||
func CodeBlockWithLanguage(language string, content string) string {
|
||||
return "```" + language + "\n" + content + "\n" + "```"
|
||||
}
|
||||
|
||||
func CodeBlock(content string) string {
|
||||
return "```\n" + content + "\n" + "```"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue