0

我正在尝试让我的 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,我似乎无法弄清楚。

4

1 回答 1

-1

我对 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();
        }
    }
}
于 2018-04-23T03:04:15.837 回答