scuzzy/permissions/permissions.go

101 lines
1.9 KiB
Go
Raw Permalink Normal View History

package permissions
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
import (
2021-05-23 02:07:57 +00:00
"github.com/bwmarrin/discordgo"
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
"github.com/foxtrot/scuzzy/models"
"strings"
)
type AdminRole struct {
Name string
ID string
}
type Permissions struct {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
AdminRoles []AdminRole
CommandRestrictions []models.CommandRestriction
Config *models.Configuration
}
func New(config *models.Configuration, guild *discordgo.Guild) *Permissions {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
var ars []AdminRole
for _, gRole := range guild.Roles {
for _, aRole := range config.AdminRoles {
if aRole != gRole.Name {
continue
}
ar := AdminRole{
Name: gRole.Name,
ID: gRole.ID,
}
ars = append(ars, ar)
}
}
var crs []models.CommandRestriction
for _, cRes := range config.CommandRestrictions {
cr := models.CommandRestriction{
Command: cRes.Command,
Mode: cRes.Mode,
Channels: cRes.Channels,
}
crs = append(crs, cr)
}
return &Permissions{
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
AdminRoles: ars,
CommandRestrictions: crs,
Config: config,
}
}
func (p *Permissions) CheckAdminRole(m *discordgo.Member) bool {
for _, aR := range p.AdminRoles {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
for _, mID := range m.Roles {
if aR.ID == mID {
return true
}
}
}
return false
}
func (p *Permissions) CheckIgnoredUser(m *discordgo.User) bool {
for _, iU := range p.Config.IgnoredUsers {
if iU == m.ID {
return true
}
}
return false
}
func (p *Permissions) CheckCommandRestrictions(m *discordgo.MessageCreate) bool {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
cName := strings.Split(m.Content, " ")[0]
cName = strings.Replace(cName, p.Config.CommandKey, "", 1)
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
cChanID := m.ChannelID
for _, cR := range p.CommandRestrictions {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
if cName == cR.Command {
for _, cID := range cR.Channels {
if cID == cChanID && cR.Mode == "white" {
return true
} else if cID == cChanID && cR.Mode == "black" {
return false
}
}
if cR.Mode == "white" {
return false
} else {
return true
}
}
}
return true
}