Skip to content

Commit

Permalink
feat: whitelist/blacklist guild wide
Browse files Browse the repository at this point in the history
Take a look at the new options in example_config.yml
  • Loading branch information
TheTipo01 committed Aug 21, 2024
1 parent a188e44 commit 584c652
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
8 changes: 5 additions & 3 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ const (
DjRoleEqual = "DJ role is already that role!"

// Blacklist
BlacklistAdded = "User added to the blacklist!"
BlacklistRemoved = "User removed from the blacklist!"
UserInBlacklist = "User is in blacklist!"
BlacklistAdded = "User added to the blacklist!"
BlacklistRemoved = "User removed from the blacklist!"
UserInBlacklist = "User is in blacklist!"
ServerInBlacklist = "Server is in blacklist!"
ServerNotInWhitelist = "Server is not in whitelist!"
)

const (
Expand Down
8 changes: 8 additions & 0 deletions example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ clientsecret: ""
# - clicking "Credentials" on the left bar, then "Create Credentials", then "API key"
youtubeapikey: ""

# If set to true, the bot will only respond to commands in the guilds listed below.
# If set to false, the bot will respond to commands in all guilds, except the ones listed below.
whitelist: true

guildlist:
- 0000000000000000
- 1111111111111111


# DON'T TOUCH THIS IF YOU DON'T KNOW WHAT YOU ARE DOING:
# DB Info, like username, password...
Expand Down
45 changes: 42 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
owner string
// Discord bot token
token string
// Cache for the blacklist
// Cache for the user blacklist
blacklist map[string]bool
// Clients
clients manager.Clients
Expand All @@ -44,6 +44,10 @@ var (
origin string
// Server mutex
serverMutex sync.RWMutex
// If set to true, the bot will only respond to commands coming from guilds in the guild list
whitelist bool
// List of guilds the bot will respond to
guildList map[string]bool
)

func init() {
Expand Down Expand Up @@ -126,6 +130,13 @@ func init() {
server[k].DjRole = dj[k].Role
}

// Load the whitelist
whitelist = cfg.WhiteList
guildList = make(map[string]bool)
for _, g := range cfg.GuildList {
guildList[g] = true
}

// Create folders used by the bot
if _, err = os.Stat(constants.CachePath); err != nil {
if err = os.Mkdir(constants.CachePath, 0755); err != nil {
Expand Down Expand Up @@ -214,6 +225,14 @@ func main() {
}
}

if lit.LogLevel == lit.LogDebug {
lit.Debug("Bot is connected to %d guilds.", len(dg.State.Guilds))

for _, g := range dg.State.Guilds {
lit.Debug("%s: %s", g.Name, g.ID)
}
}

// Wait here until CTRL-C or another term signal is received.
lit.Info("YADMB is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
Expand Down Expand Up @@ -290,8 +309,28 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
constants.UserInBlacklist).
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, nil)
} else {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
if whitelist {
// Whitelist mode: check if the guild is in the list
if guildList[i.GuildID] {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
} else {
embed.SendAndDeleteEmbedInteraction(s, embed.NewEmbed().SetTitle(s.State.User.Username).AddField(constants.ErrorTitle,
constants.ServerNotInWhitelist).
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, nil)
}
} else {
// Blacklist mode: check if the guild is not in the list
if !guildList[i.GuildID] {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
} else {
embed.SendAndDeleteEmbedInteraction(s, embed.NewEmbed().SetTitle(s.State.User.Username).AddField(constants.ErrorTitle,
constants.ServerInBlacklist).
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, nil)
}
}
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Config struct {
Address string `fig:"address"`
Origin string `fig:"origin"`
ApiTokens []apiToken `fig:"apitokens"`
WhiteList bool `fig:"whitelist"`
GuildList []string `fig:"guildlist"`
}

type apiToken struct {
Expand Down

0 comments on commit 584c652

Please sign in to comment.