send questions in dm, and post answers in embed to a channel discord.py

Viewed 658

I am trying to make it where when the user initiates the command $createprofile the bot will dm the user with the questions provided in the list, one after the other then post the answers in an embed to a specified channel. Now I have already built the list, but I am unsure of how to build in asking each question one after another, I am possibly thinking of using asyncio wait_for

import discord
from discord.ext import commands
import platform

import cogs._json

class Profile(commands.Cog):

   def __init__(self, bot):
       self.bot = bot

   @commands.Cog.listener()
   async def on_ready(self):
       print("Profile Cog has been loaded\n-----")

   # @commands.command(aliases=['pm'])
   # async def dm(self, ctx, user: discord.User, *, message=None):
   #   message = message or "This message is sent via dm"
   #   await user.send(message)
   #   await ctx.message.delete()

   @commands.command()
   async def createprofile(ctx, member: discord.Member = None):
       userName = ""
       userAge = ""
       questions = [
           "Please input your name/nickname:",
           "Please input your age:"
       ]
       dmChannel = await ctx.author.send(
           "You will be receiving two questions. One involving your name and the other your age.")

       def check(message):
           return message.author == ctx.author and message.channel == dmChannel.channel

       async def askQuestion(question):
           await ctx.author.send(question)
           print("Waiting for reply...")
           userReply = await client.wait_for('message', check=check)
           print("User replied")
           return userReply.content
           userName = await askQuestion(questions[0])
           userAge = await askQuestion(questions[1])
           e = discord.Embed(title=str(userName) + "'s Profile", description=f"""
           Age: `{str(userAge)}`
           """)
           await ctx.send(embed=e)

def setup(bot):
   bot.add_cog(Profile(bot))
1 Answers
@client.command()
async def createprofile(self, ctx, member: discord.Member = None):
    userName = ""
    userAge = ""
    questions = [
        "Please input your name/nickname:",
        "Please input your age:"
    ]
    dmChannel = await ctx.author.send(
        "Yo will be receiving two questions. One involving your name and the other your age.")

    def check(message):
        return message.author == ctx.author and message.channel == dmChannel.channel

    async def askQuestion(question):
        await ctx.author.send(question)
        print("Waiting for reply...")
        userReply = await client.wait_for('message', check=check)
        print("User replied")
        return userReply.content
    userName = await askQuestion(questions[0])
    userAge = await askQuestion(questions[1])
    e = discord.Embed(title=str(userName) + "'s Profile", description=f"""
    Age: `{str(userAge)}`
    """)
    await ctx.send(embed=e)

First, you need to send the user a question, so how you do that is through await ctx.author.send("this is a question"). You then store that message into a variable. You then create a check function that makes sure that the user who replied is in fact the user who sent the $createprofile message in the first place. You will also need to check if the message's channel is the dm channel. So that's where the message you stored earlier takes action. It will make sure that the channel of the message is the same as the channel of the dm message you sent to the user earlier. After that, you create an async function to ask a question. From there on it's pretty straightforward. Feel free to optimize your code using for loops. Hope this helps.

Related