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"
)
func ping(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{
func ping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//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) {
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{
func sping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
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) {
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{
func slowping(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//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!",
})
if err != nil {

72
main.go
View file

@ -19,7 +19,7 @@ import (
func main() {
// Lazyinator 9001
var (
s *discordgo.Session
session *discordgo.Session
err error
)
@ -54,31 +54,37 @@ func main() {
flag.Parse()
s, err = discordgo.New("Bot " + *BotToken)
session, err = discordgo.New("Bot " + *BotToken)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
// This runs slash commands
session.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
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
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)
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
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.
s.AddHandler(messageCreate)
session.AddHandler(messageCreate)
// In this example, we only care about receiving message events.
//s.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = s.Open()
err = session.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
@ -86,13 +92,13 @@ func main() {
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 {
log.Panic(err)
}
// Just incase
defer s.Close()
defer session.Close()
// Wait here until CTRL-C or other term signal is received.
log.Println("Bot is running. Press CTRL-C to exit.")
@ -103,51 +109,51 @@ func main() {
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)
err := session.ApplicationCommandDelete(session.State.User.ID, *GuildID, v.ID)
if err != nil {
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
// 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
// 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
}
/*
// If the message is "ping" reply with "Pong!"
if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!")
if message.Content == "ping" {
session.ChannelMessageSend(message.ChannelID, "Pong!")
}
// If the message is "pong" reply with "Ping!"
if m.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping!")
if message.Content == "pong" {
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()
urls := rxStrict.FindAllString(m.Content, -1)
urls := rxStrict.FindAllString(message.Content, -1)
if len(urls) > 0 {
message := 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",
Reference: m.Reference(),
response := discordgo.MessageSend {
Content: "Woah there partner! I detect some services that aren't very privacy friendly. Let me help you with that!\n",
Reference: message.Reference(),
Files: []*discordgo.File{},
}
log.Printf("Message %s has URLs!", m.ID)
log.Printf("Message %s has URLs!", message.ID)
respond := false
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.")
goutubedl.Path = "yt-dlp"
@ -155,12 +161,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if err != nil {
// Probably no videos in it, don't care
log.Println(err)
//s.ChannelMessageSend(m.ChannelID, err.Error())
//session.ChannelMessageSend(message.ChannelID, err.Error())
continue
}
// 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
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)
if err != nil {
log.Println(err)
//s.ChannelMessageSend(m.ChannelID, err.Error())
//session.ChannelMessageSend(message.ChannelID, err.Error())
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),
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: downloadResult,
@ -191,11 +197,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}
if respond {
if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil {
log.Println(message)
if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
log.Println(result)
log.Println(err)
} else {
log.Printf("Successfully responded to %s", m.ID)
log.Printf("Successfully responded to %s", message.ID)
}
}
}