【发布时间】: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