在Discord.py中发送两条私人消息并从两条消息中获取输入涉及以下基础概念:
asyncio
库来处理异步操作是必要的。以下是一个示例代码,展示如何在Discord.py中发送两条私人消息并从两条消息中获取输入:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.messages = True
intents.guilds = True
intents.dm_messages = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
@bot.command()
async def send_dm(ctx):
user = ctx.author
await user.send("Hello! This is your first DM from the bot.")
def check(m):
return m.author == user and m.channel == user.dm_channel
try:
msg1 = await bot.wait_for('message', timeout=60.0, check=check)
await user.send(f"You said: {msg1.content}")
await user.send("Please send another message.")
msg2 = await bot.wait_for('message', timeout=60.0, check=check)
await user.send(f"You said again: {msg2.content}")
except asyncio.TimeoutError:
await user.send("You took too long to respond.")
bot.run('YOUR_BOT_TOKEN')
asyncio.TimeoutError
。try-except
块捕获并处理超时错误。check
函数可能无法正确识别消息。check
函数正确设置,检查消息的作者和频道。通过以上步骤和代码示例,你应该能够在Discord.py中成功发送两条私人消息并从两条消息中获取输入。
领取专属 10元无门槛券
手把手带您无忧上云