简体   繁体   中英

Discord python Random survey/quiz

How can i make my bot to choose a random user from the server, and make a simple survey or quiz?

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

It will trigger randomly(every 1-2 hours), the survey will make questions, the user can answer anything(except for the first one), it will move onto the next one until it reaches the end. Thank you in advance and sorry for my noob questions!

You would need to create a background task that runs every set amount of time, picking a random user that hasn't participated in the survey yet. You would also need to save the responses to disk, either to a file or to a database.

Below is a high level example of how to achieve something like this. The example will save all responses to a file, which will be checked when the bot is started to ensure that no data is overwritten. The below code uses the async branch of discord.py .

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM