【问题标题】:Change discord.py embed field position更改 discord.py 嵌入字段位置
【发布时间】:2023-01-08 04:56:30
【问题描述】:

是否可以更改 discord.py 中嵌入字段的位置? 这是我更详细的问题: 我有一个嵌入,但我必须将其模块化,这意味着我们可以使用命令添加一个字段或删除一个字段。删除命令有效,添加命令有效,但现在我需要能够将字段的位置更改为与角色的位置相同(因为每个字段对应一个角色)。 我怎样才能做到这一点?

【问题讨论】:

  • 在将所有字段添加到嵌入之前,按照与您的角色相同的顺序对数据进行排序,然后添加字段。
  • 这就是我现在想做的,谢谢你的回答!我有一个角色列表,多亏了 sorted 函数,我可以对列表进行排序,但我需要对 role.position 进行排序,而不是对列表中的角色本身进行排序,我正在考虑如何去做><
  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。
  • sorted 有一个 key 参数,您可以在其中传递一个返回要排序的值的函数。在您的情况下,类似于key=lambda role: role.position
  • 我找到了解决方案,但我会在原始问题中发送我的代码

标签: discord.py


【解决方案1】:

我们无法编辑嵌入字段的位置,但我们可以尝试添加良好的每个字段,这样我们就不必编辑字段的位置!

这是我的代码(工作):

import asyncio
import discord
from discord.ext import commands
from datetime import datetime


intents = discord.Intents.all()


bot = commands.Bot(command_prefix = "!1789", help_command = None, intents=intents)

async def get_hour():
    now = ""
    now = datetime.now()
    hour = now.strftime("%H:%M")
    return hour

async def role_members_mention(role: discord.Role, emoji):
    role_str = ""
    if len(role.members):
        for member in role.members:
            role_str += f"**|** {emoji} **➜** {member.mention}
"
            role_str[:-1]
    
    return role_str

async def soon(role, emoji, max):
    soon = f"**|** {emoji} **➜ Soon**
" * (int(max[role.name].replace("/", "")) - len(role.members))
    soon[:-1]

    return soon

async def get_all_roles():
    guild = bot.get_guild(966749409314504774)
    global ceo_aceo_roles_dict
    ceo_aceo_roles_dict = {}
    ceo = discord.utils.get(guild.roles, id = 966749523949023262)
    aceo = discord.utils.get(guild.roles, id = 992137775883243530)
    global all_roles_dict
    all_roles_dict = {}
    all_roles_dict[ceo.position] = ceo
    all_roles_dict[aceo.position] = aceo
    ceo_aceo_roles_dict[ceo.position] = ceo
    ceo_aceo_roles_dict[aceo.position] = aceo

async def update_effective():
    ceo_aceo = ""
    embed = discord.Embed(title = "**E͟͟͟F͟͟͟F͟͟͟E͟͟͟C͟͟͟T͟͟͟I͟͟͟F͟͟͟ S͟͟͟T͟͟͟A͟͟͟F͟͟͟F͟͟͟**.  <:3446blurplecertifiedmoderator:1001403463214825542>", description = "", color = 0x000000)
    max = {}
    total = {}
    effectif_staff_emojis = ["<:adcrown:1001400557073866762>", "<:bluesettings:1001472768161894532>", "<:diplome:1001472522811883621>", "<:5961blurpleemployee:1001403806828994570>"]
    roles_staff_emojis = ["<:5961blurpleemployee:1001403806828994570>", "<:Dev:1001412860091580487>", "<:direction:1001476149051924591>", "<:bluevoicechannel:1001473637091659836", "<:direction:1001476149051924591>", "<:spideyn7:996331019735158864>", "<:logo_twitch:1005196891010629702>"]
    all_roles = sorted(all_roles_dict)
    ceo_aceo_roles = sorted(ceo_aceo_roles_dict)
    all_roles.reverse()
    ceo_aceo_roles.reverse()
    x = -1
    for role in all_roles:
        x += 1
        all_roles[x] = all_roles_dict[role]
    x = -1
    for role in ceo_aceo_roles:
        x += 1
        ceo_aceo_roles[x] = ceo_aceo_roles_dict[role]


    for role in all_roles:
        number = 0
        for car in role.name:
            if car == "‎":
                number += 1
        max[role.name] = "/" + str(number)
        if len(role.members) >= int(max[role.name].replace("/", "")):
            total[role.name] = f"? **{str(len(role.members))} {max[role.name]}**"
        else:
            total[role.name] = f"? **{str(len(role.members))} {max[role.name]}**"

    message = await (await get_effective_staff_channel()).fetch_message(1059250505689337998)
    for role in ceo_aceo_roles:
        ceo_aceo += f"{role.mention} {total[role.name]}
{await role_members_mention(role, effectif_staff_emojis[0])}{await soon(role, effectif_staff_emojis[0], max)}

"

    embed.add_field(name = "**C.E.O & A.C.E.O** <:adcrown:1001400557073866762>", value = ceo_aceo, inline = False)
    embed.set_thumbnail(url = "https://media.discordapp.net/attachments/1001404772496187392/1059246720669733014/Picsart_22-10-19_21-20-35-971.jpg?width=1326&height=529")
    embed.set_image(url = "https://media.discordapp.net/attachments/1001404772496187392/1059246720875241542/Picsart_22-10-13_21-14-41-363.jpg?width=618&height=618")

    await message.edit(embed = embed)

async def delete_messages(ctx, number_of_message_to_delete = 1, time_to_wait = 0):
    await asyncio.sleep(time_to_wait)
    await ctx.channel.purge(limit=number_of_message_to_delete)

@bot.event
async def on_ready():
    print(f"[{await get_hour()}] Ready !
        Username: {bot.user.name}
----------------------------------------------------")
    await get_all_roles()
    await update_effective()
    while True:
        await asyncio.sleep(600)
        await update_effective()

@bot.command()
@commands.has_permissions(administrator = True)
async def addeffectifstaff(ctx, role: discord.Role, number = 1):
    await delete_messages(ctx)
    role = discord.utils.get(ctx.guild.roles, id = role.id)
    if role in all_roles_dict.values():
        await ctx.send("Désolé mais ce rôle est déjà dans l'effectif.")
    else:
        ceo_aceo_roles_dict[role.position] = role
        all_roles_dict[role.position] = role
        await update_effective()
        await ctx.send(f"Bien ! Le rôle {role.name} a été ajouté à l'effectif !")
    await delete_messages(ctx, 1, 3)
    print(f'[{await get_hour()}] {ctx.author.name} used the command "addeffectifstaff" to make me add the role "{role.name}" to the effective message.')

@bot.command()
@commands.has_permissions(administrator = True)
async def deleffectif(ctx, role: discord.Role):
    await delete_messages(ctx)
    role = discord.utils.get(ctx.guild.roles, id = role.id)
    if not role in all_roles_dict.values():
        await ctx.send("Désolé mais ce rôle n'est déjà pas dans l'effectif.")
    else:
        ceo_aceo_roles_dict.pop(role.position)
        all_roles_dict.pop(role.position)
        await update_effective()
        await ctx.send(f"Bien ! Le rôle {role.name} a été supprimé de l'effectif !")
    await delete_messages(ctx, 1, 3)
    print(f'[{await get_hour()}] {ctx.author.name} used the command "deleffectif" to make me remove the role "{role.name}" to the effective message.')

bot.run(token)

【讨论】:

  • 这不是答案。
  • 我的问题没有答案,我想要的是不可能的,仅此而已
猜你喜欢
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多