【问题标题】:Discord.py rewrite and sending images - AttributeError: 'list' object has no attribute 'send'Discord.py 重写和发送图像 - AttributeError: 'list' object has no attribute 'send'
【发布时间】:2020-07-28 16:47:15
【问题描述】:

我是一名中级 python 用户和经验丰富的 Selenium 用户,我希望将这些技能与我对新 discord.py 重写的小知识结合起来。我创建的代码无头运行 selenium - 它导航到一个网站,填写表格,然后截取屏幕截图。我遇到的问题是 discord.py 的 channel.send 文件功能。这是我的代码-

async def on_message(message):
    id = client.get_guild(guildcode)
    valid_users = ["discord#tag"]
    if str(message.author) in valid_users:
        if message.content.find("!gen") != -1:
            await message.channel.send("Generating coupon now...")
            driver.get('example.com')
            elem = driver.find_element_by_name("CN1")
            elem.click()
            elem.clear()
            surveyCode = '36901999912012386'
            elem.send_keys(surveyCode)
            clicksDone = 0
            while int(clicksDone) < 25:
                elem = driver.find_element_by_id('NextButton')
                elem.click()
                clicksDone = clicksDone + int('1')
            if clicksDone == 25:
                driver.get_screenshot_as_file('capture.png')
                await message.channel.send('Generated!')
                await channel.send(file=discord.File('capture.png'))

机器人正确填写调查表,输出它已生成优惠券,但未能发送新截取的屏幕截图 - capture.png。这是我遇到的错误-

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\hp\PycharmProjects\telldunkin\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/hp/PycharmProjects/telldunkin/discorddonut.py", line 37, in on_message
    await channel.send(file=discord.File('capture.png'))
NameError: name 'channel' is not defined

当我摆脱发送文件代码时,不会出现错误。提前感谢您的帮助。

【问题讨论】:

  • 错误信息抱怨某行代码不在您显示的代码中。
  • 提供了我认为源于 channel.send 的主要错误。第一个错误不在现有代码行上
  • 现在这个问题已经解决了,看起来你只是有一个错字。它应该是message.channel.send 而不是channel.send,就像上一行一样。
  • 非常感谢!这似乎解决了问题。

标签: python selenium-webdriver pycharm discord discord.py


【解决方案1】:

在您提供的代码 sn-p 中,我没有看到 channel 定义,我猜它是之前定义的列表。你的问题是你在做channel.send,而不是message.channel.send

编辑:我读错了,python 说channel.send(message, image) 行有错误,但此行不在您的代码 sn-p 中。

这里python说channel没有定义,这是真的,你的代码中没有channel = ...send 方法必须在有效的discord.Channel 对象上使用,在这里您可以使用message.channel 获取消息通道。要发送文件,您必须使用message.channel.send,作为常规消息。

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2022-12-03
    • 2021-06-29
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多