Features: Add Custom Joinable/Leavable Roles

master
Marc Egerton 2020-09-18 12:32:19 +01:00
parent 10a29a6afc
commit fc19d1fe3f
No known key found for this signature in database
GPG Key ID: A11F9CA36DF845E0
8 changed files with 189 additions and 2 deletions

View File

@ -23,6 +23,10 @@
{ "color": "pink", "id": "697965484313935966" }
],
"custom_roles": [
{ "name": "WiFi Pineapple Beta", "short_name": "pineapple", "id": "756469985030832158" }
],
"ignored_users": [],
"logging_channel": "714369335254188053"

View File

@ -3,7 +3,10 @@ package features
import (
"errors"
discordgo "github.com/bwmarrin/discord.go"
"log"
"os"
"strings"
"time"
)
func (f *Features) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCreate) error {
@ -31,3 +34,50 @@ func (f *Features) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCre
return nil
}
func (f *Features) handleDisconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
return errors.New("I'm sorry Dave, I'm afraid I can't do that.")
}
msg := f.CreateDefinedEmbed("Disconnect", "Attempting Disconnect...", "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
err = s.Close()
if err != nil {
return err
}
os.Exit(0)
return nil
}
func (f *Features) handleReconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
t := time.Now()
err := s.Close()
if err != nil {
return err
}
err = s.Open()
if err != nil {
log.Fatal(err)
}
msg := f.CreateDefinedEmbed("Reconnect", "Reconnected Successfully.\nTime: `"+time.Since(t).String()+"`.", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
return nil
}

116
features/customroles.go Normal file
View File

@ -0,0 +1,116 @@
package features
import (
discordgo "github.com/bwmarrin/discord.go"
"strings"
)
func (f *Features) handleListCustomRoles(s *discordgo.Session, m *discordgo.MessageCreate) error {
msgC := "You can choose from the following roles:\n\n"
for _, v := range f.Config.CustomRoles {
msgC += "<@&" + v.ID + "> (" + v.ShortName + ")\n"
}
msgC += "\n\n Use `" + f.Config.CommandKey + "joinrole <role_name>` to join a role.\n"
msgC += "Example: `" + f.Config.CommandKey + "joinrole pineapple`.\n"
msg := f.CreateDefinedEmbed("Joinable Roles", msgC, "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
return nil
}
func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
rUserID := m.Author.ID
userInput := strings.Split(m.Content, " ")
if len(userInput) < 2 {
err = f.handleListCustomRoles(s, m)
return err
}
desiredRole := userInput[1]
desiredRole = strings.ToLower(desiredRole)
desiredRoleID := ""
for _, role := range f.Config.CustomRoles {
if role.ShortName == desiredRole {
desiredRoleID = role.ID
break
}
}
if len(desiredRoleID) == 0 {
err = f.handleListCustomRoles(s, m)
return err
}
err = s.GuildMemberRoleAdd(m.GuildID, rUserID, desiredRoleID)
if err != nil {
return err
} else {
msg := f.CreateDefinedEmbed("Join Role", "<@"+m.Author.ID+">: You have joined <@&"+desiredRoleID+">!", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
}
err = s.ChannelMessageDelete(m.ChannelID, m.ID)
if err != nil {
return err
}
return nil
}
func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
rUserID := m.Author.ID
userInput := strings.Split(m.Content, " ")
if len(userInput) < 2 {
err = f.handleListCustomRoles(s, m)
return err
}
desiredRole := userInput[1]
desiredRole = strings.ToLower(desiredRole)
desiredRoleID := ""
for _, role := range f.Config.CustomRoles {
if role.ShortName == desiredRole {
desiredRoleID = role.ID
break
}
}
if len(desiredRoleID) == 0 {
err = f.handleListCustomRoles(s, m)
return err
}
err = s.GuildMemberRoleRemove(m.GuildID, rUserID, desiredRoleID)
if err != nil {
return err
} else {
msg := f.CreateDefinedEmbed("Leave Role", "<@"+m.Author.ID+">: You have left <@&"+desiredRoleID+">!", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
}
err = s.ChannelMessageDelete(m.ChannelID, m.ID)
if err != nil {
return err
}
return nil
}

View File

@ -29,6 +29,9 @@ func (f *Features) RegisterHandlers() {
// User Settings
f.RegisterCommand("colors", f.handleUserColors)
f.RegisterCommand("color", f.handleUserColor)
f.RegisterCommand("listroles", f.handleListCustomRoles)
f.RegisterCommand("joinrole", f.handleJoinCustomRole)
f.RegisterCommand("leaverole", f.handleLeaveCustomRole)
// Conversion Helpers
f.RegisterCommand("ctof", f.handleCtoF)

View File

@ -51,12 +51,15 @@ func (f *Features) CreateDefinedEmbed(title string, desc string, status string,
}
func (f *Features) CreateCustomEmbed(embedData *models.CustomEmbed) *discordgo.MessageEmbed {
var typ discordgo.EmbedType
var ftr discordgo.MessageEmbedFooter
var img discordgo.MessageEmbedImage
var thm discordgo.MessageEmbedThumbnail
var prv discordgo.MessageEmbedProvider
var atr discordgo.MessageEmbedAuthor
typ = discordgo.EmbedType(embedData.Type)
ftr.Text = embedData.FooterText
ftr.IconURL = embedData.FooterImageURL
@ -77,7 +80,7 @@ func (f *Features) CreateCustomEmbed(embedData *models.CustomEmbed) *discordgo.M
msg := discordgo.MessageEmbed{
URL: embedData.URL,
Type: embedData.Type,
Type: typ,
Title: embedData.Title,
Description: embedData.Desc,
Timestamp: "",

View File

@ -299,6 +299,9 @@ func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate)
desc += "\n__User Settings__\n"
desc += "`colors` - Available color roles\n"
desc += "`color` - Set an available color role\n"
desc += "`listroles` - List available roles\n"
desc += "`joinrole` - Join an available role\n"
desc += "`leaverole` - Leave a joined role\n"
desc += "\n__Conversion Helpers__\n"
desc += "`ctof` - Convert Celsius to Farenheit\n"

View File

@ -49,6 +49,7 @@ func (f *Features) handleUserColor(s *discordgo.Session, m *discordgo.MessageCre
for _, role := range f.Config.ColorRoles {
if role.Name == roleColorName {
roleColorID = role.ID
break
}
}
if len(roleColorID) == 0 {

View File

@ -7,6 +7,12 @@ type ColorRole struct {
ID string `json:"id"`
}
type CustomRole struct {
Name string `json:"role_name"`
ShortName string `json:"short_name"`
ID string `json:"id"`
}
type CommandRestriction struct {
Command string `json:"command"`
Mode string `json:"mode"`
@ -27,7 +33,8 @@ type Configuration struct {
CommandRestrictions []CommandRestriction `json:"command_restrictions"`
ColorRoles []ColorRole `json:"color_roles"`
ColorRoles []ColorRole `json:"color_roles"`
CustomRoles []CustomRole `json:"custom_roles"`
IgnoredUsers []string `json:"ignored_users"`