go: Move discord into its own package
parent
9332b2688e
commit
d1588fffcf
@ -1,147 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
const guildID = "626424729234046987"
|
||||
const memberRoleID = "626434632614805526"
|
||||
const auditChannel = "631789849929711627"
|
||||
|
||||
var errMemberNotFound = errors.New("messageutil: the requested member was not found")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func nonMembersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
||||
member, err := s.GuildMember(guildID, m.Author.ID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for _, v := range member.Roles {
|
||||
if v == memberRoleID {
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+",该指令只能被非会员使用。\n"+m.Author.Mention()+", this command can only be used by non-member.",
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
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) {
|
||||
_, err := s.ChannelMessageSend(auditChannel, message)
|
||||
if err != nil {
|
||||
fmt.Println("==Audit Log==")
|
||||
fmt.Println(message)
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/discord/config"
|
||||
)
|
||||
|
||||
var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found")
|
||||
|
||||
func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
|
||||
initial, err := s.GuildMembers(config.GuildID, "", 1000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
next := initial[len(initial)-1].User.ID
|
||||
for next != "" {
|
||||
nextArray, err := s.GuildMembers(config.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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
const GuildID = "626424729234046987"
|
||||
const MemberRoleID = "626434632614805526"
|
||||
const AuditChannel = "631789849929711627"
|
||||
const VoteChannel = "627164246056239104"
|
||||
const AnnounceCustomChannel = "627165467269922864"
|
||||
const VoteSuggestionChannel = "627164561644191744"
|
@ -0,0 +1,41 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/discord/modules"
|
||||
)
|
||||
|
||||
func StartBot(token string, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
dg, err := discordgo.New("Bot " + strings.TrimSpace(token))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dg.AddHandler(ProcessCommand)
|
||||
dg.AddHandler(ProcessVote)
|
||||
dg.AddHandler(modules.CheckForVote)
|
||||
dg.AddHandler(modules.CheckForTrustUpdate)
|
||||
dg.AddHandler(modules.CheckForInvite)
|
||||
|
||||
if err := dg.Open(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||
go modules.ListenToVoteFinishes(dg)
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||
<-sc
|
||||
|
||||
dg.Close()
|
||||
fmt.Println("Discordgo exited successfully.")
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/discord/config"
|
||||
"TerraOceanBot/discord/modules"
|
||||
)
|
||||
|
||||
func ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
command := strings.Split(m.Content, " ")
|
||||
for _, v := range modules.Commands {
|
||||
if v.Name == command[0] {
|
||||
v.Handler(s, m, command)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ProcessVote(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
//TODO Factorize this code into a command
|
||||
if m.ChannelID == config.VoteSuggestionChannel {
|
||||
modules.VoteSuggestion(s, m)
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package modules
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Handler func(s *discordgo.Session, m *discordgo.MessageCreate, commands []string)
|
||||
}
|
||||
|
||||
var Commands = []Command{
|
||||
Command{
|
||||
Name: "!sendas",
|
||||
Handler: sendAs,
|
||||
},
|
||||
Command{
|
||||
Name: "!editas",
|
||||
Handler: editAs,
|
||||
},
|
||||
Command{
|
||||
Name: "!peektrust",
|
||||
Handler: viewTrustLevel,
|
||||
},
|
||||
Command{
|
||||
Name: "!invite",
|
||||
Handler: createInvite,
|
||||
},
|
||||
Command{
|
||||
Name: "!validate",
|
||||
Handler: checkUseInvite,
|
||||
},
|
||||
Command{
|
||||
Name: "!trust",
|
||||
Handler: changeTrust,
|
||||
},
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package modules
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
@ -0,0 +1,78 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/discord/config"
|
||||
)
|
||||
|
||||
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(config.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 == config.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
|
||||
}
|
||||
|
||||
func nonMembersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
||||
member, err := s.GuildMember(config.GuildID, m.Author.ID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for _, v := range member.Roles {
|
||||
if v == config.MemberRoleID {
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+",该指令只能被非会员使用。\n"+m.Author.Mention()+", this command can only be used by non-member.",
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
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) {
|
||||
_, err := s.ChannelMessageSend(config.AuditChannel, message)
|
||||
if err != nil {
|
||||
fmt.Println("==Audit Log==")
|
||||
fmt.Println(message)
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
@ -1,66 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"sync"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"TerraOceanBot/discord"
|
||||
)
|
||||
|
||||
const voteSuggestionChannel = "627164561644191744"
|
||||
|
||||
func main() {
|
||||
token, _ := ioutil.ReadFile("token.txt")
|
||||
dg, err := discordgo.New("Bot " + strings.TrimSpace(string(token)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dg.AddHandler(messageCreate)
|
||||
dg.AddHandler(checkForVote)
|
||||
dg.AddHandler(checkForTrustUpdate)
|
||||
dg.AddHandler(checkForInvite)
|
||||
|
||||
if err := dg.Open(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||
go listenToVoteFinishes(dg)
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||
<-sc
|
||||
|
||||
dg.Close()
|
||||
}
|
||||
|
||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
if m.ChannelID == voteSuggestionChannel {
|
||||
voteSuggestion(s, m)
|
||||
return
|
||||
}
|
||||
|
||||
command := strings.Split(m.Content, " ")
|
||||
switch command[0] {
|
||||
case "!sendas":
|
||||
sendAs(s, m, command)
|
||||
case "!editas":
|
||||
editAs(s, m, command)
|
||||
case "!peektrust":
|
||||
viewTrustLevel(s, m, command)
|
||||
case "!invite":
|
||||
createInvite(s, m, command)
|
||||
case "!validate":
|
||||
checkUseInvite(s, m, command)
|
||||
case "!trust":
|
||||
changeTrust(s, m, command)
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go discord.StartBot(string(token), &wg)
|
||||
wg.Wait()
|
||||
}
|
||||
|
Loading…
Reference in New Issue