brian661
brian661

Reputation: 548

Java: get and return invite url from Discord JDA

My java spring boot application have a function to create a text channel on discord and save the created channel data in database.
While I am not able to figure out how to return the created invite url from the function.

public void myFunction(String category, String channelName) {
    String inviteUrl = discordService.createTextChannel();
    MyData data = new MyData();
    data.setCategory(category);
    data.setChannelName(channelName);
    data.setInviteUrl(inviteUrl);
    myRepo.save(data);
}

@Service
@RequiredArgsConstructor
public class DiscordService {

    private final JDA jda;

    @Value("${discord.main.guild.id}")
    private String mainGuildId;

    public String createTextChannel(String categoryName, String channelName) {
        jda.getGuildById(mainGuildId).createCategory(categoryName).queue(
                category -> { category.createTextChannel(channelName).queue(
                        textChannel -> textChannel.createInvite().queue(
                                invite -> System.out.println("The url is " + invite.getUrl()));
        });
        return "return the url";
    }
}

Upvotes: 0

Views: 1727

Answers (1)

Minn
Minn

Reputation: 6134

You should use a callback:

public void createTextChannel(String categoryName, String channelName, Consumer<Invite> callback) {
    jda.getGuildById(mainGuildId)
       .createCategory(categoryName)
       .flatMap(category -> category.createTextChannel(channelName))
       .flatMap(textChannel -> textChannel.createInvite())
       .queue(callback);
}

public void myFunction(String category, String channelName) {
    discordService.createTextChannel(category, channelName, (invite) -> {
        MyData data = new MyData();
        data.setCategory(category);
        data.setChannelName(channelName);
        data.setInviteUrl(invite.getUrl());
        myRepo.save(data);
    });
}

Upvotes: 1

Related Questions