用golang去写discord机器人,但是网上资料很少,也没有如何去ping人的教程,通过查阅,实现了ping人,贴下如下代码,仅供参考:
_, _ = s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
Content: fmt.Sprintf("<@%s> Congratulate on deploying erc721 contract successfully for %s. Your contract address is %s! \n %s", m.Author.ID, userAddress, address, advertise),
Reference: m.Reference(),
AllowedMentions: &discordgo.MessageAllowedMentions{nil, nil, []string{m.Author.ID},
},
})
其中,MessageAllowedMentioned结构体对ping人权限做了约束,只有在权限列表中的用户才可以被ping
// MessageAllowedMentions allows the user to specify which mentions
// Discord is allowed to parse in this message. This is useful when
// sending user input as a message, as it prevents unwanted mentions.
// If this type is used, all mentions must be explicitly whitelisted,
// either by putting an AllowedMentionType in the Parse slice
// (allowing all mentions of that type) or, in the case of roles and
// users, explicitly allowing those mentions on an ID-by-ID basis.
// For more information on this functionality, see:
// https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object-allowed-mentions-reference
type MessageAllowedMentions struct {
// The mention types that are allowed to be parsed in this message.
// Please note that this is purposely **not** marked as omitempty,
// so if a zero-value MessageAllowedMentions object is provided no
// mentions will be allowed.
Parse []AllowedMentionType `json:"parse"`
// A list of role IDs to allow. This cannot be used when specifying
// AllowedMentionTypeRoles in the Parse slice.
Roles []string `json:"roles,omitempty"`
// A list of user IDs to allow. This cannot be used when specifying
// AllowedMentionTypeUsers in the Parse slice.
Users []string `json:"users,omitempty"`
}
由结构体可知,可以从roles和users两个维度去进行约束。具体如何去约束,可以去参考discord给出的官方文档
只需要知道的是,users是我们常用的,基于id进行约束。在ping人时,需要"<@id>"