Skip to content

Commit

Permalink
Small refactor of a Java moment
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Oct 14, 2024
1 parent 9e4071b commit c7f5cdb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.ComponentsUtil;
import net.dv8tion.jda.internal.utils.Helpers;
import org.jetbrains.annotations.Unmodifiable;

Expand Down Expand Up @@ -228,9 +229,7 @@ default ItemComponent updateComponent(@Nonnull String id, @Nullable ItemComponen
if (!(component instanceof ActionComponent))
continue;
ActionComponent action = (ActionComponent) component;
if (id.equals(action.getId())
|| (action instanceof Button && id.equals(((Button) action).getUrl()))
|| (action instanceof Button && ((Button) action).getSku() != null && id.equals(((Button) action).getSku().getId())))
if (ComponentsUtil.checkButtonIdentity(action, id))
{
if (newComponent == null)
it.remove();
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/ComponentsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.internal.utils;

import net.dv8tion.jda.api.interactions.components.ActionComponent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;

import javax.annotation.Nonnull;

public class ComponentsUtil
{
/** Checks whether the provided component has the {@code identifier} as its custom id, url or SKU id */
public static boolean checkButtonIdentity(@Nonnull ActionComponent component, @Nonnull String identifier)
{
if (identifier.equals(component.getId()))
return true;

if (component instanceof Button)
{
final Button button = (Button) component;
if (identifier.equals(button.getUrl()))
return true;
if (button.getSku() != null)
return identifier.equals(button.getSku().getId());
}

return false;
}
}

0 comments on commit c7f5cdb

Please sign in to comment.