You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.0 KiB
Go
128 lines
3.0 KiB
Go
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"github.com/bwmarrin/discordgo"
|
||
|
)
|
||
|
|
||
|
type RoleReaction struct {
|
||
|
RoleName string
|
||
|
RoleId string
|
||
|
EmojiName string
|
||
|
EmojiId string
|
||
|
}
|
||
|
|
||
|
type ServerRoleAssignConfig struct {
|
||
|
GuildId string
|
||
|
RoleReactionMap map[string]RoleReaction
|
||
|
Message *discordgo.Message
|
||
|
}
|
||
|
|
||
|
type ServerConfigOptionFunction func(s *discordgo.Session) ServerRoleAssignConfig
|
||
|
|
||
|
func InitRoleReactions(s *discordgo.Session) {
|
||
|
addHubbubHandlers(s)
|
||
|
}
|
||
|
|
||
|
func addHubbubHandlers(s *discordgo.Session) {
|
||
|
hubbubId := "827958531659530301"
|
||
|
roleAssignChannelId := "855923187354959872"
|
||
|
roleMap := map[string]RoleReaction{
|
||
|
"🤓": {
|
||
|
RoleName: "Streamer",
|
||
|
RoleId: "827959638267002880",
|
||
|
EmojiName: "nerd",
|
||
|
EmojiId: "🤓",
|
||
|
},
|
||
|
"😎": {
|
||
|
RoleName: "Viewer",
|
||
|
RoleId: "857793953125629992",
|
||
|
EmojiName: "sunglasses",
|
||
|
EmojiId: "😎",
|
||
|
},
|
||
|
}
|
||
|
serverConfig := getServerConfig(
|
||
|
s,
|
||
|
hubbubId,
|
||
|
roleAssignChannelId,
|
||
|
roleMap,
|
||
|
)
|
||
|
s.AddHandler(roleAddHandler(serverConfig))
|
||
|
s.AddHandler(roleRemoveHandler(serverConfig))
|
||
|
}
|
||
|
|
||
|
func getServerConfig(
|
||
|
s *discordgo.Session,
|
||
|
guildId string,
|
||
|
roleAssignChannelId string,
|
||
|
roleMap map[string]RoleReaction,
|
||
|
) ServerRoleAssignConfig {
|
||
|
serverConfig := ServerRoleAssignConfig{
|
||
|
GuildId: guildId,
|
||
|
RoleReactionMap: roleMap,
|
||
|
}
|
||
|
|
||
|
messages, err := s.ChannelMessages(roleAssignChannelId, 1, "", "", "")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if len(messages) == 0 {
|
||
|
content := "React to this message to get a role!\n"
|
||
|
for emote, role := range roleMap {
|
||
|
content += fmt.Sprintf("%s : %s\n", emote, role.RoleName)
|
||
|
}
|
||
|
message, err := s.ChannelMessageSend(roleAssignChannelId, content)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
for _, role := range roleMap {
|
||
|
err := s.MessageReactionAdd(roleAssignChannelId, message.ID, role.EmojiId)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
serverConfig.Message = message
|
||
|
} else {
|
||
|
serverConfig.Message = messages[0]
|
||
|
}
|
||
|
|
||
|
return serverConfig
|
||
|
}
|
||
|
|
||
|
func roleAddHandler(
|
||
|
serverConfig ServerRoleAssignConfig,
|
||
|
) func(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
||
|
return func(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
||
|
if r.MessageID != serverConfig.Message.ID {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if role, ok := serverConfig.RoleReactionMap[r.Emoji.Name]; ok {
|
||
|
err := s.GuildMemberRoleAdd(serverConfig.GuildId, r.UserID, role.RoleId)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func roleRemoveHandler(
|
||
|
serverConfig ServerRoleAssignConfig,
|
||
|
) func(s *discordgo.Session, r *discordgo.MessageReactionRemove) {
|
||
|
return func(s *discordgo.Session, r *discordgo.MessageReactionRemove) {
|
||
|
if r.MessageID != serverConfig.Message.ID {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if role, ok := serverConfig.RoleReactionMap[r.Emoji.Name]; ok {
|
||
|
err := s.GuildMemberRoleRemove(serverConfig.GuildId, r.UserID, role.RoleId)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|