1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 20:32:48 +00:00
arikawa/bot/extras/middlewares/middlewares.go

50 lines
1,003 B
Go
Raw Normal View History

2020-05-10 08:45:00 +00:00
package middlewares
import (
"github.com/diamondburned/arikawa/bot"
"github.com/diamondburned/arikawa/bot/extras/infer"
"github.com/diamondburned/arikawa/discord"
)
func AdminOnly(ctx *bot.Context) func(interface{}) error {
return func(ev interface{}) error {
var channelID = infer.ChannelID(ev)
if !channelID.IsValid() {
2020-05-10 08:45:00 +00:00
return bot.Break
}
var userID = infer.UserID(ev)
if !userID.IsValid() {
2020-05-10 08:45:00 +00:00
return bot.Break
}
2020-05-11 01:45:42 +00:00
p, err := ctx.Permissions(channelID, userID)
2020-05-10 08:45:00 +00:00
if err == nil && p.Has(discord.PermissionAdministrator) {
return nil
}
return bot.Break
}
}
func GuildOnly(ctx *bot.Context) func(interface{}) error {
return func(ev interface{}) error {
// Try and infer the GuildID.
if guildID := infer.GuildID(ev); guildID.IsValid() {
2020-05-10 08:45:00 +00:00
return nil
}
var channelID = infer.ChannelID(ev)
if !channelID.IsValid() {
2020-05-10 08:45:00 +00:00
return bot.Break
}
2020-05-11 01:45:42 +00:00
c, err := ctx.Channel(channelID)
if err != nil || !c.GuildID.IsValid() {
2020-05-10 08:45:00 +00:00
return bot.Break
}
return nil
}
}