Discord python 随机调查/测验
Posted
技术标签:
【中文标题】Discord python 随机调查/测验【英文标题】:Discord python Random survey/quiz 【发布时间】:2019-04-20 19:54:30 【问题描述】:如何让我的机器人从服务器中随机选择一个用户,并进行简单的调查或测验?
def survey(self, ctx):
await self.bot.say('CyberLife, the compàny that manufactured me, is conducting a users survey.')
await self.bot.say('Would you like to participate?')
#If user says no...
await self.bot.say("Ok, i'll remind you later.")
#If user says yes...
await self.bot.say("Great. Let's start")
await self.bot.say("Would you consider having a relationship with an android that looks like a human?")
#If user answers anything...
await self.bot.say("Do you think technology could become a threat to mankind?")
#If user answers anything...
await self.bot.say("IF you had to live on a deserted island and could only bring one object,")
await self.bot.say("what would it be?")
#If user answers anything...
await self.bot.say("Do you consider yourself dependent on technology")
#If user answers anything...
await self.bot.say("What technology do you most anticipate?")
#If user answers anything...
await self.bot.say("Do you believe in god?")
#If user answers anything...
await self.bot.say("Would you let an android take care of your children?")
#If user answers anything...
await self.bot.say("How much time per day would you say you spend on an electronic device?")
#If user answers anything...
await self.bot.say("If you needed emergency surgery,")
await self.bot.say("would you agree to be operated on by a machine?")
#If user answers anything...
await self.bot.say("Do you think think one day machines could develop consciousness?")
#If user answers anything...
#End of survey
它会随机触发(每1-2小时),调查会提出问题,用户可以回答任何问题(除了第一个),它会移动到下一个直到它结束。提前感谢您,并对我的菜鸟问题感到抱歉!
【问题讨论】:
【参考方案1】:您需要创建一个后台任务,该任务每隔设定的时间运行一次,随机选择一个尚未参与调查的用户。您还需要将响应保存到磁盘,保存到文件或数据库中。
以下是如何实现此类目标的高级示例。该示例将所有响应保存到一个文件中,该文件将在机器人启动时进行检查,以确保没有数据被覆盖。下面的代码使用了discord.py
的async
分支。
import discord
import asyncio
import os
import pickle
import random
client = discord.Client()
def check_members(member_responses):
for member in client.get_all_members():
if member.id not in member_responses:
member_responses[member.id] = 'participate': '', 'answer_1': '', 'answer_2': ''
return member_responses
async def my_background_task():
await client.wait_until_ready()
if os.path.isfile('member_responses.txt'):
member_responses = pickle.load(open('member_responses.txt', 'rb'))
else:
member_responses = check_members()
pickle.dump(member_responses, open('member_responses.txt', 'wb'))
while not client.is_closed:
non_surveyed_members = []
for member_id in member_responses:
if member_responses[member_id]['participate'] == '':
non_surveyed_members.append(member_id)
if len(non_surveyed_members) != 0:
survey_member = await client.get_user_info(random.choice(non_surveyed_members))
await client.send_message(survey_member, 'Do you wish to participate?')
participate_answer = False
while not participate_answer:
response = await client.wait_for_message(author=survey_member)
if response.content in ['Yes', 'No']:
participate_answer = True
member_responses[survey_member.id]['participate'] = response.content
else:
await client.send_message(survey_member, 'Please answer Yes or No')
if response.content == 'Yes':
await client.send_message(survey_member, 'Question 1')
response = await client.wait_for_message(author=survey_member)
member_responses[survey_member.id]['answer_1'] = response.content
await client.send_message(survey_member, 'Question 2')
response = await client.wait_for_message(author=survey_member)
member_responses[survey_member.id]['answer_2'] = response.content
member_responses = check_members(member_responses)
pickle.dump(member_responses, open('member_responses.txt', 'wb'))
await asyncio.sleep(300) # task runs every 5 minutes
client.loop.create_task(my_background_task())
client.run('token')
【讨论】:
我收到了discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received generator
该错误来自client.send_message
。上面的示例代码是抛出它还是你自己的代码?
顺便问一下,没有更有效的方法吗?我可以使用数据库,通过这种方法,当新成员加入时,它不会更新
client.get_user_info
是协程,需要await
。我已经在我的示例中解决了这个问题。在您的第二条评论中,上面的代码是一个高级示例。这是为了演示根据您的初始问题可以做什么,即如何选择随机用户进行调查并保存回复
顺便说一句,我如何循环 response = await bot.wait_for_message(author=survey_member)
直到响应为“是”或“否”?以上是关于Discord python 随机调查/测验的主要内容,如果未能解决你的问题,请参考以下文章