Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theGeekPirate/39c641d87bc963733b1d57658494dd50 to your computer and use it in GitHub Desktop.
Save theGeekPirate/39c641d87bc963733b1d57658494dd50 to your computer and use it in GitHub Desktop.
discordgo function wrappers
// SendMessage is a convenience method to send messages with
func SendMessage(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
_, err := s.ChannelMessageSend(m.ChannelID, message)
if err != nil {
return
}
}
// SendDM will attempt to send a direct message to the user
func SendDM(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
userChannel, err := s.UserChannelCreate(m.Author.ID)
if err != nil {
return
}
_, err = s.ChannelMessageSend(userChannel.ID, message)
if err != nil {
return
}
}
// IsDM tells us if the message was a direct message or not
func IsDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
channel, err := s.Channel(m.ChannelID)
if err != nil {
return false
}
if channel.Type != discordgo.ChannelTypeDM {
return false
}
return true
}
// GuildIDFromMessage is a convenience method to find out which Guild the message was sent from
func GuildIDFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) string {
c, err := s.State.Channel(m.ChannelID)
if err != nil {
return ""
}
return c.GuildID
}
// ChannelNameFromID returns the channel name using its ID
func ChannelNameFromID(s *discordgo.Session, id string) (string, error) {
c, err := s.State.Channel(id)
if err != nil {
return "", errors.New("this channel does not exist for this guild")
}
return c.Name, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment