Python discord.py类型错误:';财产';对象是不可编辑的
我正在尝试编写一个discord bot,它会检查发送的每条消息中是否有“pawel”一词,如果找到了,它会将消息发送到一个名为#pawel的discord文本频道。如果没有,它应该创建它。现在我被这个愚蠢的错误困扰着:Python discord.py类型错误:';财产';对象是不可编辑的,python,discord,discord.py,typeerror,Python,Discord,Discord.py,Typeerror,我正在尝试编写一个discord bot,它会检查发送的每条消息中是否有“pawel”一词,如果找到了,它会将消息发送到一个名为#pawel的discord文本频道。如果没有,它应该创建它。现在我被这个愚蠢的错误困扰着: We're live! Ignoring exception in on_message Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-pa
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
下面是bot的完整代码:
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'))
我什么都试过了 我认为这里的问题是,你实际上没有得到一个单独的公会,而只是产生了一个普通的公会对象
guild=discord.guild
您可以通过执行以下操作获得公会的文本频道
message.guild.text\u频道
当适应您的代码时,将导致:
channel = discord.utils.get(message.guild.text_channels, name="pawel")
请删除channel.id=channel.id
(如果通道为none,那么channel.id将抛出错误“none-type对象没有属性id”,坦率地说在这里没有用处)
将guild.text\u频道
更改为message.guild\u text\u频道
。
它也是guild.create\u text\u频道
而不是guild.create.text\u频道
我相信你想把这个消息发送到一个叫pawel的频道。在这种情况下,message.channel.send
不合适,因为它会将消息发送到触发事件的频道。然而,如果这不是有意的,那么你可以离开它。(如果按照我描述的方式发送,请使用channel.send
将其发送到名为“pawel”的频道
上面的代码是经过我几分钟的调试后得到的。
如果您对message.channel.send
和channel.send
问题感到困惑,我会将修改后的原始代码保留在此行下方
您还可以对非类型对象使用is
,而不是=
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')
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')