本文整理匯總了Golang中github.com/bwmarrin/discordgo.MessageCreate類的典型用法代碼示例。如果您正苦於以下問題:Golang MessageCreate類的具體用法?Golang MessageCreate怎麽用?Golang MessageCreate使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MessageCreate類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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)
}
示例2: SBMessageCreate
func SBMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author == nil { // This shouldn't ever happen but we check for it anyway
return
}
t := time.Now().UTC().Unix()
sb.LastMessages[m.ChannelID] = t
ch, err := sb.dg.State.Channel(m.ChannelID)
private := true
if err == nil { // Because of the magic of web development, we can get a message BEFORE the "channel created" packet for the channel being used by that message.
private = ch.IsPrivate
} else {
fmt.Println("Error retrieving channel "+m.ChannelID+": ", err.Error())
}
var info *GuildInfo = nil
isdbguild := true
isdebug := false
if !private {
info = GetChannelGuild(m.ChannelID)
if info == nil {
return
}
isdbguild = sb.IsDBGuild(info)
isdebug = info.IsDebug(m.ChannelID)
}
cid := SBatoi(m.ChannelID)
if isdebug && !sb.Debug {
return // we do this up here so the release build doesn't log messages in bot-debug, but debug builds still log messages from the rest of the channels
}
if info != nil && cid != info.config.LogChannel && isdbguild { // Log this message if it was sent to the main guild only.
sb.db.AddMessage(SBatoi(m.ID), SBatoi(m.Author.ID), m.ContentWithMentionsReplaced(), cid, m.MentionEveryone, SBatoi(ch.GuildID))
if m.Author.ID == sb.SelfID { // ALWAYS discard any of our own messages before analysis.
SBAddPings(info, m.Message) // If we're discarding a message we still need to add any pings to the ping table
return
}
}
if m.Author.ID == sb.SelfID { // if this is true here, it means we were unable to log the message, so we can't possibly add the ping.
return
}
if boolXOR(sb.Debug, isdebug) { // debug builds only respond to the debug channel, and release builds ignore it
return
}
SBProcessCommand(s, m.Message, info, t, isdbguild, isdebug, err)
}
示例3: messageCreate
func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// not relay our own messages
if m.Author.Username == b.Nick {
return
}
if len(m.Attachments) > 0 {
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL
}
}
if m.Content == "" {
return
}
flog.Debugf("Sending message from %s on %s to gateway", m.Author.Username, b.Account)
channelName := b.getChannelName(m.ChannelID)
if b.UseChannelID {
channelName = "ID:" + m.ChannelID
}
b.Remote <- config.Message{Username: m.Author.Username, Text: m.ContentWithMentionsReplaced(), Channel: channelName,
Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg"}
}
示例4: onMessageCreate
// the below method is invoked when the Discord Websocket API transmits
// a 'messageCreate' event to the bot
func onMessageCreate(s *discordgo.Session, event *discordgo.MessageCreate) {
logrus.WithFields(logrus.Fields{
"authorId": event.Author.ID,
"authorUsername": event.Author.Username,
"channelId": event.ChannelID,
"timestamp": event.Timestamp,
"content": event.Content,
}).Debug("Received message create event from Discord Websocket API.")
// do not take action if the author of the message is ourself
if s.State.Ready.User.ID == event.Author.ID {
return
}
// ensure the message was not received via direct message
currChannel, err := s.Channel(event.ChannelID)
if err != nil {
logrus.WithFields(logrus.Fields{
"channelId": event.ChannelID,
"capturedError": err,
}).Error("Could not retrieve channel object from identifier.")
return
}
if currChannel.IsPrivate {
s.ChannelMessageSend(event.ChannelID, "Hi! I don't respond to commands sent through direct message, as I cannot tell what Discord server you are trying to set on! Please send me a message in the appropriate text channel on a Discord server to try again.")
return
}
// convert the text to lowercase to avoid case-sensitivity issues
lowerContent := strings.ToLower(event.ContentWithMentionsReplaced())
if strings.HasPrefix(lowerContent, "!setteam ") {
givenTeamName := lowerContent[9:]
go handleTeamSet(s, &givenTeamName, &event.Author.ID, &event.ChannelID)
}
}
示例5: messageCreate
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
var deleteMessageAuthor bool
deleteMessageAuthor = true
text := m.ContentWithMentionsReplaced()
if m.Author.ID == s.State.User.ID {
// Ignore self
return
}
channel, _ := s.State.Channel(m.ChannelID)
if channel.IsPrivate {
channel.Name = "direct message"
}
if strings.HasPrefix(text, "!") || strings.HasPrefix(text, ".") || strings.HasPrefix(text, "bot.") {
// Ignore shit meant for other bots
return
}
isMentioned := isUserMentioned(s.State.User, m.Mentions) || m.MentionEveryone
text = strings.Replace(text, "@everyone", "", -1)
// Log cleaned up message
fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), m.Author.Username, text)
if shouldIgnore(m.Author) {
return
}
c := config.Get()
for _, ChannelID := range c.Channels {
if m.ChannelID == ChannelID {
linksFound, reply := lewd.ParseLinks(text)
if linksFound {
content := strings.Join(lewd.ContentLinks(m.Content)[:], " ")
if deleteMessageAuthor {
s.ChannelMessageDelete(m.ChannelID, m.ID)
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s: %s\n%s", m.Author.Username, content, reply))
return
}
}
}
// Accept the legacy mention as well and trim it off from text
if strings.HasPrefix(strings.ToLower(text), "lewdbot, ") {
text = text[9:]
isMentioned = true
}
if channel.IsPrivate || isMentioned {
var reply string
reply = regex.Lewdbot.ReplaceAllString(reply, m.Author.Username)
// Log our reply
fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), s.State.User.Username, reply)
s.ChannelMessageSend(m.ChannelID, reply)
}
}
示例6: onMessageCreate
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if len(m.Content) <= 0 || (m.Content[0] != '!' && len(m.Mentions) < 1) {
return
}
msg := strings.Replace(m.ContentWithMentionsReplaced(), s.State.Ready.User.Username, "username", 1)
parts := strings.Split(strings.ToLower(msg), " ")
channel, _ := discord.State.Channel(m.ChannelID)
if channel == nil {
log.WithFields(log.Fields{
"channel": m.ChannelID,
"message": m.ID,
}).Warning("Failed to grab channel")
return
}
guild, _ := discord.State.Guild(channel.GuildID)
if guild == nil {
log.WithFields(log.Fields{
"guild": channel.GuildID,
"channel": channel,
"message": m.ID,
}).Warning("Failed to grab guild")
return
}
// If this is a mention, it should come from the owner (otherwise we don't care)
if len(m.Mentions) > 0 && m.Author.ID == OWNER && len(parts) > 0 {
mentioned := false
for _, mention := range m.Mentions {
mentioned = (mention.ID == s.State.Ready.User.ID)
if mentioned {
break
}
}
if mentioned {
handleBotControlMessages(s, m, parts, guild)
}
return
}
// Find the collection for the command we got
for _, coll := range COLLECTIONS {
if scontains(parts[0], coll.Commands...) {
// If they passed a specific sound effect, find and select that (otherwise play nothing)
var sound *Sound
if len(parts) > 1 {
for _, s := range coll.Sounds {
if parts[1] == s.Name {
sound = s
}
}
if sound == nil {
return
}
}
go enqueuePlay(m.Author, guild, coll, sound)
return
}
}
}
示例7: messageCreate
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
// Ignore self
return
}
channel, _ := s.State.Channel(m.ChannelID)
if channel.IsPrivate {
channel.Name = "direct message"
}
isMentioned := isUserMentioned(s.State.User, m.Mentions) || m.MentionEveryone
if shouldIgnore(m.Author) {
return
}
text := m.ContentWithMentionsReplaced()
text = strings.Replace(text, "@everyone", "", -1)
// Log cleaned up message
fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), m.Author.Username, text)
commandFound, reply := commands.ParseMessage(s, m, text)
if commandFound {
_, err := s.ChannelMessageSend(m.ChannelID, reply)
if err != nil {
fmt.Println("s.ChannelMessageSend >> ", err)
}
return
} else if strings.HasPrefix(text, "!") || strings.HasPrefix(text, ".") || strings.HasPrefix(text, "bot.") {
// Ignore shit meant for other bots
return
}
if config.ChannelIsLewd(channel.GuildID, m.ChannelID) {
linksFound, reply := lewd.ParseLinks(text)
if linksFound {
s.ChannelMessageSend(m.ChannelID, reply)
return
}
}
// Accept the legacy mention as well and trim it off from text
if strings.HasPrefix(strings.ToLower(text), "lewdbot, ") {
text = text[9:]
isMentioned = true
}
if channel.IsPrivate || isMentioned {
reply := brain.Reply(text)
reply = regex.Lewdbot.ReplaceAllString(reply, m.Author.Username)
// Log our reply
fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), s.State.User.Username, reply)
s.ChannelMessageSend(m.ChannelID, reply)
} else if !config.GuildIsDumb(channel.GuildID) {
// Just learn
brain.Learn(text, true)
}
}