Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - Send message to specific channel in Discord with JDA

How to ask the bot to send a message to another channel (specific channel) that is not the same as the bot receive command?

Let's say bot receives the message !ban @xxx in channel #a and if action is completed, bot sends ban to user @xxx is given to channel #b.

code 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();
        }

    }

}

code 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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer is simple:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...