Permalink
Browse files

Support IsChannelOwner and convert some uses of permissions to isMode…

…rator.
  • Loading branch information...
1 parent 3d71e98 commit d3a1988c1aaa7a9377b2b635bd9204dedcb4e063 @iopred committed Dec 11, 2016
Showing with 45 additions and 35 deletions.
  1. +1 −1 bot.go
  2. +14 −2 discord.go
  3. +1 −0 interfaces.go
  4. +5 −0 irc.go
  5. +3 −14 liveplugin/liveplugin.go
  6. +5 −0 slack.go
  7. +1 −6 statsplugin/statsplugin.go
  8. +1 −11 wormholeplugin/wormholeplugin.go
  9. +14 −1 youtube.go
View
@@ -14,7 +14,7 @@ import (
)
// VersionString is the current version of the bot
-const VersionString string = "0.9.1"
+const VersionString string = "0.9.2"
type serviceEntry struct {
Service
View
@@ -336,8 +336,8 @@ func (d *Discord) IsPrivate(message Message) bool {
return err == nil && c.IsPrivate
}
-// IsModerator returns whether or not the sender of a message is a moderator.
-func (d *Discord) IsModerator(message Message) bool {
+// IsChannelOwner returns whether or not the sender of a message is a moderator.
+func (d *Discord) IsChannelOwner(message Message) bool {
c, err := d.Channel(message.Channel())
if err != nil {
return false
@@ -349,6 +349,18 @@ func (d *Discord) IsModerator(message Message) bool {
return g.OwnerID == message.UserID()
}
+// IsModerator returns whether or not the sender of a message is a moderator.
+func (d *Discord) IsModerator(message Message) bool {
+ p, err := d.UserChannelPermissions(message.UserID(), message.Channel())
+ if err == nil {
+ if p&discordgo.PermissionManageChannels == discordgo.PermissionManageChannels || p&discordgo.PermissionManageServer == discordgo.PermissionManageServer {
+ return true
+ }
+ }
+
+ return false
+}
+
// ChannelCount returns the number of channels the bot is in.
func (d *Discord) ChannelCount() int {
return len(d.Guilds())
View
@@ -50,6 +50,7 @@ type Service interface {
PrivateMessage(userID, messageID string) error
IsBotOwner(message Message) bool
IsPrivate(message Message) bool
+ IsChannelOwner(message Message) bool
IsModerator(message Message) bool
SupportsPrivateMessages() bool
SupportsMultiline() bool
View
@@ -200,6 +200,11 @@ func (i *IRC) IsPrivate(message Message) bool {
return message.UserName() == message.Channel()
}
+// IsChannelOwner returns whether or not the sender of a message is the owner.
+func (i *IRC) IsChannelOwner(message Message) bool {
+ return false
+}
+
// IsModerator returns whether or not the sender of a message is a moderator.
func (i *IRC) IsModerator(message Message) bool {
return false
@@ -10,7 +10,6 @@ import (
"google.golang.org/api/youtube/v3"
"github.com/iopred/bruxism"
- "github.com/iopred/discordgo"
)
type livePlugin struct {
@@ -114,19 +113,9 @@ func (p *livePlugin) Message(bot *bruxism.Bot, service bruxism.Service, message
service.SendMessage(messageChannel, fmt.Sprintf("Incorrect command. eg: %s%slive [add|remove|list] <%s>%s", ticks, service.CommandPrefix(), "UCGmC0A8mEAPdlELQdP9xJbw", ticks))
}
- isAuthorized := service.IsModerator(message)
-
- if service.Name() == bruxism.DiscordServiceName {
- discord := service.(*bruxism.Discord)
- p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
- if err == nil {
- isAuthorized = isAuthorized || (p&discordgo.PermissionManageRoles != 0) || (p&discordgo.PermissionManageChannels != 0) || (p&discordgo.PermissionManageServer != 0)
- }
- }
-
switch parts[0] {
case "list":
- if !isAuthorized {
+ if !service.IsModerator(message) {
service.SendMessage(messageChannel, "I'm sorry, you must be the channel owner to list live announcements.")
return
}
@@ -142,7 +131,7 @@ func (p *livePlugin) Message(bot *bruxism.Bot, service bruxism.Service, message
service.SendMessage(messageChannel, fmt.Sprintf("Currently announcing: %s", strings.Join(list, ",")))
}
case "add":
- if !isAuthorized {
+ if !service.IsModerator(message) {
service.SendMessage(messageChannel, "I'm sorry, you must be the channel owner to add live announcements.")
return
}
@@ -157,7 +146,7 @@ func (p *livePlugin) Message(bot *bruxism.Bot, service bruxism.Service, message
}
service.SendMessage(messageChannel, fmt.Sprintf("Messages will be sent here when %s goes live.", p.ytLiveChannel.ChannelName(parts[1])))
case "remove":
- if !isAuthorized {
+ if !service.IsModerator(message) {
service.SendMessage(messageChannel, "I'm sorry, you must be the channel owner to remove live announcements.")
return
}
View
@@ -208,6 +208,11 @@ func (s *Slack) IsPrivate(message Message) bool {
return false
}
+// IsChannelOwner returns whether or not the sender of a message is the owner.
+func (s *Slack) IsChannelOwner(message Message) bool {
+ return false
+}
+
// IsModerator returns whether or not the sender of a message is a moderator.
func (s *Slack) IsModerator(message Message) bool {
return false
@@ -101,12 +101,7 @@ func StatsCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Mes
if service.SupportsMultiline() {
service.SendMessage(message.Channel(), out)
} else {
- lines := strings.Split(out, "\n")
- for _, line := range lines {
- if err := service.SendMessage(message.Channel(), line); err != nil {
- break
- }
- }
+ service.SendMessage(message.Channel(), strings.Join(strings.Split(out, "\n"), " "))
}
}
@@ -65,17 +65,7 @@ func (p *wormholePlugin) Save() ([]byte, error) {
// Help returns a list of help strings that are printed when the user requests them.
func (p *wormholePlugin) Help(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
- isAuthorized := service.IsModerator(message)
-
- if service.Name() == bruxism.DiscordServiceName {
- discord := service.(*bruxism.Discord)
- p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
- if err == nil {
- isAuthorized = isAuthorized || (p&discordgo.PermissionManageRoles != 0) || (p&discordgo.PermissionManageChannels != 0) || (p&discordgo.PermissionManageServer != 0)
- }
- }
-
- if isAuthorized {
+ if service.IsModerator(message) {
if detailed {
return []string{
bruxism.CommandHelp(service, "wormhole", "info", "Learn more about wormholes.")[0],
View
@@ -545,14 +545,27 @@ func (yt *YouTube) CommandPrefix() string {
// IsBotOwner returns whether or not a message sender was the owner of the bot.
func (yt *YouTube) IsBotOwner(message Message) bool {
- return false
+ m, ok := message.(*LiveChatMessage)
+ if !ok {
+ return false
+ }
+ return m.AuthorDetails.ChannelId == "UCGmC0A8mEAPdlELQdP9xJbw"
}
// IsPrivate returns whether or not a message was private.
func (yt *YouTube) IsPrivate(message Message) bool {
return false
}
+// IsChannelOwner returns whether or not the sender of a message is the owner.
+func (yt *YouTube) IsChannelOwner(message Message) bool {
+ m, ok := message.(*LiveChatMessage)
+ if !ok {
+ return false
+ }
+ return m.AuthorDetails.IsChatOwner
+}
+
// IsModerator returns whether or not the sender of a message is a moderator.
func (yt *YouTube) IsModerator(message Message) bool {
m, ok := message.(*LiveChatMessage)

0 comments on commit d3a1988

Please sign in to comment.