【问题标题】:How to make rank card on DiscordPy如何在 DiscordPy 上制作排名卡
【发布时间】:2020-11-24 18:48:33
【问题描述】:

我正在为我的个人服务器编写一个不和谐机器人。我用 JSON 做了一个关卡系统。但是我想在你写排名命令的时候发一张排名卡。 Like This

如何用进度条做到这一点?我需要基础设计吗?我以前搜索过它,但没有用枕头写的信息图像,结果一无所获......

我的关卡系统代码在这里。谢谢。

import discord
from discord.ext import commands
import json
import os

token: str = 'token'

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix='.', intents=intents)

level_channel_name = 'bot-komut'



@client.event
async def on_ready():
print('Bot is ready')

@client.event
async def on_member_join(member):
    with open('users.json', 'r') as f:
        users = json.load(f)

    if not member.id in users:
        users[member.id] = {}
        users[member.id]['XP'] = 0
        users[member.id]['level'] = 1

    with open('users.json', 'w') as f:
        json.dump(users, f)

@client.event
async def on_message(message):
    if message.author.bot:
        return
    if message.content.startswith('.'):
        pass
    else:
        with open('users.json', 'r') as f:
            users = json.load(f)

        xp = genXP(message)
        print(xp)

        channel = discord.utils.get(client.channels, name=level_channel_name)

        await update_data(users, message.author)
        await add_xp(users, message.author, xp)
        await levelUp(users, message.author, channel)

        with open('users.json', 'w') as f:
            json.dump(users, f)


    await client.process_commands(message)


@client.command()
async def rank(ctx):
    with open('users.json', 'r') as f:
        users = json.load(f)

    level = users[str(ctx.message.author.id)]['level']
    await ctx.send(f"""{level} Levelsin, {ctx.message.author.mention}""")

    with open('users.json', 'w') as f:
        json.dump(users, f)

@client.event
async def update_data(users, user):
    if str(user.id) in users:
        pass
    else:
        print("Yeni kayıt")
        users[str(user.id)] = {}
        users[str(user.id)]['XP'] = 0
        users[str(user.id)]['level'] = 1

async def add_xp(users, user, xp):
    users[str(user.id)]['XP'] += xp

async def levelUp(users, user, channel):
    lastxp = users[str(user.id)]['XP']
    startLevel = users[str(user.id)]['level']
    levelEnd = int(lastxp ** (1/6))

    if levelEnd > startLevel:
        await channel.send(f"""{user.mention}, {levelEnd} seviyeye ulaştı""")
        users[str(user.id)]['level'] = levelEnd


async def genXP(mesaj):
    msg = mesaj.content.split(" ")
    genedXP = len(msg)
    return genedXP

client.run(token)

【问题讨论】:

  • 是的,你会使用 PIL/Pillow 来做这样的事情。您将有一个基本图像来粘贴用户的头像并添加文本。
  • @Cloud 那么如何在枕头上为 xp 制作进度条?

标签: python discord discord.py


【解决方案1】:

您可以使用一张图片作为背景一张图片作为进度条,然后根据您的百分比更改进度条图片的宽度:

from PIL import Image

# load background image
background = Image.open("background.jpg")

# load rectangle(progress bar)
rectangle = Image.open("rect.png")

# percentage of progress bar
percent = 70

# change width of progress bar based on percentage
rectangle = rectangle.resize((round(rectangle.size[0] * percent / 100), rectangle.size[1]))

# draw progress bar from x,y 50 of background image
background.paste(rectangle, (50, 50), rectangle)

# save image
background.save("result.png")

【讨论】:

  • 我按照你说的做了。它几乎工作了,但我有一个问题。我用 png 保存了我的矩形图像,但结果它看起来像一个 jpg 文件。角落不透明。我该怎么做才能解决这个问题?
  • 现在检查代码,将第三个参数添加到粘贴函数,以便指示将用于粘贴图像的掩码。如果您传递具有透明度的图像,则 Alpha 通道将用作遮罩。
  • 再次感谢。我用粘贴功能上的'mask = rectangle'解决了它,就像这样。再次感谢您。
  • 我想补充一点,如果您的进度条图像不是那么复杂,您可以使用ImageDraw.draw.rectangle 绘制它。
  • 您能否提供一些示例图片,因为我无法获得所需的图片,它说bad transparency mask 请编辑您的答案
猜你喜欢
  • 2021-05-03
  • 2020-12-14
  • 2013-05-19
  • 2022-07-04
  • 2011-05-15
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多