代码之家  ›  专栏  ›  技术社区  ›  bsrp23

Bot发布嵌入7次

  •  1
  • bsrp23  · 技术社区  · 2 年前

    我有这个应用程序机器人,并对它进行了一些更改,但每次我们回答完问题后,都会在DM中发布7次嵌入内容,并发送7次“谢谢”信息

    我正在查找错误,但无法解决,这是我的代码

     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]}\n\n' + '_' + 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)
    
    

    机器人只需要发布响应嵌入并DM用户一次,而不是7次

    1 回复  |  直到 2 年前
        1
  •  0
  •   Danielle M.    2 年前
    max_questions = len(questions) # 7
    ...
    for i in range(1, max_questions+1):
      ...
      await user.send('Thank you for applying!...')
    

    您有一个运行7次的循环,在每个循环中,您发送 Thank you! 消息看起来你需要拉一下 user.send('Thank you!') 第一行向左缩进,所以它不在循环中,也不会被执行7次。