【问题标题】:Discord.py: How to add another multi-word parameter in a command function?Discord.py:如何在命令函数中添加另一个多字参数?
【发布时间】:2021-04-22 07:38:16
【问题描述】:

我正在尝试创建一个公告功能,它将必要的嵌入发送到我指定的频道中。我已经添加了必须发送的文本,但现在我希望它也包含图像。

我无法在嵌入中发送图片链接,因为它们不起作用。

我想发送另一个包含图片链接的参数并将其单独发送到聊天中。我试过了,但它只适用于一张图片。但是当我们想再输入一个时,它就行不通了。我正在尝试了解如何做到这一点。

以下是我正在处理的命令的代码。

@nd.command()
async def announce(ctx, channel, *, message):
    townannouncerole = discord.utils.get(ctx.guild.roles, id=834684222441521193)

    townannouncementchannel = nd.get_channel(834675349409234974)

    if str(channel.lower()) == 'town':
        if townannouncerole in ctx.author.roles:
            townannouncementembed = discord.Embed(title="Town Announcement", description=message, color=discord.Colour.random())
            townannouncementembed.set_author(name=ctx.author)
            townannouncementembed.set_footer(text=datetime.datetime.now())
            await townannouncementchannel.send(embed=townannouncementembed)
        
        else:
            await ctx.channel.send(f"Hi {ctx.author.mention}, you lack necessary permissions for this command!")

这是我用来发送单个图像的代码,它运行良好。

@nd.command()
async def announce(ctx, channel, images, *, message):
    townannouncerole = discord.utils.get(ctx.guild.roles, id=834684222441521193)

    townannouncementchannel = nd.get_channel(834675349409234974)

    if str(channel.lower()) == 'town':
        if townannouncerole in ctx.author.roles:
            townannouncementembed = discord.Embed(title="Town Announcement", description=message, color=discord.Colour.random())
            townannouncementembed.set_author(name=ctx.author)
            townannouncementembed.set_footer(text=datetime.datetime.now())
            await townannouncementchannel.send(embed=townannouncementembed)
            await townannouncementchannel.send(images)
        
        else:
            await ctx.channel.send(f"Hi {ctx.author.mention}, you lack necessary permissions for this command!")

现在的问题是我无法理解 如何通过添加另一个多字参数(如 message 将包含其后的所有文本)将多个链接发送到聊天中。 不必按顺序保留参数。你可以用任何你想要的方式洗牌,解释和解决。

谢谢! :)

【问题讨论】:

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


    【解决方案1】:

    你不能在 discord bot 命令中添加另一个多字参数,但你可以做的是等待另一个响应,让我解释一下:

    你可以这样做:

    @nd.command(name = "sending_links", aliases = ["send_link", "links_send"])
    async def sending_links(ctx, channel):
        channel = channel or ctx.channel
        ctx.send("what links do you want to send? if you want to send many just seperate with \", 
        \" ex. www.image1.com, www.image2.com")
        response = await nd.wait_for("message", timeout=60)
        response = response.content.split(", ")
        for link in response:
            embed = discord.Embed(title = "title", description = "description")
            embed.set_image(url = link)
            channel.send(embed = embed)
    
    

    我无法在嵌入中发送图片链接,因为它们不起作用。

    是的,如果您这样做embed.set_image(url = image_url),您可以将图像发送到嵌入中

    【讨论】:

    • 请注意大小写正确。
    • 我的意思不是在 EMBED 的正文中。我可以使用set_image,但它只适用于一个人。
    【解决方案2】:

    discord.py 不允许使用关键字参数。所以你需要做一个参数解析器。

    示例:!announce --message '您的消息' --image 'url'

    因此,您可以根据需要从“--”和用户参数中拆分内容。

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 2021-06-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多