[英]Convert message in wait_for to channel with discord.py
我有一個用於創建反應角色的命令的wait_for
方法:
msg = await self.bot.wait_for('message', check=checkChannel, timeout=60)
它利用checkChannel
function 來驗證消息是“取消”(取消創建)還是公會中的頻道。 我的原始代碼:
def checkChannel(msg):
if ctx.message.author == msg.author and ctx.channel == msg.channel:
if (msg.content == "Cancel"):
return True
nonlocal embedChannel
message = msg.content
if message.startswith("<#"):
message = message[2:len(message) - 1]
for channel in self.bot.get_guild(ctx.guild.id).channels:
if str(channel.id) == message:
embedChannel = channel
return True
return False
else:
return False
由於這特別混亂(並且不考慮發送頻道名稱),我嘗試尋找一種更有效的方法並得出這個答案:
channel = await commands.TextChannelConverter().convert(ctx, args)
但是,由於這使用await
並需要async
語法,因此我無法在檢查 function 的上下文中使用它,並且會得到:
RuntimeWarning: coroutine '<directory>.checkChannel' was never awaited
我怎樣才能克服這個問題?
有人告訴我,克服它的最有效方法是創建一個循環(在外部函數內)並在那里調用channel = await commands.TextChannelConverter().convert(ctx, args)
以便它實際上可以被調用。
因此,在這種情況下,它將是:
def checkChannel(msg):
return ctx.message.author == msg.author and ctx.channel == msg.channel
channel_checker = True
while channel_checker == True:
try:
msg = await self.bot.wait_for('message', check=checkChannel, timeout=60)
except asyncio.TimeoutError:
await ctx.send(f"You took too long to respond")
return
if msg == "cancel":
await ctx.send("Cancelling...")
return
try:
embedChannel = await commands.TextChannelConverter().convert(ctx, msg.content)
channel_checker = False
except:
await ctx.send(f"Retry typing in the channel")
#Continue creating...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.