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

75
main.go
View file

@ -17,13 +17,18 @@ import (
"mvdan.cc/xurls/v2" // Peak lazy
)
// Variables used for command line parameters
var (
func main() {
// Variables used for command line parameters
var (
commands = []*discordgo.ApplicationCommand {
{
Name: "ping",
Description: "Hopefully replies with pong or else I'll be sad.",
},
{
Name: "sping",
Description: "Hopefully replies with pong silently or else I'll be sad.",
},
{
Name: "slowping",
Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.",
@ -31,6 +36,11 @@ var (
}
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{
@ -39,7 +49,28 @@ var (
},
})
},
"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{
@ -58,21 +89,20 @@ var (
}
},
}
)
)
// 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 (
// Lazyinator 9001
var (
s *discordgo.Session
err error
)
)
// Bot params
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
)
func init() {
flag.Parse()
s, err = discordgo.New("Bot " + *BotToken)
@ -86,9 +116,8 @@ func init() {
h(s, i)
}
})
}
func main() {
// Message to print when bot is ready to go
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)
})
@ -107,32 +136,22 @@ func main() {
}
log.Println("Adding commands...")
//registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
registeredCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands)
if err != nil {
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
defer s.Close()
// 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)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Printf("Signal received, closing bot...")
// Cleanly close down the Discord session.
for _, v := range registeredCommands {
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()
urls := rxStrict.FindAllString(m.Content, -1)