Discord.py 应用程序机器人 [请求帮助]

时间:2021-03-04 22:46:24

标签: python discord discord.py

df['diff'] = df['Data3'] - df['Data2']

df.sort_values(by='diff', ascending=False)

我遇到的问题是,问题有时会在没有等待答复的情况下发送。我目前收到此错误:url = 'https://pastebin.com/raw/zLE7fRyT' app_questions = requests.get(url) questions = app_questions.text q_list = [] q_list = questions.split("\n") a_list = [] application_channel = config.APPLICATION_LOGS_CHANNEL @bot.command(aliases=['application']) async def apply(ctx): a_list = [] submit_channel = bot.get_channel(application_channel) channel = await ctx.author.create_dm() def check(m): return m.content is not None and m.channel == channel for question in q_list: await asyncio.sleep(0.5) await channel.send(question) msg = await bot.wait_for('message', check=check) a_list.append(msg.content) submit_wait = True print("msg",msg) while msg is not None: await channel.send('End of questions - "submit" to finish') msg = await bot.wait_for('message', check=check) if "submit" in msg.content.lower(): submit_wait = False answers = "\n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1)) submit_msg = f'Application from {msg.author} \nThe answers are:\n{answers}' print("answers", answers) if answers is not None: await submit_channel.send(submit_msg) 我尝试过 Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send' 而不是 while submit_wait 问题如下所示:

1 个答案:

答案 0 :(得分:0)

首先,您还有很多其他问题。

  1. 为什么在立即将其分配给问题之后将 q_list 初始化为 []?这完全没有必要。
  2. a_list 不需要在命令外初始化。这是完全没有意义的。如果要在全局范围内修改变量,请在命令中使用 global a_list 而不是 a_list = []
  3. 您的 check 不能确保回答的人是正确的。
  4. 您的 check 不需要确保消息 contentNone,因为这是不可能的;消息的 content 永远只会是 str(不是 NoneType),并且它也不会为空,因为您不能在 Discord 中发送空消息。
  5. 您的 print 语句不会打印消息的内容;将其转换为 str 不会得到它的 content。而是使用 print("msg",msg.content)
  6. Member.create_dm() 不是必需的;您可以直接向 Member 发送消息。因此,将 channel 设置为 ctx.author 仍然允许您对它们进行 send。这是因为Member implements Messageable
  7. 您打印 printmsg 语句似乎毫无意义;它只会打印用户对最后一个问题的回答。
  8. 与您的报价保持一致。选择一种风格('")并坚持下去。您应该切换引号的唯一时间是避免在字符串中转义引号。
  9. 打印 printanswers 语句可能应该是 print("answers",answers,sep="\n")print(f"answers\n{answers}") 以避免将第一个答案打印在与“answers”相同的行上。
  10. answers 永远不会是 Nonestr.join 怎么会返回 None