-
Notifications
You must be signed in to change notification settings - Fork 1
/
ping.go
108 lines (96 loc) · 3.3 KB
/
ping.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
package applications
import (
"fmt"
"github.com/aussiebroadwan/tony/framework"
"github.com/bwmarrin/discordgo"
)
func RegisterPingApp(bot *framework.Bot) framework.Route {
return framework.NewRoute(bot, "ping",
// ping
&PingCommand{}, // [NOP]
)
}
// PingCommand defines a command for responding to "ping" interactions
// with a simple "Pong!" message. This command demonstrates a basic
// interaction within Discord using the discordgo package.
type PingCommand struct {
framework.ApplicationCommand
}
func (pc PingCommand) GetType() framework.AppType {
return framework.AppTypeCommand | framework.AppTypeEvent
}
// Register is responsible for registering the "ping" command with
// Discord's API. It defines the command name and description that
// appear in the Discord user interface.
func (pc PingCommand) GetDefinition() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: "ping",
Description: "Replies with a button to Ping-Pong!",
}
}
// OnCommand handles the execution logic for the "ping" command. When a user
// invokes this command, Discord triggers this method, allowing the bot to
// respond appropriately.
func (pc PingCommand) OnCommand(ctx framework.CommandContext) {
err := ctx.Session().InteractionRespond(ctx.Interaction(), &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Ping",
Style: discordgo.SuccessButton,
Emoji: &discordgo.ComponentEmoji{
Name: "🏓",
},
CustomID: "ping:ping",
},
},
},
},
},
})
if err != nil {
ctx.Logger().WithError(err).Error("Failed to respond to interaction")
}
}
// OnEvent handles the event logic for the "ping" command. When a user
// interacts with the button, Discord triggers this method, allowing the bot
// to respond appropriately.
func (p PingCommand) OnEvent(ctx framework.EventContext, eventType discordgo.InteractionType) {
value := ctx.EventValue()
interaction := ctx.Interaction()
// Get the user from the interaction
user := interaction.Member.User
if user == nil {
user = interaction.User
}
// Reject non message component interactions
if eventType != discordgo.InteractionMessageComponent {
ctx.Session().InteractionRespond(interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("I only respond to button presses %s :(", user.Mention()),
},
})
return
}
// Reject non-ping button presses
if value != "ping" {
ctx.Session().InteractionRespond(interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("I only respond to the ping button %s :(", user.Mention()),
},
})
return
}
// Respond to the interaction Successfully
ctx.Session().InteractionRespond(interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Pong %s!", user.Mention()),
},
})
}