JDA:一旦用户被封禁,机器人就不会通知用户
Posted
技术标签:
【中文标题】JDA:一旦用户被封禁,机器人就不会通知用户【英文标题】:JDA: The bot does not dm the user once they have been ban 【发布时间】:2021-12-11 16:42:27 【问题描述】:当我禁止该成员时,即使认为用户的 dm 是打开的,他们也没有在 dms 中发送消息。
代码在这里
public static void kickUser(@NotNull Member member, @NotNull Member author,
@NotNull String reason, long userId, @NotNull SlashCommandEvent event)
String guildName = event.getGuild().getName();
event.getJDA()
.openPrivateChannelById(userId)
.flatMap(channel -> channel.sendMessage(
"""
Hey there, sorry to tell you but unfortunately you have been kicked from the guild %s.
If you think this was a mistake, please contact a moderator or admin of the guild.
he reason for the kick is: %s
"""
.formatted(guildName, reason)))
.queue(null,
throwable -> logger.info(
"I could not dm the user '' to inform them that they were kicked. ",
userId, throwable));
event.getGuild()
.kick(member, reason)
.flatMap(v -> event.reply(member.getUser().getAsTag() + " was kicked by "
+ author.getUser().getAsTag() + " for: " + reason))
.queue();
logger.info(" ' ()' kicked the user ' ()' due to reason being ''",
author.getUser().getAsTag(), author.getIdLong(), member.getUser().getAsTag(),
userId, reason);
错误消息说即使有 dms 已打开,它也无法 dm 用户
【问题讨论】:
【参考方案1】:如果您与用户共享服务器并且用户没有阻止您,您只能发送直接消息。您需要在禁止/踢用户之前发送直接消息。
例子:
user.openPrivateChannel()
.flatMap(channel -> channel.sendMessage("you are banned")) // send a message to the channel
.mapToResult() // this means the next flatMap runs on success and error signals
.flatMap(result -> guild.ban(member)) // then ban the member
.queue(); // queue the chain of actions
【讨论】:
非常感谢。以上是关于JDA:一旦用户被封禁,机器人就不会通知用户的主要内容,如果未能解决你的问题,请参考以下文章
JDA getMembers() 只返回自机器人启动以来在聊天中写过的用户?
如何使用 JDA 在 Discord 服务器(不是用户/机器人 DM)中创建私人频道:Java Discord API
使用 JDA 制作的 Discord 机器人正在删除不正确的消息