【问题标题】:How do I check if an invite is invalid?如何检查邀请是否无效?
【发布时间】:2020-06-13 00:03:06
【问题描述】:

我在 stackoverflow 上看到了这个问题:Testing if discord invite link is invalid?,但它已经有一年的历史了,Python 3 中不存在一些模块。

我试过了(这里的邀请无效):我期待的是404 not found error

import urllib.request
content = urllib.request.urlopen('https://discord.gg/UWxaV8m').read()
print(content)

但是,这给了我一个错误:urllib.error.HTTPError: HTTP Error 403: Forbidden;还有其他方法吗?另外,查看错误:看起来我不允许访问该邀请,因为我是机器人用户?身份证

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    如果您希望用户通过命令参数输入邀请,如果邀请无效,它将引发异常(准确地说是discord.NotFound 错误):

    @bot.command()
    async def inv(ctx, invite: discord.Invite):
        await ctx.send("That invite is valid!")
    @inv.error
    async def inv_error(ctx, error):
        if isinstance(error, commands.BadArgument):
            if isinstance(error.original, discord.NotFound):
                await ctx.send("That invite is invalid or has expired.")
    

    或者你可以使用fetch_invite():

    try:
        invite = await bot.fetch_invite(URL_OR_ID_HERE)
        await ctx.send("That's a valid invite!")
    except:
        await ctx.send("Sorry, that invite was invalid or has expired.")
    

    不需要任何其他模块 - 只有不和谐库能够告诉您有关邀请的信息。


    参考资料:

    【讨论】:

      【解决方案2】:

      使用不和谐请求页面中的 url 并在标头中添加用户代理,我能够得到响应:

      import urllib.request
      url = 'https://discord.com/api/v6/invites/UWxaV8m?with_counts=true'
      user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
      headers={'User-Agent':user_agent,}
      request=urllib.request.Request(url,None,headers) #The assembled request
      response = urllib.request.urlopen(request)
      data = response.read() # The data u need
      print(data)
      

      输出:

      b'{"code": "UWxaV8m", "guild": {"id": "599754844676554783", "name": "Thicc Mario Hub | 10 Boosts", "splash": "1d76b7a1ecceffa42552abc88ea5d6ad", "banner": "7036d8cd5e36e91fe5c87478c5eae37a", "description": null, "icon": "a_3355e47f1d336ad04737afc2ba08fe62", "features": ["ANIMATED_ICON", "INVITE_SPLASH"], "verification_level": 2, "vanity_url_code": null}, "channel": {"id": "599761618720653313", "name": "\\u2554\\u27a4\\u300e\\ud83d\\udcdc\\u300frules", "type": 0}, "inviter": {"id": "429840967470678037", "username": "Thicc Mario", "avatar": "dd33989a2b86a43f53280174addd3a21", "discriminator": "8611", "public_flags": 256}, "approximate_member_count": 1662, "approximate_presence_count": 373}'
      

      【讨论】:

        【解决方案3】:

        如果您不打算以编程方式执行此操作,只需使用 curl。

        curl "https://discordapp.com/api/invite/invite-code-goes-here"

        样本输出模型:

        {"code": "invite-code-goes-here", "guild": {"id": "123456789012345678", "name": "Discord Server Name", "splash": null, "banner": null, "description": null, "icon": "0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q", "features": [], "verification_level": 0, "vanity_url_code": null, "nsfw": false, "nsfw_level": 0}, "channel": {"id": "123456789012345678", "name": "general", "type": 0}, "inviter": {"id": "876543210987654321", "username": "person-who-created-invite", "avatar": "0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p", "discriminator": "6295", "public_flags": 0}}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-12
          • 2016-01-16
          • 2010-09-12
          • 1970-01-01
          • 2021-08-16
          • 2018-08-30
          • 1970-01-01
          相关资源
          最近更新 更多