JDA - send message

前端 未结 2 1805
时光取名叫无心
时光取名叫无心 2020-12-12 07:57

I have my own Discord BOT based on JDA. I need to send a text message to the specific channel. I know how to send the message as onEvent response, but in my situation I do n

2条回答
  •  悲哀的现实
    2020-12-12 08:36

    Ok I think I know what you mean. You don't need to have an event to get an ID of a channel and send a message. The only thing you need to do is to instantiate the JDA, call awaitReady(), from the instance you can get all channels (MessageChannels, TextChannels, VoiceChannels, either by calling

    • get[Text]Channels()
    • get[Text]ChannelById(id=..)
    • get[Text]ChannelsByName(name, ignore case))

    So 1. Instantiate JDA

        JDABuilder builder; 
        JDA jda = builder.build();
        jda.awaitReady();
    
    1. Get Channel

      List channels = jda.getTextChannelsByName("general", true);
      for(TextChannel ch : channels)
      {
          sendMessage(ch, "message");
      }
      
    2. Send message

      static void sendMessage(TextChannel ch, String msg) 
      {
          ch.sendMessage(msg).queue();
      }
      

    Hope it helps.

提交回复
热议问题