【问题标题】:Discord Python Kick Function not working after a certain timeDiscord Python Kick 功能在一段时间后不起作用
【发布时间】:2021-10-02 11:46:18
【问题描述】:

所以我在 discord.py 中制作了一个验证码机器人,如果他们在特定时间内没有回答验证码,我会尝试踢用户,如果他们得到不正确的验证码或不输入。如果他们把它弄对了,它已经会打破循环,那部分就可以工作了。它只是时间问题,如果用户在给定时间内没有猜到验证码,它不会在特定时间后踢用户。我放了 10 秒只是为了测试。最后两行是他们的任何方式,如果他们在给定的时间内没有回答验证码,我可以在其中踢用户。顺便说一句,其他一切正常,他们没有错误**但是代码如下:

from discord.ext import commands
from discord.utils import get
import random
import time
import asyncio
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="+", intents=intents)
@bot.command()
async def setup(ctx):
  embed=discord.Embed(title="How to get verfiied", 
  description="1.Go to the verified channel and use the command $verify\n2.Then from there you should be able to get you role and come into the server.\n\n**YOU HAVE 3 tries and only 15 minutes to solve the captcha or else the bot will kick you. Plesae contact staff if you have any issues**", color=0x0000FF)
  await ctx.send(embed=embed)

@bot.command(name='verify', pass_context = True)
async def verify(ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel
    image = ("captcha 6.png","captcha10.png","captcha11.png","captcha2.png", "captcha3.png", "captcha4.png", "captcha5.png", "captcha7.png","captcha8.png", "captcha9.png")
    image_random = random.choice(image)
    number = ("b3xpn", "2yggg","ydg8n","pp546","fcne6","cdfen","4f8yp","m8m4x","pdyc8","55w5c","xwx7d")
    await ctx.send(file=discord.File(image_random))
    await ctx.send('Please solve the captcha image')
 
    for i in range(0, 3):
        guess = await bot.wait_for('message', check=check)
        if guess.content in number:
            await ctx.send('Correct Welcome!')
            role = discord.utils.get(ctx.author.guild.roles, name="member")
            await ctx.author.add_roles(role)
            break
          
        else:
            await ctx.send("Incorrect")

    else:
        await ctx.send("Incorrect this is your 3rd try please rejoin and try again")
        await asyncio.sleep(3)
        await ctx.author.kick()

    asyncio.sleep(5)
    await ctx.author.kick()





【问题讨论】:

  • 不是一个真正的大问题,但是如果用户响应/不响应,您的代码不会停止该功能,因此即使用户响应,最后两行也会始终运行并踢出用户

标签: python discord discord.py bots


【解决方案1】:

如果这会解决它,或者这只是一个帖子错误但你错过了awaitat asyncio.sleep(5)

【讨论】:

  • 这不会导致任何问题,但没有等待它不会休眠并立即转到下一行
【解决方案2】:

您必须将timeout 参数与wait_for() 一起传递,否则它将无限等待答案。

try:
    for i in range(0, 3):
        guess = await bot.wait_for('message', check=check, timeout=20)
        # it will stop listening for messages after 20 seconds
        if guess.content in number:
            await ctx.send('Correct Welcome!')
            role = discord.utils.get(ctx.author.guild.roles, name="member")
            await ctx.author.add_roles(role)
            break
          
        else:
            await ctx.send("Incorrect")

    else:
        await ctx.send("Incorrect this is your 3rd try please rejoin and try again")
        await asyncio.sleep(3)
        await ctx.author.kick()

except asyncio.TimeoutError:
    await asyncio.sleep(5)
    await ctx.author.kick()

【讨论】:

  • 也正如@HamsterRecords 所说,你必须将await 放入asyncio.sleep(5)
猜你喜欢
  • 2021-01-05
  • 2020-08-07
  • 2021-01-28
  • 2021-03-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多