首页 > 解决方案 > 试图弄清楚为什么我的机器人不会向频道发送嵌入

问题描述

我制作了一个通过 webhook 发送嵌入的脚本,它工作得很好,但我正在尝试将它转换为通过机器人发送到同一个频道。我似乎无法弄清楚(我以前从未使用过该机器人来发送嵌入。)

async def start(keyword):

SOME CODE HERE

    await embeded(channel)

async def embeded(channel):
    # Discord Embed Setup   
    embed = Embed(
        description=f"[**eBay Link**]({eBayLink})",
        color=0x0d0d22,
        timestamp='now'  # sets the timestamp to current time
        )

    embed.set_author(name=Titles)
    embed.add_field(name='Average', value=Averages, inline=True)
    embed.add_field(name='Lowest', value=Lowests, inline=True)
    embed.add_field(name='Highest', value=Highests, inline=True)
    embed.add_field(name='Margin', value=Margins, inline=True)
    embed.add_field(name='Postage', value=Postages, inline=True)

    embed.set_footer(text='TEST', icon_url='IMAGE')

    embed.set_thumbnail(image.get_attribute('src'))

    await channel.send(embed=embed)

    print("Embed sent to discord!")


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


@client.event
async def on_message(message):
    if message.content.startswith('!flip '):
        keyword = message.content.split('!flip ')[1]
    await start(keyword, channel)

client.run(token)

我得到错误

    await start(keyword, channel)
NameError: name 'channel' is not defined

标签: pythondiscord.py

解决方案


  1. 您还需要向函数添加channel参数。
  2. 你应该通过message.channel而不是channel.
  3. 语句中的缩进start函数。ifon_message

功能编辑:

async def start(keyword, channel):
    #Code
    await embeded(channel)

on_message 编辑:

@client.event
async def on_message(message):
    if message.content.startswith('!flip '):
        keyword = message.content.split('!flip ')[1]
        await start(keyword, message.channel)

推荐阅读