Добавление ролей упомянутому пользователю в JDA - PullRequest
0 голосов
/ 03 января 2019

Я хочу иметь возможность запустить команду, которая начинается с! Suspend, упоминает пользователя, затем определяет продолжительность и добавляет роль с именем «Suspended» для указанного пользователя на указанную продолжительность.

    Message message = event.getMessage(); //Sets the message equal to the variable type 'Message' so it can be modified within the program.
    String content = message.getContentRaw(); //Gets the raw content of the message and sets it equal to a string (best way to convert types Message to String).
    MessageChannel channel = event.getChannel(); //Gets the channel the message was sent in (useful for sending error messages).
    Guild guild = event.getGuild();
    //Role group = content.matches("((Suspended))") ? guild.getRolesByName("Suspended", true).get(0) : null;

    if(content.startsWith("!suspend"))//Pretty self explanatory. Enters the loop if the message begins with !suspend.
    {
        String[] spliced = content.split("\\s+"); //Splits the message into an array based on the spaces in the message.
        TextChannel textChannel = event.getGuild().getTextChannelsByName("ranked-ms_punishments",true).get(0); //If there is a channel called ranked-ms_punishments which there should be set the value of it equal to the variable.

        int length = spliced.length;//Sets 'length' equal to the number of items in the array.

        if(length == 3)//If the number of items in the array is 3 then...
        {
            if(spliced[1].startsWith("<"))
            {
                list.add(spliced[1]);
                String tempuser = spliced[1];
                textChannel.sendMessage(tempuser + " you have been suspended for " + spliced[2] + " days.").queue();//Sends the message in the quotations.
                //add role to the member
            }
        }else {
            channel.sendMessage("Please use the following format for suspending a user: '!suspend' <@user> (length)").queue(); //If length doesn't equal 3 then it sends the message in quotations.
        }
    }

Не уверен, как это сделать, так как я слишком незнаком с JDA, чтобы заставить его работать.У меня все работает, кроме фактического добавления роли под названием «Приостановлено».

1 Ответ

0 голосов
/ 16 апреля 2019

Добавление роли к упомянутым участникам в сообщении может быть сделано следующим образом:

Role role = event.getGuild().getRolesByName("Suspended", false).get(0);
List<Member> mentionedMembers = message.getMentionedMembers();
for (Member mentionedMember : mentionedMembers) {
    event.getGuild().getController().addRolesToMember(mentionedMember, role).queue();
}

Обратите внимание, что вашему боту понадобится разрешение MANAGE_ROLES для добавления ролей.

...