使用 JDA 将消息发送到 Discord 中的特定频道

Send message to specific channel in Discord with JDA

如何让bot发送消息到另一个与bot接收命令不同的频道(特定频道)?

假设机器人在通道 #a 中收到消息 !ban @xxx,如果操作完成,机器人将 ban to user @xxx is given 发送到通道 #b

代码Main.java:

import net.dv8tion.jda.core.*;

public class Main {

    private static String token = "NDk0MjI2Mjk2OTY5MjMyMzk0.DowgCA.j0sQHnBV3wm70rzz7Q78rX0NVPU";
    public static void main(String[] args) throws Exception{

        try {
            JDA api = new JDABuilder(AccountType.BOT).setToken(token).build();
            api.addEventListener(new MyEventListner() );
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

代码MyEventListner.java:

import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

    public void onMessageReceived(MessageReceivedEvent event) {
        if (event.getAuthor().isBot()) return;

            User author = event.getAuthor();
            Message message = event.getMessage();
            String content = message.getContentRaw();
            MessageChannel channel = event.getChannel();
            Member member = event.getMember();
            String nickname = member.getNickname();
            Role role = event.getGuild().getPublicRole();
            //that is the most needed part, I believe
}

答案很简单:

TextChannel textChannel = event.getGuild().getTextChannelsByName("CHANNEL_NAME",true).get(0);
textChannel.sendMessage("MESSAGE").queue();