2019-10-09 14:13:59 +07:00
|
|
|
package main
|
|
|
|
|
2019-10-10 12:35:53 +07:00
|
|
|
import (
|
2019-10-11 14:29:07 +07:00
|
|
|
"errors"
|
2019-10-10 12:35:53 +07:00
|
|
|
"fmt"
|
2019-10-11 14:29:07 +07:00
|
|
|
"strings"
|
2019-10-10 12:35:53 +07:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
2019-10-09 14:13:59 +07:00
|
|
|
|
|
|
|
const guildID = "626424729234046987"
|
|
|
|
const memberRoleID = "626434632614805526"
|
2019-10-10 10:38:30 +07:00
|
|
|
const auditChannel = "631789849929711627"
|
|
|
|
|
2019-10-11 14:29:07 +07:00
|
|
|
var errMemberNotFound = errors.New("messageutil: the requested member was not found")
|
|
|
|
|
2019-10-09 14:13:59 +07:00
|
|
|
func enforceDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
|
|
|
if m.GuildID != "" {
|
|
|
|
//This command can only be used in DM to protect the invite creator.
|
|
|
|
s.ChannelMessageDelete(m.ChannelID, m.ID)
|
|
|
|
s.ChannelMessageSend(
|
|
|
|
m.ChannelID,
|
|
|
|
m.Author.Mention()+",这个指令只能在私信中使用。\n"+m.Author.Mention()+", this command can only be used in DM.",
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func membersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
|
|
|
member, err := s.GuildMember(guildID, m.Author.ID)
|
|
|
|
if err != nil {
|
|
|
|
s.ChannelMessageSend(
|
|
|
|
m.ChannelID,
|
|
|
|
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, v := range member.Roles {
|
|
|
|
if v == memberRoleID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.ChannelMessageSend(
|
|
|
|
m.ChannelID,
|
|
|
|
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
2019-10-10 10:38:30 +07:00
|
|
|
|
|
|
|
func sendPrivateMessage(s *discordgo.Session, recipient, message string) error {
|
|
|
|
channel, err := s.UserChannelCreate(recipient)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = s.ChannelMessageSend(channel.ID, message)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func auditLog(s *discordgo.Session, message string) {
|
2019-10-10 12:35:53 +07:00
|
|
|
_, err := s.ChannelMessageSend(auditChannel, message)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("==Audit Log==")
|
|
|
|
fmt.Println(message)
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2019-10-10 10:38:30 +07:00
|
|
|
}
|
2019-10-11 14:29:07 +07:00
|
|
|
|
|
|
|
func getMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {
|
|
|
|
allMembers, err := getAllMembers(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(user, "@") {
|
|
|
|
user = strings.TrimPrefix(user, "@")
|
|
|
|
} else if strings.HasSuffix(user, ">") {
|
|
|
|
if strings.HasPrefix(user, "<@!") {
|
|
|
|
user = strings.TrimSuffix(strings.TrimPrefix(user, "<@!"), ">")
|
|
|
|
} else if strings.HasPrefix(user, "<@") {
|
|
|
|
user = strings.TrimSuffix(strings.TrimPrefix(user, "<@"), ">")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.ContainsRune(user, '#') {
|
|
|
|
list := strings.Split(user, "#")
|
|
|
|
username := strings.Join(list[:len(list)-1], "#")
|
|
|
|
discriminator := list[len(list)-1]
|
|
|
|
for _, v := range allMembers {
|
|
|
|
if v.User.Username == username && v.User.Discriminator == discriminator {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range allMembers {
|
|
|
|
if v.User.ID == user {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range allMembers {
|
|
|
|
if v.Nick == user {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range allMembers {
|
|
|
|
if v.User.Username == user {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errMemberNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
|
|
|
|
initial, err := s.GuildMembers(guildID, "", 1000)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
next := initial[len(initial)-1].User.ID
|
|
|
|
for next != "" {
|
|
|
|
nextArray, err := s.GuildMembers(guildID, next, 1000)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nextArray) == 0 {
|
|
|
|
next = ""
|
|
|
|
} else {
|
|
|
|
next = nextArray[len(nextArray)-1].User.ID
|
|
|
|
initial = append(initial, nextArray...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return initial, nil
|
|
|
|
}
|