如何解决:AttributeError: 'None Type'对象没有属性“invite”;不和谐.py?

发布于 2025-01-12 21:43:15 字数 2070 浏览 3 评论 0原文

我正在通过 API/Integromat 生成我的 Discord 公会频道的邀请代码。

"uses": 0,
"max_uses": 2,
"max_age": 604800,
"unique": true

接下来,我想知道使用哪个邀请将其与我发送此邮件的电子邮件地址配对。对于这个问题,我使用了以下代码:

#base on medium article:
#https://medium.com/@tonite/finding-the-invite-code-a-user-used-to-join-your-discord-server-using-discord-py-5e3734b8f21f

import os
import discord
from discord.ext import commands
import requests
from keep_alive import keep_alive

intents = discord.Intents.default().all()
intents.members = True
intents.invites = True

bot = commands.Bot(command_prefix="!", intents=intents)
client = discord.Client(intents=intents)
invites = {}

@client.event
async def on_ready(): 
    for guild in client.guilds:
        invites[guild.id] = await guild.invites()

def find_invite_by_code(invite_list, code):
    for inv in invite_list:      
        if inv.code == code:
          return inv

@client.event
async def on_member_join(member):
  
    invites_before_join = invites[member.guild.id]
    invites_after_join = await member.guild.invites()

    for invite in invites_before_join:
        if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
            await requests.post(f"https://WEBHOOK_URL?invite={invite.code}")
            invites[member.guild.id] = invites_after_join
            return

@client.event
async def on_member_remove(member):
    invites[member.guild.id] = await member.guild.invites()

@client.event
async def on_message(message):
    if message.content.startswith('!Hey'):
      await client.wait_until_ready()
      await message.channel.send(message.id)
      return

keep_alive()
client.run(os.environ['BOT_TOKEN']) 

当我尝试通过私有选项卡测试邀请时,在控制台中我输出:

AttributeError: 'None Type' object has no attribute 'code'

有时它也是 < strong>AttributeError:“None Type”对象没有属性“invite”

你知道我在哪里犯了错误吗? 也许这篇文章会给你一些启发:https://github.com/GregTCLTK/Discord-Invite-追踪器

I am generating invite codes to my discord guild channel through API/Integromat.

"uses": 0,
"max_uses": 2,
"max_age": 604800,
"unique": true

Nextly I want to know which invite was used to pair it with email adress for whom I sent this. For this problem I used this code:

#base on medium article:
#https://medium.com/@tonite/finding-the-invite-code-a-user-used-to-join-your-discord-server-using-discord-py-5e3734b8f21f

import os
import discord
from discord.ext import commands
import requests
from keep_alive import keep_alive

intents = discord.Intents.default().all()
intents.members = True
intents.invites = True

bot = commands.Bot(command_prefix="!", intents=intents)
client = discord.Client(intents=intents)
invites = {}

@client.event
async def on_ready(): 
    for guild in client.guilds:
        invites[guild.id] = await guild.invites()

def find_invite_by_code(invite_list, code):
    for inv in invite_list:      
        if inv.code == code:
          return inv

@client.event
async def on_member_join(member):
  
    invites_before_join = invites[member.guild.id]
    invites_after_join = await member.guild.invites()

    for invite in invites_before_join:
        if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
            await requests.post(f"https://WEBHOOK_URL?invite={invite.code}")
            invites[member.guild.id] = invites_after_join
            return

@client.event
async def on_member_remove(member):
    invites[member.guild.id] = await member.guild.invites()

@client.event
async def on_message(message):
    if message.content.startswith('!Hey'):
      await client.wait_until_ready()
      await message.channel.send(message.id)
      return

keep_alive()
client.run(os.environ['BOT_TOKEN']) 

When I am trying to test invite through private tab, in a console I have output:

AttributeError: 'None Type' object has no attribute 'code'

sometimes it's also AttributeError: 'None Type' object has no attribute 'invite'

Do you know where I am making a mistake??
Maybe this article give you some inspire: https://github.com/GregTCLTK/Discord-Invite-Tracker

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2025-01-19 21:43:15

更新了 2 个更改:

def find_invite_by_code(invites_after_join, code):
        for invite in invites_after_join:      
            if invite.code == code:
              return invite
    
    @client.event
    async def on_member_join(member):
    
        invites_before_join = invites[member.guild.id]
        invites_after_join = await member.guild.invites()
    
        for invite in invites_before_join:
            if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
                await requests.post(f"hook?invite={invite.code}")
                invites[member.guild.id] = invites_after_join
                return

这很有趣,因为有时我的 webhook 没有发送。我将在这里更新积压工作。

Updated 2 changes:

def find_invite_by_code(invites_after_join, code):
        for invite in invites_after_join:      
            if invite.code == code:
              return invite
    
    @client.event
    async def on_member_join(member):
    
        invites_before_join = invites[member.guild.id]
        invites_after_join = await member.guild.invites()
    
        for invite in invites_before_join:
            if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
                await requests.post(f"hook?invite={invite.code}")
                invites[member.guild.id] = invites_after_join
                return

It's interesting because sometimes my webhook was send on the other hand not. I will update backlog here.

漆黑的白昼 2025-01-19 21:43:15

解决了。
创建的邀请仅在重新启动机器人后才会显示。
所以我需要创建 100 多个邀请,然后进行操作。

Solved.
Created invites will show only after restart bot.
So you I needed to create 100+ invites, and then manipulate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文