【问题标题】:How to assign roles with a Discord bot made in Java?如何使用 Java 制作的 Discord 机器人分配角色?
【发布时间】:2018-10-02 23:59:25
【问题描述】:

我正在尝试让我的 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();

        }

    }
}

我正在努力让机器人到达他会回复“Meeseeks 先生,我需要帮助”的地方 带有头衔角色列表(这些角色没有等级目的,也不会与在线成员分开出现!)您可以从中进行选择并应用于自己。我还希望他能够将自己从角色中移除。

我想到的例子是性别代词的角色(即“她/她”或“他/他”),这样当在服务器中点击个人资料时,您就可以看看他们会叫什么。

所以你可以说,“Meeseeks 先生,将我添加到“她/她”代词中!”他会为你这样做,或者“Mr.Meeseeks,将我从“她/她”代词中删除!”。

我似乎无法为 Java 弄明白。

【问题讨论】:

    标签: java discord discord-jda


    【解决方案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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2018-01-17
      • 2018-06-26
      • 1970-01-01
      • 2020-12-30
      • 2017-09-16
      • 2021-04-16
      • 1970-01-01
      • 2020-09-27
      相关资源
      最近更新 更多