Я работаю над собственной валютной системой Python для своего бота Discord. Это выглядит законно, но я не знаю, безопасно ли это. В нем предусмотрена защита от копирования, поэтому пользователи зарабатывают честно и честно. Это работает следующим образом:
Всякий раз, когда пользователю нужны деньги, он использует функцию /addcoin, чтобы добавить деньги на свой счет. /addcoin требует кода, поэтому он должен добыть код, который является вводом хэша SHA256 (показан в /coin). Если все верно, он получает доллар, и система добавляет его идентификатор в файл. После этого система автоматически обновляет код, делая последний код непригодным для использования. Это означает, что он не сможет использовать его снова. Не волнуйтесь, проект размещается локально на компьютере Raspberry Pi и т. п., поэтому файлы будут в безопасности.
Вот денежная часть моего бота. Извините, если слишком длинно!
@bot.tree.command(name="balance", description="Shows the user's current balance")
async def balance(interaction: discord.Interaction):
await interaction.response.defer()
"""Shows the user's current balance."""
ensure_file_exists(MONEY_FILE, default_content={})
user_id = str(interaction.user.id)
with open(MONEY_FILE, "r") as file:
money_data = json.load(file)
balance = money_data.get(user_id, 0)
await interaction.followup.send(f"Your balance is ${balance}")
@bot.tree.command(name="coin", description="Shows the SHA-256 hash of the money key")
async def coin(interaction: discord.Interaction):
await interaction.response.defer()
"""Shows the SHA-256 hash of the money key."""
if os.path.exists(SHA256_FILE):
with open(SHA256_FILE, "r") as file:
sha256_hash = file.read().strip()
await interaction.followup.send(f"SHA-256 Hash: {sha256_hash}")
else:
await interaction.followup.send("SHA-256 hash file does not exist.")
@bot.tree.command(name="addcoin", description="Decodes the given answer and updates balance if correct")
@app_commands.describe(answer="The answer to decode")
async def addcoin(interaction: discord.Interaction, answer: str):
await interaction.response.defer()
"""Decodes the given answer and updates balance if correct."""
ensure_file_exists(MONEY_FILE, default_content={})
ensure_file_exists(RECEIVED_FILE, default_content={})
user_id = str(interaction.user.id)
if os.path.exists(SHA256_FILE):
with open(SHA256_FILE, "r") as file:
correct_answer = file.read().strip()
hashed_answer = hashlib.sha256(answer.encode()).hexdigest()
if hashed_answer == correct_answer:
with open(RECEIVED_FILE, "r") as file:
received_data = json.load(file)
if user_id in received_data:
await interaction.followup.send("You have already claimed your reward.")
return
with open(MONEY_FILE, "r") as file:
money_data = json.load(file)
money_data[user_id] = money_data.get(user_id, 0) + 1
with open(MONEY_FILE, "w") as file:
json.dump(money_data, file)
received_data[user_id] = True
with open(RECEIVED_FILE, "w") as file:
json.dump(received_data, file)
await interaction.followup.send("Correct answer! $1 has been added to your balance.")
await newcoin(interaction)
else:
await interaction.followup.send("Incorrect answer. Please try again.")
else:
await interaction.followup.send("SHA-256 hash file does not exist.")
async def newcoin(interaction: discord.Interaction):
"""Renew the money key and update the SHA-256 hash."""
new_key = ''.join(random_.choices(string.ascii_letters + string.digits, k=32))
new_hash = hashlib.sha256(new_key.encode()).hexdigest()
with open(KEY_FILE, "w") as key_file:
key_file.write(new_key)
with open(SHA256_FILE, "w") as hash_file:
hash_file.write(new_hash)
if os.path.exists(RECEIVED_FILE):
os.remove(RECEIVED_FILE)
await interaction.followup.send("The money key has been renewed.")
@bot.tree.command(name="transfer", description="Transfer money to another user")
@app_commands.describe(recipient="The user to transfer money to", amount="The amount of money to transfer")
async def transfer(interaction: discord.Interaction, recipient: discord.User, amount: int):
await interaction.response.defer()
"""Transfer money to another user."""
ensure_file_exists(MONEY_FILE, default_content={})
user_id = str(interaction.user.id)
recipient_id = str(recipient.id)
if amount
Подробнее здесь: https://stackoverflow.com/questions/788 ... ncy-system