More cleanup

This commit is contained in:
Logan G 2022-07-02 00:53:19 -06:00
parent 32b27c6245
commit 97e0eaf46b
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 47 additions and 56 deletions

View file

@ -7,13 +7,8 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func ping(s *discordgo.Session, i *discordgo.InteractionCreate) { func ping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if i.Interaction.Member != nil { session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
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, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
//Flags: 1 << 6, // Only lets issuer see initial response //Flags: 1 << 6, // Only lets issuer see initial response
@ -22,13 +17,8 @@ func ping(s *discordgo.Session, i *discordgo.InteractionCreate) {
}) })
} }
func sping(s *discordgo.Session, i *discordgo.InteractionCreate) { func sping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if i.Interaction.Member != nil { session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
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, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Flags: 1 << 6, // Only lets issuer see initial response Flags: 1 << 6, // Only lets issuer see initial response
@ -37,13 +27,8 @@ func sping(s *discordgo.Session, i *discordgo.InteractionCreate) {
}) })
} }
func slowping(s *discordgo.Session, i *discordgo.InteractionCreate) { func slowping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if i.Interaction.Member != nil { session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
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, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
//Flags: 1 << 6, // Only lets issuer see initial response //Flags: 1 << 6, // Only lets issuer see initial response
@ -51,9 +36,9 @@ func slowping(s *discordgo.Session, i *discordgo.InteractionCreate) {
}, },
}) })
time.Sleep(time.Second * 10) time.Sleep(10 * time.Second)
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ _, err := session.FollowupMessageCreate(interaction.Interaction, true, &discordgo.WebhookParams{
Content: "Pong!", Content: "Pong!",
}) })
if err != nil { if err != nil {

72
main.go
View file

@ -19,7 +19,7 @@ import (
func main() { func main() {
// Lazyinator 9001 // Lazyinator 9001
var ( var (
s *discordgo.Session session *discordgo.Session
err error err error
) )
@ -54,31 +54,37 @@ func main() {
flag.Parse() flag.Parse()
s, err = discordgo.New("Bot " + *BotToken) session, err = discordgo.New("Bot " + *BotToken)
if err != nil { if err != nil {
fmt.Println("error creating Discord session,", err) fmt.Println("error creating Discord session,", err)
return return
} }
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { // This runs slash commands
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { session.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
h(s, i) if h, ok := commandHandlers[interaction.ApplicationCommandData().Name]; ok {
if interaction.Interaction.Member != nil {
log.Printf("%s#%s invoked command %s", interaction.Member.User.Username, interaction.Member.User.Discriminator, interaction.ApplicationCommandData().Name)
} else if interaction.Interaction.User != nil {
log.Printf("%s#%s invoked command %s", interaction.User.Username, interaction.User.Discriminator, interaction.ApplicationCommandData().Name)
}
h(session, interaction)
} }
}) })
// Message to print when bot is ready to go // Message to print when bot is ready to go
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { session.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", session.State.User.Username, session.State.User.Discriminator)
}) })
// Register the messageCreate func as a callback for MessageCreate events. // Register the messageCreate func as a callback for MessageCreate events.
s.AddHandler(messageCreate) session.AddHandler(messageCreate)
// In this example, we only care about receiving message events. // In this example, we only care about receiving message events.
//s.Identify.Intents = discordgo.IntentsGuildMessages //s.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening. // Open a websocket connection to Discord and begin listening.
err = s.Open() err = session.Open()
if err != nil { if err != nil {
fmt.Println("error opening connection,", err) fmt.Println("error opening connection,", err)
return return
@ -86,13 +92,13 @@ func main() {
log.Println("Adding commands...") log.Println("Adding commands...")
registeredCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands) registeredCommands, err := session.ApplicationCommandBulkOverwrite(session.State.User.ID, *GuildID, commands)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
// Just incase // Just incase
defer s.Close() defer session.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 running. Press CTRL-C to exit.") log.Println("Bot is running. Press CTRL-C to exit.")
@ -103,51 +109,51 @@ func main() {
log.Printf("Signal received, closing bot...") 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 := session.ApplicationCommandDelete(session.State.User.ID, *GuildID, v.ID)
if err != nil { if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err) log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
} }
} }
s.Close() session.Close()
} }
// This function will be called (due to AddHandler above) every time a new // This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to. // message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself // Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice. // This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID { if message.Author.ID == session.State.User.ID {
return return
} }
/* /*
// If the message is "ping" reply with "Pong!" // If the message is "ping" reply with "Pong!"
if m.Content == "ping" { if message.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!") session.ChannelMessageSend(message.ChannelID, "Pong!")
} }
// If the message is "pong" reply with "Ping!" // If the message is "pong" reply with "Ping!"
if m.Content == "pong" { if message.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping!") session.ChannelMessageSend(message.ChannelID, "Ping!")
} }
*/ */
//log.Printf("[%s] %s#%s: %s", m.ID, m.Author.Username, m.Author.Discriminator, m.Content) //log.Printf("[%s] %s#%s: %s", message.ID, message.Author.Username, message.Author.Discriminator, message.Content)
rxStrict := xurls.Strict() rxStrict := xurls.Strict()
urls := rxStrict.FindAllString(m.Content, -1) urls := rxStrict.FindAllString(message.Content, -1)
if len(urls) > 0 { if len(urls) > 0 {
message := discordgo.MessageSend { response := discordgo.MessageSend {
Content: "Woah there partner! I detect some services that don't embed very well in Discord. Let me help you with that!\n", Content: "Woah there partner! I detect some services that aren't very privacy friendly. Let me help you with that!\n",
Reference: m.Reference(), Reference: message.Reference(),
Files: []*discordgo.File{}, Files: []*discordgo.File{},
} }
log.Printf("Message %s has URLs!", m.ID) log.Printf("Message %s has URLs!", message.ID)
respond := false respond := false
for _, url := range urls { for _, url := range urls {
if output, _ := regexp.MatchString("http.*twitter.com/.*/status", url); output { if output, _ := regexp.MatchString("(http.*twitter.com/.*/status)|(http.*t.co/.*)", url); output {
log.Println("Cringe twitter post detected.") log.Println("Cringe twitter post detected.")
goutubedl.Path = "yt-dlp" goutubedl.Path = "yt-dlp"
@ -155,12 +161,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if err != nil { if err != nil {
// Probably no videos in it, don't care // Probably no videos in it, don't care
log.Println(err) log.Println(err)
//s.ChannelMessageSend(m.ChannelID, err.Error()) //session.ChannelMessageSend(message.ChannelID, err.Error())
continue continue
} }
// Get rid of the stupid short URL that Shitter appends to everything // Get rid of the stupid short URL that Shitter appends to everything
message.Content = message.Content + fmt.Sprintf("\"%s\"\n", strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly response.Content = response.Content + fmt.Sprintf("\"%s\"\n", strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly
var choice goutubedl.Format var choice goutubedl.Format
for i := len(result.Formats())-1; i >= 0; i-- { for i := len(result.Formats())-1; i >= 0; i-- {
@ -175,11 +181,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
downloadResult, err := result.Download(context.Background(), choice.FormatID) downloadResult, err := result.Download(context.Background(), choice.FormatID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
//s.ChannelMessageSend(m.ChannelID, err.Error()) //session.ChannelMessageSend(message.ChannelID, err.Error())
continue continue
} }
message.Files = append(message.Files, &discordgo.File { response.Files = append(response.Files, &discordgo.File {
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext), Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext),
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: downloadResult, Reader: downloadResult,
@ -191,11 +197,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
} }
if respond { if respond {
if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil { if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
log.Println(message) log.Println(result)
log.Println(err) log.Println(err)
} else { } else {
log.Printf("Successfully responded to %s", m.ID) log.Printf("Successfully responded to %s", message.ID)
} }
} }
} }