Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何制作不和谐的等级卡_Python_Discord_Discord.py - Fatal编程技术网

Python 如何制作不和谐的等级卡

Python 如何制作不和谐的等级卡,python,discord,discord.py,Python,Discord,Discord.py,我正在为我的个人服务器编写一个discord bot。我用JSON做了一个级别系统。但是我想在你写军衔命令时发一张军衔卡 如何使用进度条实现这一点?我需要基础设计吗?我已经搜索过了,但没有结果没有一个信息图像写的枕头 我的等级系统代码在这里。多谢各位 import discord from discord.ext import commands import json import os token: str = 'token' intents = discord.Intents.defau

我正在为我的个人服务器编写一个discord bot。我用JSON做了一个级别系统。但是我想在你写军衔命令时发一张军衔卡

如何使用进度条实现这一点?我需要基础设计吗?我已经搜索过了,但没有结果没有一个信息图像写的枕头

我的等级系统代码在这里。多谢各位

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)

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

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")

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

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")

是的,你会用PIL/枕头做这样的东西。您将有一个基本图像来粘贴用户的头像,并向云添加文本。那么我如何在枕头上为xp创建进度条呢?是的,您将使用PIL/pillow进行类似的操作。你会有一个基本图像来粘贴用户的头像,并向云添加文本。那么我如何在枕头上为xp创建进度条呢?我按照你说的做了。这几乎奏效了,但我有个问题。我用png保存了我的矩形图像,但结果它看起来像一个jpg文件。角是不透明的。我能做些什么来解决这个问题呢?现在检查代码,在粘贴函数中添加第三个参数,以指示将用于粘贴图像的掩码。如果你传递一个透明的图像,那么alpha通道将用作遮罩。再次感谢。我用粘贴函数中的“mask=rectangle”解决了这个问题,如下所示。再次感谢你。我想补充一点,如果你的进度条图像没有那么复杂,你可以用它来画。我照你说的做了。这几乎奏效了,但我有个问题。我用png保存了我的矩形图像,但结果它看起来像一个jpg文件。角是不透明的。我能做些什么来解决这个问题呢?现在检查代码,在粘贴函数中添加第三个参数,以指示将用于粘贴图像的掩码。如果你传递一个透明的图像,那么alpha通道将用作遮罩。再次感谢。我用粘贴函数中的“mask=rectangle”解决了这个问题,如下所示。再次感谢你。我想补充一点,如果你的进度条图像没有那么复杂,你可以用它来画。