我已经尝试过多次使用我的java discord bot在提到的成员中添加角色,但是我尝试的任何方法似乎都不起作用。
我想让我的机器人收到!suspend @member time
之类的命令,然后向消息中提到的用户添加一个名为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();
List<Member> member = message.getMentionedMembers(guild);
String name = "";
Role group = content.matches("Suspended") ? guild.getRolesByName("Suspended", true).get(0) : null;
//Member member = new Member(tempuser);
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]);
textChannel.sendMessage(spliced[1] + " you have been suspended for " + spliced[2] + " days.").queue();//Sends the message in the quotations.
guild.getController().addRolesToMember(member, group);
}
}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.
}
}
如何在不弄乱数据类型的情况下向用户添加角色,这似乎是我的主要问题?