Globals are bad, also bot no longer segfaults if you PM it

This commit is contained in:
Logan G 2022-07-01 14:46:55 -06:00
parent f7475d684b
commit bf458fdea9
Signed by: logan
GPG key ID: E328528C921E7A7A

153
main.go
View file

@ -17,62 +17,92 @@ import (
"mvdan.cc/xurls/v2" // Peak lazy "mvdan.cc/xurls/v2" // Peak lazy
) )
// Variables used for command line parameters func main() {
var ( // Variables used for command line parameters
commands = []*discordgo.ApplicationCommand { var (
{ commands = []*discordgo.ApplicationCommand {
Name: "ping", {
Description: "Hopefully replies with pong or else I'll be sad.", Name: "ping",
}, Description: "Hopefully replies with pong or else I'll be sad.",
{ },
Name: "slowping", {
Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.", Name: "sping",
}, Description: "Hopefully replies with pong silently or else I'll be sad.",
} },
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) { {
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { Name: "slowping",
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.",
Type: discordgo.InteractionResponseChannelMessageWithSource, },
Data: &discordgo.InteractionResponseData{ }
//Flags: 1 << 6, // Only lets issuer see initial response commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Interaction.Member != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.Member.User.Username, i.Interaction.Member.User.Discriminator)
} else if i.Interaction.User != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.User.Username, i.Interaction.User.Discriminator)
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Flags: 1 << 6, // Only lets issuer see initial response
Content: "Pong!",
},
})
},
"sping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Interaction.Member != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.Member.User.Username, i.Interaction.Member.User.Discriminator)
} else if i.Interaction.User != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.User.Username, i.Interaction.User.Discriminator)
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6, // Only lets issuer see initial response
Content: "Pong!",
},
})
},
"slowping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Interaction.Member != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.Member.User.Username, i.Interaction.Member.User.Discriminator)
} else if i.Interaction.User != nil {
log.Printf("%s#%s invoked ping command", i.Interaction.User.Username, i.Interaction.User.Discriminator)
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Flags: 1 << 6, // Only lets issuer see initial response
Content: "No problem.",
},
})
time.Sleep(time.Second * 10)
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: "Pong!", Content: "Pong!",
}, })
}) if err != nil {
}, log.Println(err)
"slowping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { }
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ },
Type: discordgo.InteractionResponseChannelMessageWithSource, }
Data: &discordgo.InteractionResponseData{ )
//Flags: 1 << 6, // Only lets issuer see initial response
Content: "No problem.",
},
})
time.Sleep(time.Second * 10) // Lazyinator 9001
var (
s *discordgo.Session
err error
)
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ // Bot params
Content: "Pong!", var (
}) GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
if err != nil { BotToken = flag.String("token", "", "Bot access token")
log.Println(err) )
}
},
}
)
// Bot params
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
)
// Lazyinator 9001
var (
s *discordgo.Session
err error
)
func init() {
flag.Parse() flag.Parse()
s, err = discordgo.New("Bot " + *BotToken) s, err = discordgo.New("Bot " + *BotToken)
@ -86,9 +116,8 @@ func init() {
h(s, i) h(s, i)
} }
}) })
}
func main() { // Message to print when bot is ready to go
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v\n", s.State.User.Username, s.State.User.Discriminator) log.Printf("Logged in as: %v#%v\n", s.State.User.Username, s.State.User.Discriminator)
}) })
@ -107,32 +136,22 @@ func main() {
} }
log.Println("Adding commands...") log.Println("Adding commands...")
//registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
registeredCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands) registeredCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
/*
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
*/
// Just incase // Just incase
defer s.Close() defer s.Close()
// Wait here until CTRL-C or other term signal is received. // Wait here until CTRL-C or other term signal is received.
log.Println("Bot is starting. Press CTRL-C to exit.") log.Println("Bot is running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc <-sc
log.Printf("Signal received, closing bot...")
// Cleanly close down the Discord session. // Cleanly close down the Discord session.
for _, v := range registeredCommands { for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID) err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID)
@ -163,7 +182,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
*/ */
log.Printf("[%s] %s#%s: %s", m.ID, m.Author.Username, m.Author.Discriminator, m.Content) //log.Printf("[%s] %s#%s: %s", m.ID, m.Author.Username, m.Author.Discriminator, m.Content)
rxStrict := xurls.Strict() rxStrict := xurls.Strict()
urls := rxStrict.FindAllString(m.Content, -1) urls := rxStrict.FindAllString(m.Content, -1)