Search code examples
discord.pydm

Discord.py rewrite have bot respond to DMs


I am trying to have my bot respond to a DM message that accepts the rules.

Here is what I have so far:

@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


Solution

  • Here is my code for your query:

    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?)
    

    The first 3 lines are what you provided us, although I updated them so they work for discord.py-rewrite (read more on the discord.py rewrite docs here.

    Check Function

    Then, the check function first checks if the channel the message was sent in is a DM, it checks if the author is the member, and it checks if the message content is mbs accept.

    Wait For Function

    Then, it waits_for the message, using the check we provided. Read more on the wait_for function here.

    PS: Sorry for the late response, if you have any unforeseen errors or have questions, feel free to follow up!