feat: add AllowedMentions

This commit is contained in:
Siwoo Jeon 2025-05-18 17:57:32 +09:00
parent 20c0debf4a
commit 7390cc31ed
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 44 additions and 38 deletions

View file

@ -14,7 +14,6 @@ import (
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
func argParser(content string) (args []string) {
@ -109,9 +108,6 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
go func() {
cur, err := databases.Database.Learns.Find(context.TODO(), bson.D{{Key: "command", Value: content}})
if err != nil {
if err == mongo.ErrNilDocument {
learnData = []databases.Learn{}
}
log.Fatalln(err)
}
@ -131,27 +127,27 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
user, _ := s.User(data.UserId)
result := resultParser(data.Result, s, m)
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
Reference: m.Reference(),
Content: fmt.Sprintf("%s\n%s", result, utils.InlineCode(fmt.Sprintf("%s님이 알려주셨어요.", user.Username))),
AllowedMentions: &discordgo.MessageAllowedMentions{
utils.NewMessageSender(m).
SetContent(fmt.Sprintf("%s\n%s", result, utils.InlineCode(fmt.Sprintf("%s님이 알려주셨어요.", user.Username)))).
SetAllowedMentions(discordgo.MessageAllowedMentions{
Roles: []string{},
Parse: []discordgo.AllowedMentionType{},
Users: []string{},
},
})
}).
SetReply(true).
Send()
return
}
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
Reference: m.Reference(),
Content: data[rand.Intn(len(data))].Text,
AllowedMentions: &discordgo.MessageAllowedMentions{
utils.NewMessageSender(m).
SetContent(data[rand.Intn(len(data))].Text).
SetAllowedMentions(discordgo.MessageAllowedMentions{
Roles: []string{},
Parse: []discordgo.AllowedMentionType{},
Users: []string{},
},
})
}).
SetReply(true).
Send()
return
}

26
main.go
View file

@ -21,6 +21,20 @@ import (
"github.com/devproje/commando/types"
)
func init() {
go commands.Discommand.LoadCommand(commands.HelpCommand)
go commands.Discommand.LoadCommand(commands.DataLengthCommand)
go commands.Discommand.LoadCommand(commands.LearnCommand)
go commands.Discommand.LoadCommand(commands.LearnedDataListCommand)
go commands.Discommand.LoadCommand(commands.InformationCommand)
go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand)
go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent)
go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)
go commands.Discommand.LoadModal(modals.PaginationEmbedModal)
}
func main() {
command := commando.NewCommando(os.Args[1:])
config := configs.Config
@ -68,18 +82,6 @@ func main() {
dg, _ := discordgo.New("Bot " + config.Bot.Token)
go commands.Discommand.LoadCommand(commands.HelpCommand)
go commands.Discommand.LoadCommand(commands.DataLengthCommand)
go commands.Discommand.LoadCommand(commands.LearnCommand)
go commands.Discommand.LoadCommand(commands.LearnedDataListCommand)
go commands.Discommand.LoadCommand(commands.InformationCommand)
go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand)
go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent)
go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)
go commands.Discommand.LoadModal(modals.PaginationEmbedModal)
go dg.AddHandler(handler.MessageCreate)
go dg.AddHandler(handler.InteractionCreate)

View file

@ -8,12 +8,13 @@ type MessageCreate struct {
}
type MessageSender struct {
Embeds []*discordgo.MessageEmbed
Content string
Components []discordgo.MessageComponent
Ephemeral bool
Reply bool
m any
Embeds []*discordgo.MessageEmbed
Content string
Components []discordgo.MessageComponent
Ephemeral bool
Reply bool
AllowedMentions *discordgo.MessageAllowedMentions
m any
}
func NewMessageSender(m any) *MessageSender {
@ -45,6 +46,11 @@ func (s *MessageSender) SetReply(reply bool) *MessageSender {
return s
}
func (s *MessageSender) SetAllowedMentions(allowedMentions discordgo.MessageAllowedMentions) *MessageSender {
s.AllowedMentions = &allowedMentions
return s
}
func (s *MessageSender) Send() {
switch m := s.m.(type) {
case *MessageCreate:
@ -55,10 +61,11 @@ func (s *MessageSender) Send() {
}
m.Session.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
Content: s.Content,
Embeds: s.Embeds,
Components: s.Components,
Reference: reference,
Content: s.Content,
Embeds: s.Embeds,
Components: s.Components,
AllowedMentions: s.AllowedMentions,
Reference: reference,
})
return
case *InteractionCreate:
@ -83,5 +90,6 @@ func (s *MessageSender) Send() {
Components: s.Components,
Flags: flags,
})
return
}
}