[英]send questions in dm, and post answers in embed to a channel discord.py
我試圖讓它在用戶啟動命令 $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))
@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")
。 然后將該消息存儲到一個變量中。 然后,您創建一個檢查 function 以確保回復的用戶實際上是首先發送$createprofile
消息的用戶。 您還需要檢查消息的頻道是否為 dm 頻道。 這就是您之前存儲的消息采取行動的地方。 它將確保消息的頻道與您之前發送給用戶的 dm 消息的頻道相同。 之后,您創建一個異步 function 來提出問題。 從那里開始,它非常簡單。 隨意使用for loops
優化您的代碼。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.