Created
March 11, 2018 07:24
-
-
Save theGeekPirate/39c641d87bc963733b1d57658494dd50 to your computer and use it in GitHub Desktop.
discordgo function wrappers
This file contains hidden or 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
// 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