aboutsummaryrefslogtreecommitdiff
path: root/rolereact/views.go
blob: b18ac86a7dd5da8aef0d7eff18535dcc7f3112ec (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package rolereact

import (
	"context"
	"fmt"
	"slices"

	"git.anhgelus.world/anhgelus/les-copaings-bot/config"
	"git.anhgelus.world/anhgelus/les-copaings-bot/dynamicid"
	"github.com/nyttikord/gokord/channel"
	"github.com/nyttikord/gokord/component"
	"github.com/nyttikord/gokord/discord/types"
	"github.com/nyttikord/gokord/interaction"
)

func MessageModifyComponents(ctx context.Context, i *interaction.Interaction, params *EditID) []component.Message {
	message, ok := GetMessageFromEditID(ctx, i, params.MessageEditID)
	if !ok {
		return []component.Message{
			&component.TextDisplay{Content: "Cette modification est trop vieille et a été oubliée."},
		}
	}
	var note string
	if message.Note != "" {
		note = message.Note
	} else {
		note = "*Pas de note*"
	}
	components := []component.Message{
		&component.TextDisplay{Content: "## Modifier un message de réaction"},
		&component.Separator{},
		&component.Section{
			Components: []component.Message{&component.TextDisplay{Content: note}},
			Accessory: &component.Button{
				Label:    "Modifier",
				Style:    component.ButtonStyleSecondary,
				CustomID: dynamicid.FormatCustomID(SetNote, *params),
			},
		},
		&component.Separator{},
	}
	for _, role := range message.Roles {
		var reaction string
		if role.Reaction != "" {
			reaction = FormatEmoji(role.Reaction)
		} else {
			reaction = ":no_entry_sign:"
		}
		var roleMention string
		if role.RoleID != 0 {
			roleMention = fmt.Sprintf("<@&%d>", role.RoleID)
		} else {
			roleMention = "*Pas de rôle sélectionné*"
		}
		if role.CounterID == 0 {
			role.CounterID = roleCounter
			roleCounter++
		}
		components = append(components, &component.Section{
			Components: []component.Message{&component.TextDisplay{Content: fmt.Sprintf("%s %s", reaction, roleMention)}},
			Accessory: &component.Button{
				Label:    "Modifier",
				Style:    component.ButtonStyleSecondary,
				CustomID: dynamicid.FormatCustomID(OpenRole, EditIDWithRole{params.MessageEditID, role.CounterID}),
			},
		})
	}
	if len(message.Roles) == 0 {
		components = append(components, &component.TextDisplay{
			Content: "*Pas de rôles de réaction défini*",
		})
	}
	components = append(components, []component.Message{
		&component.ActionsRow{
			Components: []component.Message{
				&component.Button{
					Style:    component.ButtonStylePrimary,
					Label:    "Ajouter",
					CustomID: dynamicid.FormatCustomID(NewRole, EditID{MessageEditID: params.MessageEditID}),
					Disabled: len(message.Roles) >= 20,
				},
			},
		},
		&component.Separator{},
		&component.ActionsRow{
			Components: []component.Message{
				&component.Button{
					Label:    "Appliquer",
					Style:    component.ButtonStylePrimary,
					CustomID: dynamicid.FormatCustomID(ApplyMessage, EditID{MessageEditID: params.MessageEditID}),
				},
				&component.Button{
					Label:    "Réinitialiser",
					Style:    component.ButtonStyleDanger,
					CustomID: dynamicid.FormatCustomID(ResetMessage, *params),
				},
				&component.Button{
					Label: "Message",
					Style: component.ButtonStyleLink,
					URL:   fmt.Sprintf("https://discord.com/channels/%d/%d/%d", message.GuildID, message.ChannelID, message.MessageID),
				},
			},
		}}...)
	return []component.Message{
		&component.Container{
			Components: components,
		},
	}
}

func MessageModifyData(ctx context.Context, i *interaction.Interaction, params *EditID) *interaction.ResponseData {
	components := []component.Component{}
	for _, component := range MessageModifyComponents(ctx, i, params) {
		components = append(components, component)
	}
	responseData := &interaction.ResponseData{
		Flags:      channel.MessageFlagsIsComponentsV2 | channel.MessageFlagsEphemeral,
		Components: components,
	}
	return responseData
}

func MessageModifyRoleComponents(ctx context.Context, i *interaction.Interaction, params *EditIDWithRole, emojiMessage string) []component.Message {
	message, ok := GetMessageFromEditID(ctx, i, params.MessageEditID)
	var role *config.RoleReact
	if ok {
		roleIndex := slices.IndexFunc(message.Roles, func(role *config.RoleReact) bool { return role.CounterID == params.RoleCounterID })
		if roleIndex != -1 {
			role = message.Roles[roleIndex]
		}
	}
	if !ok || role == nil {
		return []component.Message{
			&component.TextDisplay{Content: "Impossible de trouver la modification de message. Veuillez réessayer."},
		}
	}
	disableBack := false
	var reactionDescription string
	var reactionButton component.Button
	if role.Reaction != "" {
		reactionDescription = fmt.Sprintf("**Réaction : ** %s", FormatEmoji(role.Reaction))
		reactionButton = component.Button{Label: "Modifier", Style: component.ButtonStyleSecondary}
	} else {
		reactionDescription = "*Aucune réaction pour le moment*"
		reactionButton = component.Button{Label: "Ajouter", Style: component.ButtonStylePrimary}
		disableBack = true
	}
	reactionButton.CustomID = dynamicid.FormatCustomID(SetRoleReaction, *params)
	defaultRoleValues := make([]component.SelectMenuDefaultValue, 0)
	if role.RoleID != 0 {
		defaultRoleValues = append(defaultRoleValues, component.SelectMenuDefaultValue{
			Type: types.SelectMenuDefaultValueRole,
			ID:   role.RoleID,
		})
	}
	disableBack = disableBack || (role.RoleID == 0)
	one := 1
	components := []component.Message{
		&component.TextDisplay{Content: "## Modifier un message de réaction"},
		&component.Separator{},
		&component.Section{
			Components: []component.Message{
				&component.TextDisplay{Content: reactionDescription},
			},
			Accessory: &reactionButton,
		},
	}
	if emojiMessage != "" {
		components = append(components, &component.TextDisplay{Content: "-# " + emojiMessage})
	}
	components = append(components,
		[]component.Message{
			&component.ActionsRow{Components: []component.Message{
				&component.SelectMenu{
					MenuType:  types.SelectMenuRole,
					CustomID:  dynamicid.FormatCustomID(SetRoleRoleID, *params),
					MinValues: &one, MaxValues: 1,
					Placeholder:   "Sélectionner un rôle",
					DefaultValues: defaultRoleValues,
				},
			}},
			&component.ActionsRow{Components: []component.Message{
				&component.Button{
					Style:    component.ButtonStyleDanger,
					Label:    "Supprimer",
					CustomID: dynamicid.FormatCustomID(DelRole, *params),
				},
			}},
			&component.Separator{},
			&component.ActionsRow{Components: []component.Message{
				&component.Button{
					Label:    "Retour",
					Style:    component.ButtonStyleSecondary,
					Disabled: disableBack,
					CustomID: dynamicid.FormatCustomID(OpenMessage, EditID{MessageEditID: params.MessageEditID}),
				},
				&component.Button{
					Label: "Message", Style: component.ButtonStyleLink,
					URL: fmt.Sprintf("https://discord.com/channels/%d/%d/%d", message.GuildID, message.ChannelID, message.MessageID),
				},
			}},
		}...)
	return []component.Message{&component.Container{
		Components: components,
	}}
}

func MessageModifyRoleData(ctx context.Context, i *interaction.Interaction, params *EditIDWithRole, emojiMessage string) interaction.ResponseData {
	components := []component.Component{}
	for _, component := range MessageModifyRoleComponents(ctx, i, params, emojiMessage) {
		components = append(components, component)
	}
	return interaction.ResponseData{
		Flags:      channel.MessageFlagsEphemeral | channel.MessageFlagsIsComponentsV2,
		Components: components,
	}
}