简体   繁体   English

Java Discord4j 如何将消息发送到通道 REST API?

[英]Java Discord4j how to send message to channel REST API?

I try to send message to channel我尝试向频道发送消息

GatewayDiscordClient gatewayDiscordClient = DiscordClient
                .builder("TOKEN")
                .build()
                .login()
                .block();

gatewayDiscordClient.rest().getChannelById(Snowflake.of("ChannelId")).createMessage("p");

But channel doesn't displaythe message in history.但频道不显示历史消息。 How can I fix that?我该如何解决?

Discord4J utilizes the Reactor framework which is "lazily executed" Discord4J 利用了“延迟执行”的Reactor框架

You must .subscribe() or .block() on publishers (usually a Mono<T> or Flux<T> )您必须在发布者上使用.subscribe().block() (通常是Mono<T>Flux<T>

So, to get your code to create the message: gatewayDiscordClient.rest().getChannelById(Snowflake.of("ChannelId")).createMessage("p").subscribe();因此,要让您的代码创建消息: gatewayDiscordClient.rest().getChannelById(Snowflake.of("ChannelId")).createMessage("p").subscribe();

I would advise however, to not use the RestX classes unless you are not using the gateway (which, in your question, is being used).但是,我建议不要使用RestX类,除非您没有使用网关(在您的问题中,正在使用网关)。 Rest classes generally provide less of the helpful abstractions D4J offers and is meant to be used internally by the library or for a webserver, not a bot connected to the gateway. Rest 类通常提供较少的 D4J 提供的有用抽象,并且旨在由库内部使用或用于网络服务器,而不是连接到网关的机器人。

To send a message without using rest classes your code would look like this:要在不使用休息类的情况下发送消息,您的代码将如下所示:

client.getChannelById(channelIdHere)
    .ofType(MessageChannel.class)
    .flatMap(channel -> channel.createMessage("Your content here"))
    .subscribe();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM