-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchChannelForThread.go
156 lines (140 loc) · 4.93 KB
/
watchChannelForThread.go
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
154
155
156
package main
import (
"log/slog"
"time"
"github.com/bwmarrin/discordgo"
"github.com/itsvyle/hxi_bot/config"
)
type ServiceWatchChannels struct {
config config.ConfigSchemaJsonChannelThreadsWatcherServicesElem
}
func CreateNewServiceWatchChannels(config config.ConfigSchemaJsonChannelThreadsWatcherServicesElem) *ServiceWatchChannels {
return &ServiceWatchChannels{
config: config,
}
}
func (s *ServiceWatchChannels) InitWatchChannelForThread(discordSession *discordgo.Session) {
channelID := s.config.ChannelId
discordSession.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.ChannelID != channelID {
return
}
if message.Author.ID == session.State.User.ID {
return
}
time.Sleep(500 * time.Millisecond)
thread, err := session.MessageThreadStartComplex(message.ChannelID, message.ID, &discordgo.ThreadStart{
Name: message.Content,
AutoArchiveDuration: 24 * 60,
})
if err != nil {
slog.With("error", err).Error("Error creating thread")
return
}
_, err = session.ChannelMessageSendComplex(thread.ID, &discordgo.MessageSend{
Content: "Clique sur le boutton pour changer le nom du fil!",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Style: discordgo.SuccessButton,
Label: "Renommer le fil",
CustomID: "thread_rename",
},
},
},
},
})
if err != nil {
slog.With("error", err, "threadID", thread.ID).Error("Error sending message to thread")
return
}
})
if !s.config.SendRenameButton {
slog.With("channelID", channelID).Info("Initialized watching channel for messages to create threads in channel", "renameButton", false)
return
}
// Handle button clicks
discordSession.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if interaction.Type != discordgo.InteractionMessageComponent {
return
}
customID := interaction.MessageComponentData().CustomID
if customID != "thread_rename" {
return
}
sourceChannel, err := session.Channel(interaction.ChannelID)
if err != nil {
slog.With("error", err, "channelID", interaction.ChannelID).Error("Error getting source channel")
return
}
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
CustomID: "modal_thread_rename_" + interaction.ChannelID, // Unique ID so the previous value doesn't automatically get filled in
Title: "Renommer le thread",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "name",
Label: "Titre du thread",
Placeholder: "Nouveau titre:",
Style: discordgo.TextInputParagraph,
Required: true,
Value: sourceChannel.Name,
},
},
},
},
},
})
if err != nil {
slog.With("error", err).Error("Error presenting rename modal for user")
return
}
})
// Handle modal submit
discordSession.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if interaction.Type != discordgo.InteractionModalSubmit {
return
}
customID := interaction.ModalSubmitData().CustomID
if customID != "modal_thread_rename_"+interaction.ChannelID {
return
}
newName := interaction.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
if newName == "" {
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Le nom du thread n'a pas été modifié.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
_, err := session.ChannelEdit(interaction.ChannelID, &discordgo.ChannelEdit{
Name: newName,
})
if err != nil {
slog.With("error", err, "channelID", interaction.ChannelID).Error("Error renaming thread")
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Le nom du thread n'a pas pu etre modifié, il y a eu une erreur.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Le nom du thread a été modifié avec succès.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
})
slog.With("channelID", channelID).Info("Initialized watching channel for messages to create threads in channel", "renameButton", true)
}