【问题标题】:discord.py TypeError: 'property' object is not iterablediscord.py TypeError:“属性”对象不可迭代
【发布时间】:2021-05-19 13:07:15
【问题描述】:

我正在尝试编写一个 discord 机器人,它会在发送的每条消息中检查单词“pawel”,如果找到,它应该向名为 #pawel 的 discord 文本频道发送一条消息。如果没有,它应该创建它。现在我被这个愚蠢的错误困住了:

We're live!
Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 22, in on_message
    channel = discord.utils.get(guild.text_channels, name='pawel')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 269, in get
    for elem in iterable:
TypeError: 'property' object is not iterable

机器人的完整代码在这里:

import discord
import os

client = discord.Client()
guild = discord.Guild

@client.event
async def on_ready():
    print("We're live!")

pawel = ('pawel')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if pawel in message.content:
        channel = discord.utils.get(guild.text_channels, name='pawel')
        channel.id = channel.id
        if channel == None:
            await guild.create.text_channel('pawel')
        await message.channel.send('pawel')

client.run(os.getenv('TOKEN'))

我什么都试过了!

【问题讨论】:

    标签: python discord discord.py typeerror


    【解决方案1】:
    pawel = 'pawel'
    @client.event
    async def on_message(message):
            if message.author == client.user:
                return
            if pawel in message.content:
                channel = discord.utils.get(message.guild.text_channels, name = 'pawel')
                if channel is None:
                    await guild.create_text_channel('pawel')
                await channel.send('pawel')
    

    请去掉channel.id = channel.id(如果channel是none那么channel.id会抛出错误'None type object does not have attribute id',坦白说这里没有用)

    guild.text_channel 更改为message.guild_text_channels。 也是guild.create_text_channel 不是guild.create.text_channel

    我相信您想将消息发送到名为 pawel 的频道。在这种情况下,message.channel.send 是不合适的,因为它会向触发事件的通道发送消息。但是,如果这不是故意的,那么您可以离开它。 (如果按照我描述的方式进行,请使用channel.send 而不是将其发送到名为“pawel”的频道

    上面的代码是我这边调试几分钟后得到的。 如果您对message.channel.sendchannel.send 问题感到困惑,我会将修改后的原始代码留在此行下方。

    您也可以将is 用于None 类型的对象,而不是==

    pawel = 'pawel'
    @client.event
    async def on_message(message):
            if message.author == client.user:
                return
            if pawel in message.content:
                channel = discord.utils.get(message.guild.text_channels, name = 'pawel')
                if channel is None:
                    await guild.create_text_channel('pawel')
                await message.channel.send('pawel')
    

    【讨论】:

      【解决方案2】:

      我认为这里的问题是您实际上并没有获得一个单独的公会,而只是产生了一个通用的公会对象guild = discord.Guild 您可以通过以下方式获取公会的文本频道 message.guild.text_channels

      当适应您的代码时,它将导致:

      channel = discord.utils.get(message.guild.text_channels, name="pawel")
      

      【讨论】:

        猜你喜欢
        • 2021-08-23
        • 2021-04-14
        • 2013-09-01
        • 2017-08-27
        • 2018-10-10
        • 2021-12-13
        • 2019-02-20
        • 2020-03-27
        • 2018-12-12
        相关资源
        最近更新 更多