1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-12 18:02:59 +00:00
arikawa/bot/extras/middlewares/middlewares.go

50 lines
1,012 B
Go
Raw Normal View History

2020-05-10 08:45:00 +00:00
package middlewares
import (
2020-10-28 22:39:59 +00:00
"github.com/diamondburned/arikawa/v2/bot"
"github.com/diamondburned/arikawa/v2/bot/extras/infer"
"github.com/diamondburned/arikawa/v2/discord"
2020-05-10 08:45:00 +00:00
)
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
}
}