このサイトからcodeを取っていく
人のプログラムを改変していく
その前にlevelというフォルダ作っていてください 上の画像をlevelフォルダにrank.pngという名前でインストール
ここからフォントをlevelフォルダにラノベPOP.ttfという名前でインストールしてください
level.dbは自動で作られます
from discord.ext import commands
import discord
import os
import sqlite3
conn=sqlite3.connect("level.db", check_same_thread=False)
c=conn.cursor()
prefix="?"
token="トークン"
bot=commands.Bot(command_prefix=prefix)
c.execute("CREATE TABLE IF NOT EXISTS level(userid, level, exp)")
@bot.event
async def on_ready():
print("起動")
@bot.listen("on_message")
async def level_count(message):
if message.author.bot:
return
if message.content.startswith(prefix):
return
c.execute("SELECT * FROM level WHERE userid=?", (message.author.id,))
data=c.fetchone()
if data is None:
c.execute("INSERT INTO level VALUES(?, ?, ?)",(message.author.id, 1, 0))
conn.commit()
return
c.execute("UPDATE level set exp=? WHERE userid=?",(data[2]+1, message.author.id))
conn.commit()
c.execute("SELECT * FROM level WHERE userid=?", (message.author.id,))
data=c.fetchone()
if data[2] >= data[1]*5:
c.execute("UPDATE level set level=?,exp=? WHERE userid=?",(data[1]+1,0,message.author.id))
conn.commit()
await message.channel.send("レベルアップしました")
@bot.command()
async def level(ctx, target:discord.User=None):
if target is None:
user=ctx.author
else:
user=target
c.execute("SELECT * FROM level WHERE userid=?", (user.id,))
data=c.fetchone()
if data is None:
await ctx.send("ユーザーが登録されていません")
e=discord.Embed(title=f"{user}のランク", description=f"Lv.{data[1]}")
await ctx.send(embed=e)
@bot.command()
async def rank(ctx):
r={}
title="ランク(トップ3まで)"
c.execute("SELECT * FROM level")
for i in c.fetchall():
user=await bot.fetch_user(i[0])
r[i[1]]=user.name
rag=[i for i in r]
rank=sorted(rag, reverse=True)
b=0
description="\n".join(f"{r[f]}" for f in rank)
e=discord.Embed(title=title, description=description)
await ctx.send(embed=e)
bot.run(token)
これが改変してないcodeです
改変していきます
from discord.ext import commands
import discord
import os
import aiohttp
import sqlite3
import datetime #datetimeを入れました
import asyncio
from PIL import ImageFont, ImageDraw, Image
from io import BytesIO
conn=sqlite3.connect("level/level.db", check_same_thread=False)
c=conn.cursor()
prefix="?"
token = "your token"
def jst():
now = datetime.datetime.utcnow()
now = now + datetime.timedelta(hours=9)
return now
client=commands.Bot(command_prefix=prefix)
c.execute("CREATE TABLE IF NOT EXISTS level(userid, level, exp)")
@client.event
async def on_ready():
print("起動")
@client.listen("on_message")
async def level_count(message):
if message.author.bot:
return
if message.content.startswith(prefix):
return
c.execute("SELECT * FROM level WHERE userid=?", (message.author.id,))
data=c.fetchone()
if data is None:
c.execute("INSERT INTO level VALUES(?, ?, ?)",(message.author.id, 1, 0))
conn.commit()
return
c.execute("UPDATE level set exp=? WHERE userid=?",(data[2]+1, message.author.id))
conn.commit()
c.execute("SELECT * FROM level WHERE userid=?", (message.author.id,))
data=c.fetchone()
if data[2] >= data[1]*5:
c.execute("UPDATE level set level=?,exp=? WHERE userid=?",(data[1]+1,0,message.author.id))
conn.commit()
await message.channel.send("レベルアップしました")
@client.command()
async def level(ctx, target:discord.User=None):
if target is None:
user = ctx.author
else:
user=target
c.execute("SELECT * FROM level WHERE userid=?", (user.id,))
data=c.fetchone()
if data is None:
await ctx.send("ユーザーが登録されていません")
img = Image.open("level/rank.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("level/ラノベPOP.ttf", 35)
font1 = ImageFont.truetype("level/ラノベPOP.ttf", 24)
font2 = ImageFont.truetype("level/ラノベPOP.ttf", 20)
async with aiohttp.ClientSession() as session:
async with session.get(str(ctx.author.avatar_url)) as response:
image = await response.read()
icon = Image.open(BytesIO(image)).convert("RGBA")
img.paste(icon.resize((156, 156)), (50, 60))
draw.text((265, 125), f"{data[1]}", (255, 255, 255), font=font)
now = jst()
draw.text((510, 250), now.strftime('%Y年%m月%d日%H時%M分%S秒'), (255, 255, 255), font=font2)
draw.text((50,220), f"{ctx.author.name}", (255, 255, 255), font=font1)
draw.text((50,240), f"#{ctx.author.discriminator}", (255, 255, 255), font=font1)
img.save('level/infoimg2.png') #Change Leveling/infoimg2.png if needed.
ffile = discord.File("level/infoimg2.png")
await ctx.send(file=ffile)
@client.command()
async def rank(ctx):
r={}
title="ランク(トップ3まで)"
c.execute("SELECT * FROM level")
for i in c.fetchall():
user=await client.fetch_user(i[0])
r[i[1]]=user.name
rag=[i for i in r]
rank=sorted(rag, reverse=True)
b=0
description="\n".join(f"{r[f]}" for f in rank)
e=discord.Embed(title=title, description=description)
await ctx.send(embed=e)
client.run(token, bot=True)
ファイル2個フォルダ一個
計3個