如何使用Java制作的Discord bot分配角色?

时间:2018-04-23 02:28:19

标签: java discord discord-jda

我正在尝试让我的Meeseeks机器人为我的个人服务器分配和删除不和谐的角色。我不太熟悉特殊的方法和命令,而且我没有找到它的运气!

这是我现在的代码;

package discord.meeseeksBot;

import discord.meeseeksBot.Ref2;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

public class App extends ListenerAdapter
{
    public static void main(String[] args) throws Exception
    {
        JDA jda = new 
        JDABuilder(AccountType.BOT).setToken(Ref2.token).buildBlocking();

        jda.addEventListener(new App());
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent evt)
    {
        User objUser = evt.getAuthor();
        MessageChannel objMsgCh = evt.getChannel();
        Message objMsg = evt.getMessage();


        //the prefix to which the bot responds to is "Mr.Meeseeks, "
        if(objMsg.getContentRaw().equalsIgnoreCase(Ref2.prefix+"I need 
help"))
        {
            objMsgCh.sendMessage("Hi, " + objUser.getAsMention() + ", " +  " 
I'm Mr.Meeseeks! Look at me! How can I help?").queue();

            objMsgCh.sendMessage("You can tell me to **ADD** you to a role, 
or **REMOVE** you from a role!").queue();

        }

    }
}

我正在努力让机器人到他回复“Mr.Meeseeks,我需要帮助”的地方 有一个标题角色列表(这些角色没有分层目的,也没有与在线成员分开出现!)你可以选择并适用于你自己。我也希望他能够脱离角色。

我想到的是一个性别代词的角色(例如“她/她”或“他/他”)的例子,这样当在服务器中点击个人资料时,你就能够看看他们会被称为什么。

所以你可以说,“Mr.Meeseeks,加我”她/她的“代词!”并且他会为你做这件事,或者“Mr.Meeseeks,将我从”她/她的“代词中删除!”。

我似乎无法弄清楚Java。

1 个答案:

答案 0 :(得分:0)

我对JDA不太熟悉,因为Discord4J更好,但我可以指出你正确的方向。

您希望使用正则表达式在同一条消息中测试“Mr”,“Meeseeks”,“add”和“me”。然后你可以测试性别代词:

@Override
public void onMessageReceived(MessageReceivedEvent evt) {
    User objUser = evt.getAuthor();
    MessageChannel objMsgCh = evt.getChannel();
    Message objMsg = evt.getMessage();
    String content = objMsg.getContentRaw();
    Guild guild = evt.getGuild();

    //the prefix to which the bot responds to is "Mr.Meeseeks, "
    if (objMsg.getContentRaw().equalsIgnoreCase(Ref2.prefix + "I need help")) {
        objMsgCh.sendMessage("Hi, " + objUser.getAsMention() + ", " + " I'm Mr.Meeseeks! Look at me! How can I help?").queue();

        objMsgCh.sendMessage("You can tell me to **ADD** you to a role, or **REMOVE** you from a role!").queue();

    // Test for "Mr", "Meeseeks", "add", and "me".
    } else if (content.matches("^(?=.*\\badd\\b)(?=.*\\bme\\b)(?=.*\\bto\\b)(?=.*\\bMr\\b)(?=.*\\bMeeseeks\\b).+")) {
        // Test for pronouns (Assuming your roles names are "he/him" and "she/her")
        Role group = content.matches("((she)|(her))") ? guild.getRolesByName("she/her", true).get(0) :
            content.matches("((he)|(him))") ? guild.getRolesByName("he/him", true).get(0) : null;
        if (group == null) {
            // Let the user know if they used an invalid pronoun.
            objMsgCh.sendMessage("Sorry " + objUser.getAsMention() + ", I can't find that role!").queue();
        } else {
            // Assign the role.
            guild.getController().addRolesToMember​(guild.getMember(objUser), group);
            objMsgCh.sendMessage("Added " + objUser.getAsMention() + " to " + group.getName() + "!").queue();
        }
    }
}