0% found this document useful (0 votes)
2 views2 pages

PdcA3njs

This document contains a Python script for a Discord bot that generates linking codes and handles JSON file uploads. The bot allows users to create a linking code that expires in 30 minutes and checks for user matches, while also enabling the upload of JSON files containing item data. It includes error handling and uses asynchronous programming for managing user interactions.

Uploaded by

bigfarts123pugs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

PdcA3njs

This document contains a Python script for a Discord bot that generates linking codes and handles JSON file uploads. The bot allows users to create a linking code that expires in 30 minutes and checks for user matches, while also enabling the upload of JSON files containing item data. It includes error handling and uses asynchronous programming for managing user interactions.

Uploaded by

bigfarts123pugs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import discord

from discord import app_commands


from discord.ext import commands
import asyncio
import random
import string
import json

TOKEN = 'MTM4NDY0MDU2NTI5OTI1MzMzMQ.GPlhGo.TgL3KYBe-WiDxDUv0bnGZ24SC_KSHT4GNCarbc'
# Replace this with your bot token

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

bot = commands.Bot(command_prefix="!", intents=intents)

linking_codes = {}

@bot.event
async def on_ready():
print(f"Bot is online as {bot.user}")
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands.")
except Exception as e:
print(e)

@bot.tree.command(name="linkcode", description="Generate a linking code")


async def linkcode(interaction: discord.Interaction):
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
linking_codes[code] = None
await interaction.response.send_message(f"Your linking code is: `{code}`.
Expires in 30 minutes.", ephemeral=True)

async def wait_for_join():


for _ in range(360): # 30 minutes / 5 seconds
await asyncio.sleep(5)
# Replace this with your own game backend check logic
dummy_user = "Player_" + code.lower() # Pretend someone joined
if dummy_user: # Simulate a match
await interaction.followup.send(f"Is this you? `{dummy_user}`
(yes/no)", ephemeral=True)

def check(m):
return m.author.id == interaction.user.id and m.content.lower()
in ["yes", "no"]

try:
msg = await bot.wait_for("message", check=check, timeout=30)
if msg.content.lower() == "yes":
linking_codes[code] = dummy_user
await interaction.followup.send(f"Linked to: `{dummy_user}`
✅", ephemeral=True)
return
else:
await interaction.followup.send("Rechecking...",
ephemeral=True)
except asyncio.TimeoutError:
await interaction.followup.send("Timeout. Rechecking...",
ephemeral=True)

bot.loop.create_task(wait_for_join())

@bot.tree.command(name="json", description="Upload a JSON with items")


@app_commands.describe(json_file="Your item JSON file")
async def json_upload(interaction: discord.Interaction, json_file:
discord.Attachment):
try:
content = await json_file.read()
data = json.loads(content)

code = data.get("linking_code")
player = linking_codes.get(code)
if player:
await interaction.response.send_message(f"Items assigned to
`{player}` 🎁", ephemeral=True)
else:
await interaction.response.send_message("Invalid or expired linking
code ⛔", ephemeral=True)

except Exception as e:
await interaction.response.send_message(f"Error: {e}", ephemeral=True)

bot.run(TOKEN)

You might also like