AttributeError:“列表”对象没有属性“通道” discord.py

时间:2019-04-01 11:19:24

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

我正在尝试创建一个函数,该函数将使用https://www.youtube.com/watch?v=ZBVaH6nToyM清除不和谐的聊天记录,但是我认为我已经声明了频道(channel = ctx.message.channel),但是输入.clear会导致错误

Ignoring exception in command clear
Traceback (most recent call last):
File "C:\Users\mark\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\ext\commands\core.py", line 50, in 
wrapped
ret = yield from coro(*args, **kwargs)
File "C:/Users/mark/Desktop/purge.py", line 17, in clear
await client.delete_message(messages)
File "C:\Users\mark\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\discord\client.py", line 1261, in delete_message
channel = message.channel
AttributeError: 'list' object has no attribute 'channel
import discord
from discord.ext import commands
from discord.ext.commands import Bot
TOKEN = "token here"
client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
print("Bot is online")

@client.command(pass_context = True)
async def clear(ctx, amount=100):
     channel = ctx.message.channel
     messages = []
     async for message in client.logs_from(channel,limit=int(amount)):
         messages.append(messages)
     await client.delete_message(messages)
     await client.say('message has been deleted')

  client.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

您基本上是一遍又一遍地添加列表

my_list=[]
my_list.append(my_list)
print(my_list)

会导致类似这样的结果

>>[[[...]]

命令应该是这样的

@bot.command(pass_context=True)
async def clear(msg,amt:int=20): #can be converted into int via :type
    messages=[]
    async for i in bot.logs_from(msg.message.channel,limit=amt):
        messages.append(i)
    await bot.delete_messages(messages)

另外请注意,您不能批量删除超过14天的邮件,这意味着您不能使用delete_messages只能将其单独删除