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.

165 lines
3.9 KiB
Go

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) {
addHubbubRoleHandlers(s)
addHubbubPronounHandlers(s)
}
func addHubbubRoleHandlers(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,
"React to this message to get a role!",
roleMap,
)
s.AddHandler(roleAddHandler(serverConfig))
s.AddHandler(roleRemoveHandler(serverConfig))
}
func addHubbubPronounHandlers(s *discordgo.Session) {
hubbubId := "827958531659530301"
roleAssignChannelId := "932432742547869726"
roleMap := map[string]RoleReaction{
"🇭": {
RoleName: "He/Him",
RoleId: "932433573292699699",
EmojiName: "regional_indicator_h",
EmojiId: "🇭",
},
3 years ago
"🇸": {
RoleName: "She/Her",
RoleId: "932433721750081587",
3 years ago
EmojiName: "regional_indicator_s",
EmojiId: "🇸",
},
"🇹": {
RoleName: "They/Them",
RoleId: "932433758290853908",
EmojiName: "regional_indicator_t",
EmojiId: "🇹",
},
}
serverConfig := getServerConfig(
s,
hubbubId,
roleAssignChannelId,
"React to this message to assign pronouns! You are allowed to select multiple.",
roleMap,
)
s.AddHandler(roleAddHandler(serverConfig))
s.AddHandler(roleRemoveHandler(serverConfig))
}
func getServerConfig(
s *discordgo.Session,
guildId string,
roleAssignChannelId string,
roleAssignMessage 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 := fmt.Sprintf("%s\n", roleAssignMessage)
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)
}
}
}
}