forked from DiscordBots/lilJudd
feat: improved function of AcceptEditButton
Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
parent
6b612dcec6
commit
5d2ef4fc44
2 changed files with 119 additions and 94 deletions
|
@ -0,0 +1,119 @@
|
||||||
|
/*
|
||||||
|
* lilJudd
|
||||||
|
* Copyright (C) 2023 moonleay
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.moonleay.lilJudd.buttons.matchplanner
|
||||||
|
|
||||||
|
import dev.kord.common.entity.Snowflake
|
||||||
|
import dev.kord.core.behavior.edit
|
||||||
|
import dev.kord.core.behavior.interaction.response.PublicMessageInteractionResponseBehavior
|
||||||
|
import dev.kord.core.entity.Guild
|
||||||
|
import dev.kord.core.entity.User
|
||||||
|
import dev.kord.core.entity.channel.MessageChannel
|
||||||
|
import dev.kord.core.entity.interaction.ButtonInteraction
|
||||||
|
import dev.kord.rest.builder.message.modify.embed
|
||||||
|
import net.moonleay.lilJudd.Bot
|
||||||
|
import net.moonleay.lilJudd.buttons.component.IEditButton
|
||||||
|
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
|
||||||
|
import net.moonleay.lilJudd.data.tables.MatchPlanningData
|
||||||
|
import net.moonleay.lilJudd.util.EmbedUtil
|
||||||
|
import net.moonleay.lilJudd.util.Logger
|
||||||
|
import net.moonleay.lilJudd.util.MessageUtil
|
||||||
|
import org.jetbrains.exposed.sql.and
|
||||||
|
import org.jetbrains.exposed.sql.select
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
class AcceptEditButton() : IEditButton {
|
||||||
|
override val id: String = "public.edit.btn.matchmanagement.accept"
|
||||||
|
|
||||||
|
override suspend fun onInteraction(
|
||||||
|
interaction: ButtonInteraction,
|
||||||
|
response: PublicMessageInteractionResponseBehavior,
|
||||||
|
guild: Guild,
|
||||||
|
user: User
|
||||||
|
) {
|
||||||
|
val m = interaction.message
|
||||||
|
val eb = MessageUtil.getAClonedEmbed(m.embeds[0])
|
||||||
|
var shouldEditButton = false
|
||||||
|
lateinit var mpdd: MatchPlanningDataData
|
||||||
|
var found = false
|
||||||
|
transaction {
|
||||||
|
for (pnr in MatchPlanningData.select {
|
||||||
|
MatchPlanningData.messageid eq (interaction.message.id.value.toString()) and (
|
||||||
|
MatchPlanningData.serverid eq (guild.id.value.toString())) and (
|
||||||
|
MatchPlanningData.channelid eq (interaction.channelId.value.toString()))
|
||||||
|
}) {
|
||||||
|
mpdd = MatchPlanningDataData(
|
||||||
|
pnr[MatchPlanningData.id].value,
|
||||||
|
pnr[MatchPlanningData.serverid],
|
||||||
|
pnr[MatchPlanningData.channelid],
|
||||||
|
pnr[MatchPlanningData.matchtype],
|
||||||
|
pnr[MatchPlanningData.registererid],
|
||||||
|
pnr[MatchPlanningData.roleid],
|
||||||
|
pnr[MatchPlanningData.opponentName],
|
||||||
|
pnr[MatchPlanningData.messageid],
|
||||||
|
pnr[MatchPlanningData.timestamp],
|
||||||
|
pnr[MatchPlanningData.jobstr]
|
||||||
|
)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found || mpdd == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val role = guild.getRoleOrNull(Snowflake(mpdd.roleid)) ?: return
|
||||||
|
val member = interaction.user.asMember(guild.id) ?: return
|
||||||
|
// do the checks and update
|
||||||
|
if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
|
||||||
|
if (member.roleIds.contains(Snowflake(mpdd.roleid))) {
|
||||||
|
Logger.out("Removing role from ${member.username}")
|
||||||
|
member.removeRole(role.id)
|
||||||
|
}
|
||||||
|
// remove the user from the 1st list in the embed
|
||||||
|
Logger.out("Removing ${user.username} from the 1st list in the embed")
|
||||||
|
eb.fields = EmbedUtil.replaceXWithYinValuesAtTable(user.id.value.toString(), "", eb, 1).fields
|
||||||
|
shouldEditButton = true
|
||||||
|
}
|
||||||
|
if (m.embeds[0].fields[1].value.contains(user.id.value.toString())) {
|
||||||
|
Logger.out("Removing ${user.username} from the 2nd list in the embed")
|
||||||
|
eb.fields = EmbedUtil.replaceXWithYinValuesAtTable(user.id.value.toString(), "", eb, 2).fields
|
||||||
|
shouldEditButton = true
|
||||||
|
}
|
||||||
|
if (!m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
|
||||||
|
if (!member.roleIds.contains(Snowflake(mpdd.roleid))) {
|
||||||
|
Logger.out("Adding role to ${member.username}")
|
||||||
|
member.addRole(role.id)
|
||||||
|
}
|
||||||
|
//Add the user to the list in the embed
|
||||||
|
Logger.out("Adding ${user.username} to the 1st list in the embed")
|
||||||
|
eb.fields = EmbedUtil.addXToValuesAtTable(user.id.value.toString(), eb, 1).fields
|
||||||
|
shouldEditButton = true
|
||||||
|
}
|
||||||
|
if (shouldEditButton) {
|
||||||
|
// update the message
|
||||||
|
Bot.bot.kordRef.getChannelOf<MessageChannel>(interaction.channelId)!!.getMessage(m.id).edit {
|
||||||
|
this.embed {
|
||||||
|
this.color = eb.color
|
||||||
|
this.title = eb.title
|
||||||
|
this.description = eb.description
|
||||||
|
this.fields = eb.fields
|
||||||
|
this.footer = eb.footer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,94 +0,0 @@
|
||||||
/*
|
|
||||||
* lilJudd
|
|
||||||
* Copyright (C) 2023 moonleay
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.moonleay.lilJudd.buttons.matchplanner
|
|
||||||
|
|
||||||
import dev.kord.common.entity.Snowflake
|
|
||||||
import dev.kord.core.behavior.edit
|
|
||||||
import dev.kord.core.behavior.interaction.response.PublicMessageInteractionResponseBehavior
|
|
||||||
import dev.kord.core.entity.Guild
|
|
||||||
import dev.kord.core.entity.User
|
|
||||||
import dev.kord.core.entity.channel.MessageChannel
|
|
||||||
import dev.kord.core.entity.interaction.ButtonInteraction
|
|
||||||
import dev.kord.rest.builder.message.modify.embed
|
|
||||||
import net.moonleay.lilJudd.Bot
|
|
||||||
import net.moonleay.lilJudd.buttons.component.IEditButton
|
|
||||||
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
|
|
||||||
import net.moonleay.lilJudd.data.tables.MatchPlanningData
|
|
||||||
import net.moonleay.lilJudd.util.EmbedUtil
|
|
||||||
import org.jetbrains.exposed.sql.and
|
|
||||||
import org.jetbrains.exposed.sql.select
|
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
|
||||||
|
|
||||||
class SignMeUpEditButton() : IEditButton {
|
|
||||||
override val id: String = "public.edit.btn.matchmanagement.accept"
|
|
||||||
|
|
||||||
override suspend fun onInteraction(
|
|
||||||
interaction: ButtonInteraction,
|
|
||||||
response: PublicMessageInteractionResponseBehavior,
|
|
||||||
guild: Guild,
|
|
||||||
user: User
|
|
||||||
) {
|
|
||||||
val m = interaction.message
|
|
||||||
if (!m.embeds[0].fields[0].value.contains(user.id.value.toString())) {
|
|
||||||
lateinit var mpdd: MatchPlanningDataData
|
|
||||||
var found = false
|
|
||||||
transaction {
|
|
||||||
for (pnr in MatchPlanningData.select {
|
|
||||||
MatchPlanningData.messageid eq (interaction.message.id.value.toString()) and (
|
|
||||||
MatchPlanningData.serverid eq (guild.id.value.toString())) and (
|
|
||||||
MatchPlanningData.channelid eq (interaction.channelId.value.toString()))
|
|
||||||
}) {
|
|
||||||
mpdd = MatchPlanningDataData(
|
|
||||||
pnr[MatchPlanningData.id].value,
|
|
||||||
pnr[MatchPlanningData.serverid],
|
|
||||||
pnr[MatchPlanningData.channelid],
|
|
||||||
pnr[MatchPlanningData.matchtype],
|
|
||||||
pnr[MatchPlanningData.registererid],
|
|
||||||
pnr[MatchPlanningData.roleid],
|
|
||||||
pnr[MatchPlanningData.opponentName],
|
|
||||||
pnr[MatchPlanningData.messageid],
|
|
||||||
pnr[MatchPlanningData.timestamp],
|
|
||||||
pnr[MatchPlanningData.jobstr]
|
|
||||||
)
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found || mpdd == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val role = guild.getRoleOrNull(Snowflake(mpdd.roleid)) ?: return
|
|
||||||
val member = interaction.user.asMember(guild.id)
|
|
||||||
if (!member.roleIds.contains(Snowflake(mpdd.roleid))) {
|
|
||||||
member.addRole(role.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add the user to the list in the embed
|
|
||||||
Bot.bot.kordRef.getChannelOf<MessageChannel>(interaction.channelId)!!.getMessage(m.id).edit {
|
|
||||||
this.embed {
|
|
||||||
val temp = EmbedUtil.addXToValuesAtTable(user.id.value.toString(), m.embeds[0], 1)
|
|
||||||
this.color = temp.color
|
|
||||||
this.title = temp.title
|
|
||||||
this.description = temp.description
|
|
||||||
this.fields = temp.fields
|
|
||||||
this.footer = temp.footer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue