You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在discord.py触发命令后发送Embed更新任务状态

Discord.py 发送Embed消息问题解决

我之前使用discord.js,现在转学习discord.py,开发了一个用于引导访问者进入Ebay列表的Discord机器人,目前机器人能正常运行但只能发送标准消息,想改为发送Embed消息,但尝试替换代码后未能成功。当前代码如下:

import requests
from discord.ext.commands import Bot
from discord.ext import commands

token = ""

intents = discord.Intents.all()
intents.members = True
activity = discord.Activity(type=discord.ActivityType.listening, name="-views [number of views] [link]")
client = commands.Bot(command_prefix = "-", intents = intents, activity=activity, status=discord.Status.idle)

@client.event
async def on_ready():
    print('Connected to bot: {}'.format(client.user.name))
    print('Bot ID: {}'.format(client.user.id))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if '-views' in message.content:
        number_of_views = message.content.split(' ')[1]
        link = message.content.split(' ')[2]

    response=f'Sending {number_of_views} viewers to {link}!'
    await message.channel.send(response)


    #response = discord.Embed(description="Sent {number_of_views} viewers!\nProduct Link\n{link}", color=0x2B2F51)
    #response.set_footer(text = "Flex Offenders", icon_url = "https://i.imgur.com/KbKB5gk.png")
    #message.channel.send(embed=response)

    for i in range(int(number_of_views)):
            requests.get(link)

    response= f'{number_of_views} viewers have been sent to {link}!'
    await message.channel.send(response)

client.run(token)

#async def on_message(message):
    #embedSend = discord.Embed(description='Sending {number_of_views} viewers!\nProduct Link\n{link}', color=0x2B2F51)
    #embedSend.set_footer(text = "Flex Offenders", icon_url = "https://i.imgur.com/KbKB5gk.png")
    #message.channel.send(embed=embedSend)

问题分析与修正方案

  1. 缺少discord模块导入
    代码中使用了discord.Embed、discord.Intents等类,但未导入discord模块,需在开头添加:

    import discord
    
  2. on_message函数内缩进错误
    判断'-views' in message.content的代码块结束后,后续的变量定义和消息发送逻辑未缩进,会导致number_of_views和link变量未定义的报错,需将这些代码缩进至if块内。

  3. 发送Embed时缺少await关键字
    message.channel.send()是异步方法,必须添加await才能正确执行。

  4. Embed描述字符串未格式化
    原注释中的Embed描述使用了占位符但未做格式化,需用.format()或f-string填充变量值。

  5. 同步requests调用阻塞机器人(可选优化)
    循环中使用requests.get()是同步操作,会导致机器人在执行期间无法响应其他事件,建议改用aiohttp进行异步请求。

修正后的完整代码

import discord
import requests
from discord.ext.commands import Bot
from discord.ext import commands

token = ""

intents = discord.Intents.all()
intents.members = True
activity = discord.Activity(type=discord.ActivityType.listening, name="-views [number of views] [link]")
client = commands.Bot(command_prefix="-", intents=intents, activity=activity, status=discord.Status.idle)

@client.event
async def on_ready():
    print('Connected to bot: {}'.format(client.user.name))
    print('Bot ID: {}'.format(client.user.id))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if '-views' in message.content:
        # 拆分命令参数,增加格式校验
        parts = message.content.split(' ')
        if len(parts) < 3:
            await message.channel.send("格式错误:请使用 `-views [浏览量] [链接]`")
            return
        number_of_views = parts[1]
        link = parts[2]

        # 发送开始处理的Embed消息
        start_embed = discord.Embed(
            description=f"正在发送 {number_of_views} 个浏览量到链接!\n商品链接:{link}",
            color=0x2B2F51
        )
        start_embed.set_footer(text="Flex Offenders", icon_url="https://i.imgur.com/KbKB5gk.png")
        await message.channel.send(embed=start_embed)

        # 模拟发送浏览量
        for i in range(int(number_of_views)):
            requests.get(link)

        # 发送完成通知的Embed消息
        finish_embed = discord.Embed(
            description=f"{number_of_views} 个浏览量已成功发送到链接!\n商品链接:{link}",
            color=0x2B2F51
        )
        finish_embed.set_footer(text="Flex Offenders", icon_url="https://i.imgur.com/KbKB5gk.png")
        await message.channel.send(embed=finish_embed)

client.run(token)

内容的提问来源于stack exchange,提问作者JediSZN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:25:34