CaptchaHandler or Bot.handle_captcha() ? #652
-
Hi, i am completely lost between versions of discord.py-self, and with the CORRECT WAY to solve captchas. In the v2.0.0, it's possible to implement a discord.CaptchaHandler when defining my discord.Bot or discord.Client, and there is no need to use bot.handle_captcha() when a CaptchaRequired is raised (because bot.handle_captcha() is not defined in the v2.0.0) class CaptchaSolver(discord.CaptchaHandler):
def two_captcha(self, data: dict, proxy: str, proxy_auth: aiohttp.BasicAuth) -> str:
api_key = 'my_api_key'
solver = twocaptcha.TwoCaptcha(apiKey=api_key, defaultTimeout=180)
result = solver.hcaptcha(
sitekey=data['captcha_sitekey'],
data=data['captcha_rqdata'],
url='https://discord.com/channels/@me',
)
return result['code'] #perhaps return result instead of result["code"]
async def fetch_token(self, data: dict, proxy: str, proxy_auth: aiohttp.BasicAuth) -> str:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, self.two_captcha, data, proxy, proxy_auth) But this is not working. So I looked for Bot.handle_captcha(), which is only available with cloning the lastest version of the github repository (looks like it's V2.1.0) BUT in this version, discord.CaptchaHandler class has been removed, so I absolutely don't understand what do I have to put in the captcha_handler parameter ! This is a part of my script : elif msg.content.startswith(f"{bot.user.mention} dm ") :
nothing, dm, user, *args = msg.content.split(" ")
user = await bot.fetch_user(int(user))
try :
dmchannel = await user.create_dm()
await dmchannel.send(' '.join(args))
except discord.CaptchaRequired as err :
await bot.handle_captcha(err) NOTE : depending on the version of discord.py-self I use, i remove Thank you so much 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Captcha handling was rewritten in v2.1 because it was unnecessarily complicated. The Optional[Callable[[CaptchaRequired, Client], Awaitable[str]])] syntax essentially means it accepts a method that looks like this: async def captcha(exc: discord.CaptchaRequired, bot: commands.Bot) -> str:
# do stuff here, e.g. run your existing two_captcha() function in an executor
log.debug(f'Got a CAPTCHA with sitekey {exc.sitekey}')
bot = commands.Bot(captcha_handler=captcha) This is handled by the Bot.handle_captcha() method you mentioned, which can alternatively be overrided if you are already subclassing Client/Bot: class MyBot(commands.Bot):
async def handle_captcha(self, exc: discord.CaptchaRequired) -> str:
# do stuff here, e.g. run your existing two_captcha() function in an executor
log.debug(f'Got a CAPTCHA with sitekey {exc.sitekey}')
bot = MyBot() # do NOT use the captcha_handler parameter if you override the above method Side note: I don't recommend services like TwoCaptcha. |
Beta Was this translation helpful? Give feedback.
-
So, It should be used like: async def captcha(exc: discord.CaptchaRequired, bot: commands.Bot) -> str:
def two_captcha(self, data: dict, proxy: str, proxy_auth: aiohttp.BasicAuth) -> str:
api_key = 'key'
solver = twocaptcha.TwoCaptcha(apiKey=api_key, defaultTimeout=180)
result = solver.hcaptcha(
sitekey=data['captcha_sitekey'],
data=data['captcha_rqdata'],
url='https://discord.com/channels/@me',
)
return result['code'] (It will be really helpful if you can answer quickly!) |
Beta Was this translation helpful? Give feedback.
Captcha handling was rewritten in v2.1 because it was unnecessarily complicated.
The Optional[Callable[[CaptchaRequired, Client], Awaitable[str]])] syntax essentially means it accepts a method that looks like this:
This is handled by the Bot.handle_captcha() method you mentioned, which can alternatively be overrided if you are already subclassing Client/Bot: