discord.py 创建文本通道时出现AttributeError
discord.py AttributeError when creating text channel
我正在尝试为我的服务器制作一个 discord 机器人,并为 modmail 创建一个频道。但是每次我尝试创建一个时,都会出现此错误。
我正在使用 python 3.10
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\cogs\modmail.py", line 35, in on_message
channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py",
line 948, in create_text_channel
data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py",
line 844, in _create_channel
'id': target.id
AttributeError: 'str' object has no attribute 'id'
这是我的代码:
@commands.Cog.listener()
async def on_message(self, message):
if message.content.lower() in ["yes", "y", "ya", "yee", "ye", "no", "n", "nope"]:
return
if message.author == self.client.user:
return
def check(msg):
return msg.content.lower() in ["yes", "y", "ya", "yee", "ye"] and msg.author == message.author
if isinstance(message.channel, discord.channel.DMChannel):
await message.channel.send("Create ticket to modmail in Veemo's Kingdom? Yes or no.")
response = await self.client.wait_for("message", check=check)
if response:
guild = discord.utils.get(self.client.guilds, id=834666434042658816)
text_channel_name = message.author.name + "-" + message.author.discriminator
roles = guild.roles
overwrites = {
guild.default_role: discord.PermissionOverwrite(view_channel=False)
}
for role in roles:
if role.name not in mod_roles_list:
overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)
channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
else:
await message.channel.send("Ticket not created.")
我认为这里的问题是 overwrites
字典变量,
下行将 overwrites
变量附加为 keys
作为 str
而不是 discord.py Role
对象。这会导致此 AttributeError: 'str' object has no attribute 'id'
错误。
overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)
有问题的输出:
{
<Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
'@everyone': <discord.permissions.PermissionOverwrite object at ...>,
'Bot': <discord.permissions.PermissionOverwrite object at ...>,
'pbot': <discord.permissions.PermissionOverwrite object at ...>
}
它应该像下面这样,所以 key
是包含属性 id
.
的 discord.py Role
对象
overwrites[role] = discord.PermissionOverwrite(view_channel=False)
输出:
{
<Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
<Role id=<id no> name='Bot'>: <discord.permissions.PermissionOverwrite object at ...>,
<Role id=<id no> name='pbot'>: <discord.permissions.PermissionOverwrite object at ...>
}
我正在尝试为我的服务器制作一个 discord 机器人,并为 modmail 创建一个频道。但是每次我尝试创建一个时,都会出现此错误。 我正在使用 python 3.10
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\cogs\modmail.py", line 35, in on_message
channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py",
line 948, in create_text_channel
data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py",
line 844, in _create_channel
'id': target.id
AttributeError: 'str' object has no attribute 'id'
这是我的代码:
@commands.Cog.listener()
async def on_message(self, message):
if message.content.lower() in ["yes", "y", "ya", "yee", "ye", "no", "n", "nope"]:
return
if message.author == self.client.user:
return
def check(msg):
return msg.content.lower() in ["yes", "y", "ya", "yee", "ye"] and msg.author == message.author
if isinstance(message.channel, discord.channel.DMChannel):
await message.channel.send("Create ticket to modmail in Veemo's Kingdom? Yes or no.")
response = await self.client.wait_for("message", check=check)
if response:
guild = discord.utils.get(self.client.guilds, id=834666434042658816)
text_channel_name = message.author.name + "-" + message.author.discriminator
roles = guild.roles
overwrites = {
guild.default_role: discord.PermissionOverwrite(view_channel=False)
}
for role in roles:
if role.name not in mod_roles_list:
overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)
channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
else:
await message.channel.send("Ticket not created.")
我认为这里的问题是 overwrites
字典变量,
下行将 overwrites
变量附加为 keys
作为 str
而不是 discord.py Role
对象。这会导致此 AttributeError: 'str' object has no attribute 'id'
错误。
overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)
有问题的输出:
{
<Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
'@everyone': <discord.permissions.PermissionOverwrite object at ...>,
'Bot': <discord.permissions.PermissionOverwrite object at ...>,
'pbot': <discord.permissions.PermissionOverwrite object at ...>
}
它应该像下面这样,所以 key
是包含属性 id
.
discord.py Role
对象
overwrites[role] = discord.PermissionOverwrite(view_channel=False)
输出:
{
<Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
<Role id=<id no> name='Bot'>: <discord.permissions.PermissionOverwrite object at ...>,
<Role id=<id no> name='pbot'>: <discord.permissions.PermissionOverwrite object at ...>
}