Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic slowmode channel manager to slowmode channel interface #2348

Merged
merged 3 commits into from Dec 7, 2022

Conversation

Sanduhr32
Copy link
Contributor

@Sanduhr32 Sanduhr32 commented Dec 6, 2022

Pull Request Etiquette

Changes

  • Internal code
  • Library interface (affecting end-user code)
  • Documentation (maybe)
  • Other: _____

Closes Issue: NaN

Description

Seems only logical to me to be able to use the generic manager, so you don't have to cast to 3 specific slow mode channels (Text, Thread or Forum).

Example use case from attempting to port JDA-Butler to JDA-v5

public static void announce(StandardGuildMessageChannel channel, Role role, MessageCreateData message, boolean slowmode)
{
    if (slowmode && channel instanceof ISlowmodeChannel)
    {
        ISlowmodeChannel slow = (ISlowmodeChannel) channel;
        ISlowmodeChannelManager<?, ?> slowMan = (ISlowmodeChannelManager<?, ?>) slow.getManager(); // currently resolves to ChannelManager and requires an unchecked cast
        slowMan.setSlowmode(30).queue(v -> slowMan.setSlowmode(0).queueAfter(2, TimeUnit.MINUTES));
    }

    role.getManager().setMentionable(true)                  // make role mentionable
        .flatMap(v -> channel.sendMessage(message))         // send announcement
        .flatMap(m -> {
            if (channel instanceof NewsChannel && channel.getGuild().getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE))
                m.crosspost().queue();                      // publish if it's a news channel
            return role.getManager().setMentionable(false); // make role unmentionable
        })
        .queue();
    }

after overloading the manager return type the first if statement's body becomes

    if (slowmode && channel instanceof ISlowmodeChannel)
    {
        ISlowmodeChannel slow = (ISlowmodeChannel) channel;
        ISlowmodeChannelManager<?, ?> slowMan = slow.getManager();
        slowMan.setSlowmode(30).queue(v -> slowMan.setSlowmode(0).queueAfter(2, TimeUnit.MINUTES));
    }

@MinnDevelopment MinnDevelopment marked this pull request as draft December 6, 2022 15:18
@MinnDevelopment MinnDevelopment marked this pull request as ready for review December 6, 2022 17:05
@MinnDevelopment MinnDevelopment merged commit 936d85f into discord-jda:master Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants