Spring 启动 discord bot 没有响应

Spring boot discord bot doesn' t respond

我使用 spring boot 和 discord4j 依赖创建了一个 discord 机器人。机器人在 discord 服务器上显示为在线,但不响应命令 !todo 应该打印待办事项列表。项目编译没有任何错误。

配置CLASS:

package com.example.demo;
import discord4j.core.DiscordClientBuilder;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.Event;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class ConfigurationClass {



        @Value("${token}")
        private String token;
        @Bean
        public <T extends Event> GatewayDiscordClient gatewayDiscordClient(List<EventListener<T>> eventListeners) {
                GatewayDiscordClient client = DiscordClientBuilder.create(token)
                        .build()
                        .login()
                        .block();

                for (EventListener<T> listener : eventListeners) {
                        client.on(listener.getEventType())
                                .flatMap(listener::execute)
                                .onErrorResume(listener::handleError)
                                .subscribe();

                }

                return client;
        }


}

已创建消息CLASS:


import com.austinv11.servicer.Service;
import discord4j.core.event.domain.message.MessageCreateEvent;
import reactor.core.publisher.Mono;

@Service
public class MessageCreateListener extends MessageListener implements EventListener<MessageCreateEvent> {

    @Override
    public Class<MessageCreateEvent> getEventType() {
        return MessageCreateEvent.class;
    }

    @Override
    public Mono<Void> execute(MessageCreateEvent event) {
        return processCommand(event.getMessage());
    }
} 

消息侦听器 CLASS:

package com.example.demo;

import discord4j.core.object.entity.Message;
import reactor.core.publisher.Mono;

public abstract class MessageListener {

    public Mono<Void> processCommand(Message eventMessage) {
        return Mono.just(eventMessage)
                .filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
                .filter(message -> message.getContent().equalsIgnoreCase("!todo"))
                .flatMap(Message::getChannel)
                .flatMap(channel -> channel.createMessage("Things to do today:\n - write a bot\n - eat lunch\n - play a game"))
                .then();
    }
}

事件侦听器界面:

package com.example.demo;

import discord4j.core.event.domain.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;



public interface EventListener<T extends Event> {

    Logger LOG = LoggerFactory.getLogger(EventListener.class);

    Class<T> getEventType();
    Mono<Void> execute(T event);

    default Mono<Void> handleError(Throwable error) {
        LOG.error("Unable to process " + getEventType().getSimpleName(), error);
        return Mono.empty();
    }
}

您使用的是 com.austinv11.servicer.Service 而不是 org.springframework.stereotype.Service,因此 MessageCreateListener 不会被类路径扫描选中。