“JDA 成员可能不为空”错误的替代解决方案
Posted
技术标签:
【中文标题】“JDA 成员可能不为空”错误的替代解决方案【英文标题】:alternate solutions to "JDA Member may not be null" error 【发布时间】:2021-11-19 04:59:04 【问题描述】:我正在努力做到这一点,当具有“所有者”角色的人键入静音命令时,它会使用他们@提到的人并赋予他们“静音”角色。
其余代码自行工作,唯一不工作的部分是行
event.getGuild().addRoleToMember(member,event.getGuild().getRoleById(0)).complete();
变量“member”由
定义Member member = event.getGuild().getMemberById(mentionid);
完整的代码块是:
package radishmouse.FoodWorld.Events;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import radishmouse.FoodWorld.FoodWorld;
public class GuildMessageReceived extends ListenerAdapter
public void onGuildMessageReceived(GuildMessageReceivedEvent event)
String[] args = event.getMessage().getContentRaw().split("\\s+");
if (args[0].equalsIgnoreCase(FoodWorld.prefix + "mute"))
if (hasRole("Owner", event))
if (args.length == 2)
String mentionid = args[1].replace("<@!", "").replace(">", "");
Member member = event.getGuild().getMemberById(mentionid);
event.getGuild().addRoleToMember(member, event.getGuild().getRoleById(0)).complete();
EmbedBuilder msg = FoodWorld.sendMessage(null, "idk " + mentionid + member, "Blue");
event.getChannel().sendMessageEmbeds(msg.build()).queue();
else
EmbedBuilder msg = FoodWorld.sendMessage("Specify Who To Mute", "Usage: " + FoodWorld.prefix + "mute [@mention who to mute]", "Blue");
event.getChannel().sendMessageEmbeds(msg.build()).queue();
/* If the bot ever sends a message, then add a ❌ reaction so users can delete that message */
if (event.getAuthor().equals(event.getJDA().getSelfUser()))
event.getMessage().addReaction("❌").queue();
private boolean hasRole(String string, GuildMessageReceivedEvent event)
Boolean toReturn = false;
for(int i=0; i < event.getMember().getRoles().size(); i++)
if("Owner".equals(event.getMember().getRoles().get(i).getName()))
toReturn = true;
return toReturn;
作为参考,我在 youtube 上关注本教程:tutorial。 我对 JDA 不是最熟悉,也不知道如何以更简单的方式完成。
【问题讨论】:
【参考方案1】:而不是解析字符串:
String mentionid = args[1].replace("<@!", "").replace(">", "");
Member member = event.getGuild().getMemberById(mentionid);
使用getMentionedMembers:
List<Member> mentions = event.getMessage().getMentionedMembers();
if (mentions.isEmpty())
EmbedBuilder msg = FoodWorld.sendMessage("Specify Who To Mute", "Usage: " + FoodWorld.prefix + "mute [@mention who to mute]", "Blue");
event.getChannel().sendMessageEmbeds(msg.build()).queue();
else
Member member = mentions.get(0);
event.getGuild().addRoleToMember(member, event.getGuild().getRoleById(0)).queue();
EmbedBuilder msg = FoodWorld.sendMessage(null, "idk " + member.getId() + member, "Blue");
event.getChannel().sendMessageEmbeds(msg.build()).queue();
【讨论】:
感谢工作!我刚刚使用:mentions.get(0)
作为代码中的“成员”。谢谢
以上是关于“JDA 成员可能不为空”错误的替代解决方案的主要内容,如果未能解决你的问题,请参考以下文章
为啥不推荐使用一个 Public OleDbConnection?解决该错误的替代方法:打开的连接过多
Mac Catalyst 的替代 UIActivityViewController 保存对话框或 UIDocumentPickerViewController 抛出错误代码 260 的解决方案
如何解决“连接尝试失败,因为连接方在一段时间后没有正确响应......”错误?