github.com/starshine-sys/bcr@v0.21.0/perm_check.go (about)

     1  package bcr
     2  
     3  import "github.com/diamondburned/arikawa/v3/discord"
     4  
     5  // GuildPerms returns the global (guild) permissions of this Context's user.
     6  // If in DMs, it will return the permissions users have in DMs.
     7  func (ctx *Context) GuildPerms() (perms discord.Permissions) {
     8  	if ctx.Guild == nil || ctx.Member == nil {
     9  		return discord.PermissionViewChannel | discord.PermissionSendMessages | discord.PermissionAddReactions | discord.PermissionReadMessageHistory
    10  	}
    11  
    12  	if ctx.Guild.OwnerID == ctx.Author.ID {
    13  		return discord.PermissionAll
    14  	}
    15  
    16  	for _, id := range ctx.Member.RoleIDs {
    17  		for _, r := range ctx.Guild.Roles {
    18  			if id == r.ID {
    19  				if r.Permissions.Has(discord.PermissionAdministrator) {
    20  					return discord.PermissionAll
    21  				}
    22  
    23  				perms |= r.Permissions
    24  				break
    25  			}
    26  		}
    27  	}
    28  
    29  	return perms
    30  }
    31  
    32  // checkOwner returns true if the user can run the command
    33  func (ctx *Context) checkOwner() bool {
    34  	if !ctx.Cmd.OwnerOnly {
    35  		return true
    36  	}
    37  
    38  	for _, i := range ctx.Router.BotOwners {
    39  		if i == ctx.Author.ID.String() {
    40  			return true
    41  		}
    42  	}
    43  
    44  	return false
    45  }
    46  
    47  // CheckBotSendPerms checks if the bot can send messages in a channel
    48  func (ctx *Context) CheckBotSendPerms(ch discord.ChannelID, e bool) bool {
    49  	// if this is a DM channel we always have perms
    50  	if ctx.Channel.ID == ch && ctx.Message.GuildID == 0 {
    51  		return true
    52  	}
    53  
    54  	perms, err := ctx.State.Permissions(ch, ctx.Bot.ID)
    55  	if err != nil {
    56  		return false
    57  	}
    58  
    59  	// if the bot doesn't have permission to send messages to the channel, return
    60  	if !perms.Has(discord.PermissionViewChannel) || !perms.Has(discord.PermissionSendMessages) {
    61  		return false
    62  	}
    63  
    64  	// if the bot requires embed links but doesn't have it, return false
    65  	if e && !perms.Has(discord.PermissionEmbedLinks) {
    66  		// but we *can* send an error message (at least probably, we've checked for perms already)
    67  		ctx.State.SendMessage(ch, ":x: I do not have permission to send embeds in this channel. Please ensure I have the `Embed Links` permission here.")
    68  		return false
    69  	}
    70  
    71  	return true
    72  }