Globals are bad, also bot no longer segfaults if you PM it
This commit is contained in:
parent
f7475d684b
commit
bf458fdea9
1 changed files with 86 additions and 67 deletions
63
main.go
63
main.go
|
@ -17,6 +17,7 @@ import (
|
||||||
"mvdan.cc/xurls/v2" // Peak lazy
|
"mvdan.cc/xurls/v2" // Peak lazy
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
// Variables used for command line parameters
|
// Variables used for command line parameters
|
||||||
var (
|
var (
|
||||||
commands = []*discordgo.ApplicationCommand {
|
commands = []*discordgo.ApplicationCommand {
|
||||||
|
@ -24,6 +25,10 @@ var (
|
||||||
Name: "ping",
|
Name: "ping",
|
||||||
Description: "Hopefully replies with pong or else I'll be sad.",
|
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",
|
Name: "slowping",
|
||||||
Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.",
|
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) {
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
"ping": 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{
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
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) {
|
"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{
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
@ -60,19 +91,18 @@ 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
|
// Lazyinator 9001
|
||||||
var (
|
var (
|
||||||
s *discordgo.Session
|
s *discordgo.Session
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
// Bot params
|
||||||
|
var (
|
||||||
|
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
|
||||||
|
BotToken = flag.String("token", "", "Bot access token")
|
||||||
|
)
|
||||||
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue