本文整理匯總了Golang中github.com/bwmarrin/discordgo.Session類的典型用法代碼示例。如果您正苦於以下問題:Golang Session類的具體用法?Golang Session怎麽用?Golang Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Session類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Welcome
//Welcome sends an acknowledge to the terminal that it is listening, and prints the current Username
func Welcome(dg *discordgo.Session) {
d := color.New(color.FgYellow, color.Bold)
d.Printf("Listening!\n\n")
user, _ := dg.User("@me")
d.Printf("Welcome, %s!\n\n", user.Username)
}
示例2: playSound
// playSound plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
return err
}
// Sleep for a specified amount of time before playing the sound
time.Sleep(250 * time.Millisecond)
// Start speaking.
_ = vc.Speaking(true)
// Send the buffer data.
for _, buff := range buffer {
vc.OpusSend <- buff
}
// Stop speaking
_ = vc.Speaking(false)
// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)
// Disconnect from the provided voice channel.
_ = vc.Disconnect()
return nil
}
示例3: changeAvatar
// Helper function to change the avatar
func changeAvatar(s *discordgo.Session) {
resp, err := http.Get(URL)
if err != nil {
fmt.Println("Error retrieving the file, ", err)
return
}
defer func() {
_ = resp.Body.Close()
}()
img, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading the response, ", err)
return
}
base64 := base64.StdEncoding.EncodeToString(img)
avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64)
_, err = s.UserUpdate("", "", BotUsername, avatar, "")
if err != nil {
fmt.Println("Error setting the avatar, ", err)
}
}
示例4: Message
// Message receives a Message struct and sends it to appropriate channels
func Message(s *discordgo.Session, m *types.Message) error {
message := strings.Join(m.Payload, "\n")
var channels []string
var dchannels []*discordgo.Channel
var err error
c := config.Get()
if m.Prefix != "" {
message = fmt.Sprintf("%s: %s", m.Prefix, message)
}
if m.Channels[0] == "*" {
dchannels, err = s.GuildChannels(c.Guild)
if err != nil {
return err
}
//errhandler.Handle(err)
for _, chann := range dchannels {
channels = append(channels, chann.ID)
}
} else {
channels = m.Channels
}
log.Debugf("%s\n", len(channels))
for _, channel := range channels {
s.ChannelMessageSend(channel, message)
}
return nil
}
示例5: dgoListen
func dgoListen(s *discordgo.Session) error {
log.Notice("Digo listening for WS Events")
// Listen blocks until it returns
//go acceptInvite(s)
err := s.Listen()
return err
}
示例6: messageCreate
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID != config.Discord.ChannelID {
return
}
ign := ""
member, err := s.State.Member(config.Discord.ServerID, m.Author.ID)
if err != nil {
log.Printf("[Discord] Failed to get member: %s (Make sure you have set the bot permissions to see members)", err.Error())
return
}
roles, err := s.GuildRoles(config.Discord.ServerID)
if err != nil {
log.Printf("[Discord] Failed to get roles: %s (Make sure you have set the bot permissions to see roles)", err.Error())
return
}
for _, role := range member.Roles {
if ign != "" {
break
}
for _, gRole := range roles {
if ign != "" {
break
}
if strings.TrimSpace(gRole.ID) == strings.TrimSpace(role) {
if strings.Contains(gRole.Name, "IGN:") {
splitStr := strings.Split(gRole.Name, "IGN:")
if len(splitStr) > 1 {
ign = strings.TrimSpace(splitStr[1])
}
}
}
}
}
if ign == "" {
return
}
msg := m.ContentWithMentionsReplaced()
//Maximum limit of 4k
if len(msg) > 4000 {
msg = msg[0:4000]
}
if len(msg) < 1 {
return
}
ign = sanitize(ign)
msg = sanitize(msg)
//Send message.
if err = Sendln(fmt.Sprintf("emote world 260 %s says from discord, '%s'", ign, msg)); err != nil {
log.Printf("[Discord] Error sending message to telnet (%s:%s): %s\n", ign, msg, err.Error())
return
}
log.Printf("[Discord] %s: %s\n", ign, msg)
}
示例7: sendMessage
func sendMessage(sess *discordgo.Session, message string) {
channelid := fetchPrimaryTextChannelID(sess)
logInfo("SENDING MESSAGE:", message)
retryOnBadGateway(func() error {
_, err := sess.ChannelMessageSend(channelid, message)
return err
})
}
示例8: Ban
//Ban function calls the BanCreate api for discord and sets the warnning
//and banned information appropriatly
func (u *User) Ban(dg *discordgo.Session) (success bool, err error) {
u.Banned = true
u.BannedDate = time.Now()
u.Warnings = 0
err = dg.GuildBanCreate(u.Member.GuildID, u.Member.User.ID, 0)
if err == nil {
return true, nil
}
return false, nil
}
示例9: ChangeBotName
func ChangeBotName(s *discordgo.Session, name string, avatarfile string) {
binary, _ := ioutil.ReadFile(avatarfile)
avatar := base64.StdEncoding.EncodeToString(binary)
_, err := s.UserUpdate("", "", name, "data:image/jpeg;base64,"+avatar, "")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Changed username successfully")
}
}
示例10: bdLinks
func bdLinks(s *discordgo.Session, id string) {
resp, err := http.Get("https://betterdiscord.net/home/")
checkErr(err)
bytes, err := ioutil.ReadAll(resp.Body)
checkErr(err)
rx := regexp.MustCompile(`<a href="(.*.zip)`)
mm := rx.FindAllStringSubmatch(string(bytes), 2)
s.ChannelMessageSend(id,
fmt.Sprintf("\n`OSX:` %s\n`Windows:` %s", mm[1][1], mm[0][1]))
}
示例11: onGuildCreate
func onGuildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable != nil {
return
}
for _, channel := range event.Guild.Channels {
if channel.ID == event.Guild.ID {
s.ChannelMessageSend(channel.ID, "**AIRHORN BOT READY FOR HORNING. TYPE `!AIRHORN` WHILE IN A VOICE CHANNEL TO ACTIVATE**")
return
}
}
}
示例12: guildCreate
// This function will be called (due to AddHandler above) every time a new
// guild is joined.
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable != nil {
return
}
for _, channel := range event.Guild.Channels {
if channel.ID == event.Guild.ID {
_, _ = s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.")
return
}
}
}
示例13: acceptInvite
func acceptInvite(s *discordgo.Session) error {
var err error
c := config.Get()
//time.Sleep(1 * time.Second)
if c.InviteID != "" {
log.Debugf("Attempting to accept invite: %s", c.InviteID)
_, err = s.InviteAccept(c.InviteID)
} else {
log.Debug("No DIGO_INVITE_ID specified, no invite to accept.")
}
return err
}
示例14: fetchUser
func fetchUser(sess *discordgo.Session, userid string) *discordgo.User {
var result *discordgo.User
retryOnBadGateway(func() error {
var err error
result, err = sess.User(userid)
if err != nil {
return err
}
return nil
})
return result
}
示例15: checkForPermissions
func checkForPermissions(s *discordgo.Session,
channelID string, roleID []string) bool {
var ok bool
if len(roleID) > 0 {
_, ok = adminIds[roleID[0]]
if ok == true {
return ok
}
}
s.ChannelMessageSend(channelID, fmt.Sprintf("Insuficient permissions"))
return ok
}