Bot posting embed 7 times

Question:

I have got this application bot and made some changes to it but everytime we finsh answering the questions the posts the embed 7 times as well as sending the "thank you" meaasage 7 times in the DM’s

Im looking for the bug but can’t work it out, here is my code

 import discord
import json
from discord.ext import commands
from discord.utils import get
import inspect
from discord import colour
from discord import user
from discord.ext import commands
from discord.ext.commands import Bot
bot = Bot("?")

intents = discord.Intents.all()
client = discord.Client(intents=intents)
#------------------------
channel_id = 1144768805499834409 # sends application to when it is done
token = "MY TOKEN"
# ------------------------
prefix = '?'
questions = {
    1: "What is your In-Game Name?",
    2: "Why do you want to be a police constable in BSRP",
    3: "In your own words,  what are your strengths and weaknesses? ",
    4: "What can you bring to the Metropolitan Police Service?",
    5: "Can you tell us a time you faced a difficult challenge and how you dealt with it?",
    6: "Where do you see yourself in 3 months time?",
    7: "What is your discord name and tag?"
}
max_questions = len(questions)
is_open = True

@client.event
async def on_message(message):
    global is_open
    if not message.content.startswith(prefix) or message.author.bot:
        return

    args = message.content[len(prefix):].split()
    command = args.pop(0).lower()

    if command == 'apply':
        if not is_open:
            await message.channel.send("Sorry, Applications are currently closed.")
            return
        user = await message.author.create_dm()
        await user.send("Hey! Welcome to your police Constable application! Your application has started. You have 3 minutes to complete it. Please rember to put lots of detail into youe respones and think about your answers")
        await message.channel.send("Application has been sent to your DM's, Please check for messages from BSRP Bot")
       
        application = {'userId': message.author.id}
        for i in range(1, max_questions+1):
            embed = discord.Embed(title=f'Question [{i}/{max_questions}]', description=questions[i],)
            await user.send(embed=embed)
            response = await client.wait_for('message', check=lambda m: m.author == message.author and m.channel == user, timeout=300)
            application[f'question{i}'] = response.content

        try:
            with open('applications.json', 'r') as f:
                data = json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            data = []

        data.append(application)
        with open('applications.json', 'w') as f:
            json.dump(data, f)

        channel = client.get_channel(channel_id)
        embed = discord.Embed(title='Police Constable Application: ' + message.author.display_name)
        for i in range(1, max_questions+1):
            myval = f'{questions[i]}nn' + '_' + application[f'question{i}'] + '_'
            embed.add_field(name=f'Question {i}: ', value=myval, inline=True)
            await channel.send(embed=embed)
            await user.send('Thank you for applying! Your Application will now be reviewed! after it has been reviewed a member of the training team will contact you! Please do dont open support tickets unless it has been more then 48 hours since you applied. Best of luck!!')

    elif command == 'open' and message.author.guild_permissions.administrator:
        is_open = True
        await message.channel.send("Applications are now open.")

    elif command == 'close' and message.author.guild_permissions.administrator:
        is_open = False
        await message.channel.send("Applications are now closed.")


@client.event
async def on_ready():
    print(f"I'm Alive {bot.user}")

client.run(token)

The bot only needs to post the respone Embed and DM the user once instead of 7 times

Asked By: bsrp23

||

Answers:

max_questions = len(questions) # 7
...
for i in range(1, max_questions+1):
  ...
  await user.send('Thank you for applying!...')

You have a loop that runs 7 times, and in every loop, you send the Thank you! message. It looks like you need to pull that user.send('Thank you!') line one indent to the left, so it’s not in the loop, and won’t get executed 7 times.

Answered By: Danielle M.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.