forked from kageru/discord-selphybot
Commands can now be limited to a channel or set of Channels (fixes #5)
Also extracted the command permission logic to a separate function because it was getting ridiculous. It might be a by slower now, but I really don’t care. Readabiliy should be much better, except now it’s even more obvious how bad the entire idea behind that logic was/is. Shikata ga nai, I guess.
This commit is contained in:
parent
3b672e2f30
commit
982144f059
|
@ -5,6 +5,9 @@ Updates are listed in reverse chronological order.
|
||||||
- a copy of each deleted message is now send via DM to the author (suggested by CommanderLook)
|
- a copy of each deleted message is now send via DM to the author (suggested by CommanderLook)
|
||||||
- seasonal fluff
|
- seasonal fluff
|
||||||
- finally use gofmt
|
- finally use gofmt
|
||||||
|
- better logging of own messages
|
||||||
|
- redirect function is much cleaner now
|
||||||
|
- commands can be limited to certain channels
|
||||||
|
|
||||||
### 1.3
|
### 1.3
|
||||||
- use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy
|
- use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy
|
||||||
|
|
35
command.go
35
command.go
|
@ -41,6 +41,7 @@ type Command struct {
|
||||||
IgnoreCase bool
|
IgnoreCase bool
|
||||||
// for custom commands that go beyond prints and deletions
|
// for custom commands that go beyond prints and deletions
|
||||||
Function func(*discordgo.Session, *discordgo.MessageCreate)
|
Function func(*discordgo.Session, *discordgo.MessageCreate)
|
||||||
|
AllowedChannels mapset.Set // allowed everywhere if blank
|
||||||
|
|
||||||
UsersOnCooldown mapset.Set // don’t set this manually (it’s overwritten anyway)
|
UsersOnCooldown mapset.Set // don’t set this manually (it’s overwritten anyway)
|
||||||
}
|
}
|
||||||
|
@ -114,10 +115,7 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
Sets command cooldowns if necessary and also clears them again.
|
Sets command cooldowns if necessary and also clears them again.
|
||||||
*/
|
*/
|
||||||
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
|
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
|
||||||
if isAdmin(message.Author) || // no restrictions for admins
|
if commandAllowed(session, message, command) {
|
||||||
(!command.AdminOnly && (isDM(session, message) || !command.UsersOnCooldown.Contains(message.Author.ID)) &&
|
|
||||||
(!command.DMOnly || isDM(session, message))) {
|
|
||||||
|
|
||||||
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
|
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
|
||||||
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
|
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
|
||||||
command.UsersOnCooldown.Add(message.Author.ID)
|
command.UsersOnCooldown.Add(message.Author.ID)
|
||||||
|
@ -143,6 +141,35 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check if a user has the permission to trigger a given command.
|
||||||
|
* To be honest, this whole logic is a mess, but I don’t know a better way to handle it.
|
||||||
|
*/
|
||||||
|
func commandAllowed(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) bool {
|
||||||
|
// no restrictions for admins
|
||||||
|
if isAdmin(message.Author) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// blacklist admin commands for everyone else
|
||||||
|
if command.AdminOnly {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// cooldowns are irrelevant in DMs
|
||||||
|
if !isDM(session, message) && command.UsersOnCooldown.Contains(message.Author.ID) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// the command is not limited to DMs or we are inside a DM chat
|
||||||
|
if command.DMOnly && !isDM(session, message) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// no allowed channels = all channels are allowed.
|
||||||
|
// DMs are whitelisted by default
|
||||||
|
if command.AllowedChannels.Cardinality() != 0 && !command.AllowedChannels.Contains(message.ChannelID) && !isDM(session, message) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func removeCooldown(command *Command, uid string) {
|
func removeCooldown(command *Command, uid string) {
|
||||||
time.Sleep(time.Duration(command.Cooldown) * time.Second)
|
time.Sleep(time.Duration(command.Cooldown) * time.Second)
|
||||||
if command.UsersOnCooldown.Contains(uid) {
|
if command.UsersOnCooldown.Contains(uid) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user