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)
|
2020-07-29 20:10:17 +00:00
|
|
|
if !channelID.IsValid() {
|
2020-05-10 08:45:00 +00:00
|
|
|
return bot.Break
|
|
|
|
}
|
|
|
|
|
|
|
|
var userID = infer.UserID(ev)
|
2020-07-29 20:10:17 +00:00
|
|
|
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 {
|
2021-05-30 21:16:16 +00:00
|
|
|
// Try and infer the GuildID.
|
2020-07-29 20:10:17 +00:00
|
|
|
if guildID := infer.GuildID(ev); guildID.IsValid() {
|
2020-05-10 08:45:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var channelID = infer.ChannelID(ev)
|
2020-07-29 20:10:17 +00:00
|
|
|
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)
|
2020-07-29 20:10:17 +00:00
|
|
|
if err != nil || !c.GuildID.IsValid() {
|
2020-05-10 08:45:00 +00:00
|
|
|
return bot.Break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|