feat(discord bot): added in a ping host command to get the bot going :D
more commands will be added as we continue working on this bot, including service restarts, log monitors
This commit is contained in:
59
controller/internal/bot/ping_device.go
Normal file
59
controller/internal/bot/ping_device.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func HandlePingCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
// Get the host parameter
|
||||
options := i.ApplicationCommandData().Options
|
||||
host := options[0].StringValue()
|
||||
|
||||
// Optionally, do a DNS lookup or ICMP ping
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Could not resolve host `%s`: %v", host, err),
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// // host is the user-provided hostname
|
||||
// pinger, err := probing.NewPinger(host)
|
||||
// if err != nil {
|
||||
// s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
// Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
// Data: &discordgo.InteractionResponseData{
|
||||
// Content: fmt.Sprintf("Error creating pinger: %v", err),
|
||||
// },
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
// pinger.Count = 3
|
||||
// pinger.SetPrivileged(false) // <- key for Windows non-admin
|
||||
// err = pinger.Run()
|
||||
// if err != nil {
|
||||
// s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
// Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
// Data: &discordgo.InteractionResponseData{
|
||||
// Content: fmt.Sprintf("Error running ping: %v", err),
|
||||
// },
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
// stats := pinger.Statistics()
|
||||
|
||||
// Respond with the first IP found
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Host `%s` resolves to %s ✅", host, ips[0].String()),
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user