Created
November 18, 2021 07:13
-
-
Save aminoxix/8a32626eb1c4215f2f0021d6aa6d99a4 to your computer and use it in GitHub Desktop.
DiscordGo: Ping Pong!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pingome is a discord bot written in Go. It follows specific text by user, to notify them via direct message. | |
* To installing dependencies, run via replit: | |
* | |
* $ go mod init amino19/pingo | |
* $ go get github.com/bwmarrin/discordgo | |
* | |
* ⭐ Star the gist to bookmark, fork to share and appreciate the work. | |
* | |
* FMI: https://github.com/amino19/pingo | |
*/ | |
package main | |
import ( | |
"os" | |
"fmt" | |
"flag" | |
"syscall" | |
"os/signal" | |
"github.com/bwmarrin/discordgo" | |
) | |
// Variables used for command line parameters | |
var ( | |
Token string | |
) | |
func init() { | |
flag.StringVar(&Token, "t", "", "Bot Token") | |
flag.Parse() | |
} | |
func main() { | |
// Create a new Discord session using the provided bot token. | |
mySecret := os.Getenv("Token") | |
dg, err := discordgo.New("Bot " + mySecret) | |
if err != nil { | |
fmt.Println("error creating Discord session,", err) | |
return | |
} | |
// Register the messageCreate func as a callback for MessageCreate events. | |
dg.AddHandler(messageCreate) | |
// Just like the ping pong example, we only care about receiving message events in this example. | |
dg.Identify.Intents = discordgo.IntentsGuildMessages | |
// Open a websocket connection to Discord and begin listening. | |
err = dg.Open() | |
if err != nil { | |
fmt.Println("error opening connection,", err) | |
return | |
} | |
// Wait here until CTRL-C or other term signal is received. | |
fmt.Println("Bot is now running. Press CTRL-C to exit.") | |
sc := make(chan os.Signal, 1) | |
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) | |
<-sc | |
// Cleanly close down the Discord session. | |
dg.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. | |
* | |
* It is called whenever a message is created but only when it's sent through a server as we did not request IntentsDirectMessages. | |
*/ | |
func messageCreate(s *discordgo.Session, m *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 { | |
return | |
} | |
// In this example, we only care about messages that are "ping". | |
if m.Content != "hey" { | |
return | |
} | |
// We create the private channel with the user who sent the message. | |
channel, err := s.UserChannelCreate(m.Author.ID) | |
if err != nil { | |
/* If an error occurred, we failed to create the channel. | |
* | |
* Some common causes are: | |
* 1. We don't share a server with the user (not possible here). | |
* 2. We opened enough DM channels quickly enough for Discord to label us as abusing the endpoint, blocking us from opening new ones. | |
*/ | |
fmt.Println("error creating channel:", err) | |
s.ChannelMessageSend( | |
m.ChannelID, | |
"Something went wrong while sending the DM!", | |
) | |
return | |
} | |
// Then we send the message through the channel we created. | |
_, err = s.ChannelMessageSend(channel.ID, "Pl join here, it will be more inclusive to shoot: https://discord.com/invite/MVujzTBqed") | |
if err != nil { | |
/* If an error occurred, we failed to send the message. | |
* | |
* It may occur either when we do not share a server with the user (highly unlikely as we just received a message) or the user disabled DM in their settings (more likely). | |
*/ | |
fmt.Println("error sending DM message:", err) | |
s.ChannelMessageSend( | |
m.ChannelID, | |
"Failed to send you a DM. "+ | |
"Did you disable DM in your privacy settings?", | |
) | |
} | |
} | |
/* Congratulations on making it till the end! ✨ | |
* Made with ♥️ by @amino19 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment