【问题标题】:send questions in dm, and post answers in embed to a channel discord.py在 dm 中发送问题,并将答案嵌入到频道 discord.py
【发布时间】:2021-04-04 23:20:16
【问题描述】:

我试图让它在用户启动命令 $createprofile 时,机器人将向用户发送列表中提供的问题,一个接一个地发布答案,然后将答案嵌入到指定的频道。现在我已经建立了列表,但是我不确定如何建立一个又一个问题,我可能正在考虑使用 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))

【问题讨论】:

    标签: python discord discord.py-rewrite


    【解决方案1】:
    @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)
    

    首先,您需要向用户发送一个问题,因此您可以通过await ctx.author.send("this is a question") 进行操作。然后将该消息存储到一个变量中。然后,您创建一个检查函数,以确保回复的用户实际上是首先发送$createprofile 消息的用户。您还需要检查消息的频道是否为 dm 频道。这就是您之前存储的消息采取行动的地方。它将确保消息的频道与您之前发送给用户的 dm 消息的频道相同。之后,您创建一个异步函数来提出问题。从那里开始它非常简单。随意使用for loops 优化您的代码。希望这会有所帮助。

    【讨论】:

    • 是的,但是您是否必须在 channel = dm.channel 下定义频道,或者我在那个方面错了,每个部分实际上都会像一个问题一样表达,例如你的名字是什么?,什么你的年龄是?所以我试图让它先问你的名字是什么,然后一旦用户回答,它会问下一个
    • @JangoDarkWind 随时提出任何问题
    • 但如果我理解正确,那么 dm 通道和消息通道是分开的,因为现在的主要问题是它发送消息“您将收到两个问题。一个涉及您的姓名和另一个你的年龄。”我回答没有其他事情发生。我运行时的原始代码也给出了错误 ctx has no attribute author,所以我将 dmChannel = await ctx.author.send 更改为 dmChannel = message.author.send 并发送了 dm 否则它会出错
    • 啊,你一定是在使用 on_message 事件。我的错。它现在工作了吗?因为对我来说它工作正常。
    • @JangoDarkWind 在您提供的示例代码中找不到名为“Profile”的对象或类,这意味着没有人可以帮助您解决此错误,因为未提供相关代码。请考虑重写您的问题以包含所有相关代码。
    猜你喜欢
    • 2021-10-17
    • 2021-05-06
    • 2021-04-16
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2018-11-24
    • 2022-07-16
    • 2016-11-30
    相关资源
    最近更新 更多