Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Support stage channels for proxying
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampflower committed Feb 18, 2023
1 parent f830471 commit aa2fcaf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
19 changes: 14 additions & 5 deletions modules/bot/src/main/kotlin/dev/proxyfox/bot/BotUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.builder.kord.KordBuilder
import dev.kord.core.entity.Member
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.entity.channel.thread.ThreadChannel
Expand Down Expand Up @@ -333,18 +334,26 @@ data class Either<A, B>(
val GuildMessageChannel.sendPermission
get() = if (this is ThreadChannel) Permission.SendMessagesInThreads else Permission.SendMessages

suspend fun GuildMessageChannel.permissionHolder() = if (this is ThreadChannel) kord.getChannel(parentId)!! else this
suspend fun GuildChannel.permissionHolder() = if (this is ThreadChannel) kord.getChannel(parentId)!! else this

suspend fun MessageChannelBehavior.selfCanSend(): Boolean {
return if (this is GuildMessageChannel) selfHasPermissions(Permissions(sendPermission, Permission.ViewChannel)) else true
}

suspend fun GuildMessageChannel.selfHasPermissions(permissions: Permissions): Boolean {
return permissionHolder().getEffectivePermissions(guild.getMember(kord.selfId)).contains(permissions)
suspend fun GuildChannel.selfHasPermissions(permissions: Permissions): Boolean {
return permissionHolder().getEffectivePermissions(guild.getMember(kord.selfId)).adminOrContains(permissions)
}

suspend fun GuildMessageChannel.selfHasPermissions(permission: Permission): Boolean {
return permissionHolder().getEffectivePermissions(guild.getMember(kord.selfId)).contains(permission)
suspend fun GuildChannel.selfHasPermissions(permission: Permission): Boolean {
return permissionHolder().getEffectivePermissions(guild.getMember(kord.selfId)).adminOrContains(permission)
}

fun Permissions.adminOrContains(permissions: Permissions): Boolean {
return contains(Permission.Administrator) || contains(permissions)
}

fun Permissions.adminOrContains(permission: Permission): Boolean {
return contains(Permission.Administrator) || contains(permission)
}

suspend fun Channel.getEffectivePermissions(member: Member): Permissions {
Expand Down
12 changes: 6 additions & 6 deletions modules/bot/src/main/kotlin/dev/proxyfox/bot/MessageHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import dev.kord.common.entity.MessageType
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.behavior.channel.asChannelOfOrNull
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.cache.data.AttachmentData
import dev.kord.core.cache.data.EmbedData
import dev.kord.core.entity.Attachment
import dev.kord.core.entity.Embed
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.event.message.MessageCreateEvent
import dev.kord.core.event.message.MessageUpdateEvent
import dev.kord.core.event.message.ReactionAddEvent
Expand All @@ -41,7 +41,8 @@ private val logger = LoggerFactory.getLogger("MessageHandler")

suspend fun MessageCreateEvent.onMessageCreate() {
val user = message.author ?: return
val channel = message.getChannel()
val channel = message.channel
val guildChannel = channel.asChannelOfOrNull<GuildChannel>()

// Return if bot
if (message.webhookId != null || user.isBot || (message.type != MessageType.Default && message.type != MessageType.Reply)) return
Expand All @@ -66,8 +67,8 @@ suspend fun MessageCreateEvent.onMessageCreate() {
if (output.isNotBlank())
channel.createMessage(output)
}
} else if (channel is GuildMessageChannel && channel.selfHasPermissions(Permissions(Permission.ManageWebhooks, Permission.ManageMessages))) {
val guild = channel.getGuild()
} else if (guildChannel != null && guildChannel.selfHasPermissions(Permissions(Permission.ManageWebhooks, Permission.ManageMessages))) {
val guild = guildChannel.getGuild()
val hasStickers = message.stickers.isNotEmpty()
// TODO: Boost to upload limit; 8 MiB is default.
val hasOversizedFiles = message.attachments.fold(0L) { size, attachment -> size + attachment.size } >= UPLOAD_LIMIT
Expand All @@ -83,7 +84,6 @@ suspend fun MessageCreateEvent.onMessageCreate() {

suspend fun MessageUpdateEvent.onMessageUpdate() {
val guild = kord.getGuild(new.guildId.value ?: return) ?: return
val channel = channel.asChannelOf<GuildMessageChannel>()
val content = new.content.value ?: return
val authorRaw = new.author.value ?: return
if (authorRaw.bot.discordBoolean) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package dev.proxyfox.bot.webhook

import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.MessageBehavior
import dev.kord.core.behavior.channel.GuildMessageChannelBehavior
import dev.kord.core.behavior.channel.MessageChannelBehavior
import dev.kord.core.entity.*

// Created 2022-15-10T00:34:30
Expand All @@ -24,7 +24,7 @@ data class GuildMessage(
val id: Snowflake,
val content: String,
val author: User,
val channel: GuildMessageChannelBehavior,
val channel: MessageChannelBehavior,
val guild: Guild,
val attachments: Collection<Attachment>,
val embeds: Collection<Embed>,
Expand All @@ -34,7 +34,7 @@ data class GuildMessage(
constructor(
message: Message,
guild: Guild,
channel: GuildMessageChannelBehavior,
channel: MessageChannelBehavior,
author: User = message.author!!
) : this(
id = message.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package dev.proxyfox.bot.webhook

import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.behavior.channel.threads.ThreadChannelBehavior
import dev.kord.core.cache.data.WebhookData
import dev.kord.core.entity.Webhook
Expand Down Expand Up @@ -42,7 +43,7 @@ object WebhookUtil {

return ProxyContext(
messageContent,
createOrFetchWebhookFromCache(message.channel.asChannel()),
createOrFetchWebhookFromCache(message.channel.asChannelOf()),
message,
system,
member,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.entity.channel.Channel
import dev.proxyfox.common.ellipsis
import dev.proxyfox.common.fromColor
import dev.proxyfox.common.logger
Expand Down Expand Up @@ -425,9 +427,10 @@ class JsonDatabase(val file: File = File("systems.json")) : Database() {
) {
val message = ProxiedMessageRecord()
message.memberName = memberName
message.userId = userId.value
message.oldMessageId = oldMessageId.value
message.newMessageId = newMessageId.value
val channel = channelBehavior.fetchChannel()
val channel = channelBehavior.asChannelOf<Channel>()
message.channelId = channel.id.value
message.guildId = channel.data.guildId.value?.value ?: 0UL
message.memberId = memberId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoCollection
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.entity.channel.Channel
import dev.proxyfox.database.records.MongoRecord
import dev.proxyfox.database.records.member.MemberProxyTagRecord
import dev.proxyfox.database.records.member.MemberRecord
Expand Down Expand Up @@ -246,7 +248,7 @@ class MongoDatabase(private val dbName: String = "ProxyFox") : Database() {
message.userId = userId.value
message.oldMessageId = oldMessageId.value
message.newMessageId = newMessageId.value
val channel = channelBehavior.fetchChannel()
val channel = channelBehavior.asChannelOf<Channel>()
message.guildId = channel.data.guildId.value?.value ?: 0UL
message.channelId = channel.id.value
message.memberId = memberId
Expand Down

0 comments on commit aa2fcaf

Please sign in to comment.