首页 > 解决方案 > Discord.py 重写让机器人响应 DM

问题描述

我正在尝试让我的机器人响应接受规则的 DM 消息。

这是我到目前为止所拥有的:

@bot.event
async def on_member_join(member):
  role = discord.utils.get(member.servers.roles, name='Newcomers') #get role
  await bot.add_roles(member, role) #give the role to the user
  await bot.member.send("Welcome to the Moderator Bot Server! I will need you to first read the rules then accept by using the `mbs accept` in this DM")#send a message
  #wait for accept

标签: discord.pydiscord.py-rewritedm

解决方案


这是我的查询代码:

role = discord.utils.get(member.guild.roles, name='Newcomers') #get role
await member.add_roles(role) #give the role to the user
await member.send("Welcome to the Moderator Bot Server! I will need you to first read the rules then accept by using the `mbs accept` in this DM")#send a message

def check(m):
    return isinstance(m.channel, discord.DMChannel) and m.author == member and m.content == "mbs accept"

await bot.wait_for("message", check=check)
#do stuff here; this line runs once the user types `mbs accept` in the DM (maybe you want to send the user a message that thanks them for accepting the rules?)

前 3 行是您提供给我们的,尽管我更新了它们以便它们适用于 discord.py-rewrite(在此处阅读有关 discord.py 重写文档的更多信息。

检查功能

然后,该check函数首先检查发送消息的频道是否为 DM,检查作者是否为会员,检查消息内容是否为mbs accept.

等待函数

然后,它使用我们提供的检查来等待消息。在此处阅读有关该wait_for功能的更多信息。

PS:很抱歉回复晚了,如果您有任何不可预见的错误或有疑问,请随时跟进!


推荐阅读