2022-05-22 19:16:42 +00:00
|
|
|
package main
|
2022-04-14 17:03:44 +00:00
|
|
|
|
|
|
|
import (
|
2023-01-11 16:24:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2022-04-14 17:03:44 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
2023-01-11 16:24:08 +00:00
|
|
|
func ptrBool(val bool) *bool {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) channelIsBridgeable(channel *discordgo.Channel) bool {
|
2022-04-14 17:03:44 +00:00
|
|
|
switch channel.Type {
|
2023-01-11 16:24:08 +00:00
|
|
|
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
|
|
|
|
// allowed
|
|
|
|
default:
|
|
|
|
// everything else is not allowed
|
|
|
|
return false
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 16:24:08 +00:00
|
|
|
hasRole := map[string]bool{
|
|
|
|
channel.GuildID: true,
|
|
|
|
}
|
|
|
|
var roles []string
|
|
|
|
member, err := user.Session.State.Member(channel.GuildID, user.DiscordID)
|
|
|
|
if errors.Is(err, discordgo.ErrStateNotFound) {
|
|
|
|
user.log.Debugfln("Fetching own membership in %s to check own roles", channel.GuildID)
|
|
|
|
member, err = user.Session.GuildMember(channel.GuildID, user.DiscordID)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to get own membership in %s from server to determine own roles for bridging %s: %v", channel.GuildID, channel.ID, err)
|
|
|
|
} else {
|
|
|
|
err = user.Session.State.MemberAdd(member)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to add own membership in %s to cache: %v", channel.GuildID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
user.log.Warnfln("Failed to get own membership in %s from cache to determine own roles for bridging %s: %v", channel.GuildID, channel.ID, err)
|
|
|
|
}
|
|
|
|
if member != nil {
|
|
|
|
roles = member.Roles
|
|
|
|
for _, role := range member.Roles {
|
|
|
|
hasRole[role] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var userAllowed, roleAllowed *bool
|
|
|
|
for _, override := range channel.PermissionOverwrites {
|
|
|
|
if override.Type == discordgo.PermissionOverwriteTypeMember && override.ID == user.DiscordID {
|
|
|
|
if override.Allow&discordgo.PermissionViewChannel > 0 {
|
|
|
|
userAllowed = ptrBool(true)
|
|
|
|
} else if override.Deny&discordgo.PermissionViewChannel > 0 {
|
|
|
|
userAllowed = ptrBool(false)
|
|
|
|
}
|
|
|
|
} else if override.Type == discordgo.PermissionOverwriteTypeRole && hasRole[override.ID] {
|
|
|
|
if override.Allow&discordgo.PermissionViewChannel > 0 {
|
|
|
|
roleAllowed = ptrBool(true)
|
|
|
|
} else if override.Deny&discordgo.PermissionViewChannel > 0 {
|
|
|
|
roleAllowed = ptrBool(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allowed := true
|
|
|
|
if userAllowed != nil {
|
|
|
|
allowed = *userAllowed
|
|
|
|
} else if roleAllowed != nil {
|
|
|
|
allowed = *roleAllowed
|
|
|
|
}
|
|
|
|
if !allowed {
|
|
|
|
dat, _ := json.Marshal(channel.PermissionOverwrites)
|
|
|
|
user.log.Debugfln("Permission overwrites (%s) resulted in %s/%s not being allowed to bridge with roles %+v", dat, channel.GuildID, channel.ID, roles)
|
|
|
|
}
|
|
|
|
return allowed
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|