【问题标题】:Discord.py Say Command, Message & EmbedDiscord.py 说命令、消息和嵌入
【发布时间】:2020-09-08 03:00:09
【问题描述】:

我有 say 命令可以正常工作,并回复我在其中提供的消息。但是,我想更改代码以支持 自定义嵌入正文消息

基本上,我的问题是:如何让我的say 命令回复消息或自定义嵌入正文消息

我的say 命令

@commands.command(name='say', aliases=['repeat', 'talk'])
    @commands.has_permissions(manage_messages=True)
    async def _say(self, ctx, *, message):
        await ctx.send(message, ctx)

我希望我的命令能够做的是......

示例:

-say {embed=discord.Embed(title="Test", description="Embed Example")
embed.set_author(name="Author", url="https://authorlink.com", icon_url="https://authoricon.com")
embed.set_thumbnail(url="https://thumbnail.png")
embed.add_field(name="Field 1", value="Field test 1", inline=True)
embed.add_field(name="Field 2", value="Field test 2", inline=False)
embed.set_footer(text=ctx.guild.name)
await ctx.send(embed=embed)}

expected bot response

有谁知道如何使代码也发送自定义嵌入正文消息?我了解嵌入以及如何形成它。现在,我只希望我的命令能够同时说出自定义嵌入和自定义消息。我希望我的想法是有意义的。

  • 编辑: 这就是我得到的嵌入(作为单独的命令)

(这样我不能自定义整个嵌入,而只能自定义代码中提供的字段的值。如果可能的话,我正在寻找一种使其完全自定义的方法,并将其与一个命令合并仅用于嵌入和普通消息)_

@commands.command(name="sayembed")
    @commands.has_permissions(manage_messages=True)
    async def _saye(self, ctx, *, message):
        embedsay = discord.Embed(color=ctx.author.color, timestamp=ctx.message.created_at)
        embedsay.set_author(name="Message from:", icon_url=ctx.author.avatar_url)
        embedsay.add_field(name=ctx.message.author, value=str(message))
        embedsay.set_thumbnail(url=ctx.author.avatar_url)
        embedsay.set_footer(text=ctx.guild.name)
        await ctx.send(embed=embedsay)

【问题讨论】:

  • 您能否提供一些示例,说明如何调用命令来发送嵌入?如果您正在编写一个刚刚发送嵌入的命令,那会是什么样子?
  • 就像我说的,它是同一个命令,我只是在寻找一种-say 命令可以发送两种消息的方式。如果不可能,我想我会对单独的嵌入命令感到满意,可能是-sayembed或其他东西。
  • 现在我确实有一个发送嵌入的示例命令,但问题是,它只能发送自定义字段值,而不是整个自定义嵌入消息(如自定义页脚、字段、嵌入信息等.)。我将在我的帖子中添加一个嵌入消息的示例。

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


【解决方案1】:

因为你的say命令很好,但是如果你想添加嵌入,你需要一个embed = discord.Embed() 示例:

import datetime
import discord
from discord import Embed
from discord.ext import commands

client = commands.Bot(command_prefix='PREFIX')

@client.command()
async def embed(ctx):
    embed = discord.Embed(
        title='This is a title! you can customise this!',
        description='This is a description!',
        colour=discord.Color.green(),
        timestamp=datetime.datetime.now()

        ).add_field(
        name='This is non inlined field 1!',
        value='This is a value!',
        inline=False

        ).add_field(
        name='This is a non inlined field 2!',
        value='This is a value!',
        inline=False

        ).add_field(
        name='This is a inlined field 1!',
        value='This is a value!',
        inline=True

        ).add_field(
        name='This is a inlined field 2!',
        value='This is a value!',
        inline=True

        ).set_footer(
        text='This is a footer!',
        icon_url='https://cdn.discordapp.com/avatars/694446165197979670/eb946325b85f913abdec645d0c14b98a.webp?size=1024'

        ).set_image(
        url='https://cdn.discordapp.com/avatars/694446165197979670/eb946325b85f913abdec645d0c14b98a.webp?size=1024'

        ).set_thumbnail(
        url='https://cdn.discordapp.com/avatars/694446165197979670/eb946325b85f913abdec645d0c14b98a.webp?size=1024'
        )

    await ctx.send(embed=embed)

您可以完全自定义嵌入,但如果您不包含标题或 add_field 上的值,则会引发错误(当然您可以将它们串起来)。

Embed文档

如果你想给一个特定的消息发送消息,你可以这样做

@client.command()
async def say(ctx, member: discord.Member, *, message):
    embed = discord.Embed(
        title=message,
        description=f"From: {ctx.author}",
        colour=discord.Color.green()
        )

    await member.send(embed=embed)

【讨论】:

    【解决方案2】:

    我知道这已经解决了,但是除非每个参数的消息都发生变化(例如 kick 命令),否则您不必将其包含在命令本身中,您可以使用:

    <varname> = discord.Embed(title=„title”, description=„description”, color=(use hex codes)
    <varname>.add_field(basically the field, im on an iPhone rn and dont want to type it all out)
    <varname>.set_footer, .set_thumbnail, etc.
    
    
    async def say(ctx):
    [TAB] ctx.send(embed=<varname>)
    

    【讨论】:

    • 欢迎来到 Stack Overflow!如您所知,您无需手动指定[TAB] 来显示缩进,预格式化的代码块会为您执行此操作。您可以安全地将 edit 替换为 PEP 8-standard 4 个空格。此外,您应该考虑使用 registering an account 来访问 Stack Overflow 的更多功能。
    【解决方案3】:

    您可以使用此代码,记住将标题和描述用/ 分开,否则命令将不起作用示例命令:!embed title/description

    import discord
    from discord.ext import commands
    import datetime
    
    bot = commands.bot(command_prefix=commands.when_mentioned_or("!"))
    
    @bot.command()
    async def embed(ctx, *, content: str):
       title, description= content.split('/')
       embed = discord.Embed(
       title=title,
       description=description,
       colour=discord.Color.green(),
       timestamp=datetime.datetime.now()
    
            
       )
    
       await ctx.send(embed=embed)
    
    bot.run("your token here")
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2021-02-28
      • 2021-06-04
      • 2019-01-12
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      相关资源
      最近更新 更多