From 27436bd3043134df0fc233a76775efd27a815ec2 Mon Sep 17 00:00:00 2001 From: ssyyhhrr <808648@student.derby-college.ac.uk> Date: Tue, 23 Nov 2021 19:56:17 +0000 Subject: [PATCH] Initial commit --- .env | 1 + main.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ users.json | 5 ++++ 3 files changed, 82 insertions(+) create mode 100644 .env create mode 100644 main.py create mode 100644 users.json diff --git a/.env b/.env new file mode 100644 index 0000000..689913b --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DISCORD_TOKEN=OTExMTg2OTA0NTM4NTc4OTQ0.YZdu7w.bSDm8KoGuzoFaKwsRVL2uwYx19s \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..070722a --- /dev/null +++ b/main.py @@ -0,0 +1,76 @@ +import os +import json +import random +import discord +from dotenv import load_dotenv +from discord.ext import commands + +load_dotenv() +TOKEN = os.getenv("DISCORD_TOKEN") + +INTENTS = discord.Intents.default() +INTENTS.members = True + +bot = commands.AutoShardedBot(command_prefix=commands.when_mentioned_or("!"), owner_id=518124039714242562, intents=INTENTS) + +@bot.event +async def on_ready(): + print(f"{bot.user} has connected to Discord!") + +@bot.event +async def on_member_join(member): + if not member.id in data: + data[member.id] = {'coins': 0} + with open("users.json", "w") as f: + json.dump(data, f, indent=4) + +@bot.command() +async def ping(ctx): + await ctx.send("Pong!") + +@bot.command() +async def coins(ctx): + getConfig() + await ctx.reply(f"You have {str(data[str(ctx.message.author.id)]['coins'])} coins.") + +@bot.command() +async def roulette(ctx, bet = None, colour = None): + getConfig() + if not bet or not colour: + await ctx.send("Invalid syntax. `!roulette (bet) (colour)`") + return + try: + betAmount = int(bet) + if betAmount < 1: + raise Exception() + except: + await ctx.send("Enter a valid bet amount.") + return + if colour != "red" and colour != "black": + await ctx.send("Enter a valid colour.") + return + if data[str(ctx.message.author.id)]["coins"] < betAmount: + await ctx.send("You do not have sufficient coins.") + return + + select = random.randint(0, 1) + if didWin(colour, select): + data[str(ctx.message.author.id)]["coins"] += betAmount + else: + data[str(ctx.message.author.id)]["coins"] -= betAmount + setConfig() + await ctx.send(f"{'🔴' if select == 0 else '⚫'} The ball landed on {'red' if select == 0 else 'black'}. You {'won' if (didWin(colour, select)) else 'lost'} **{betAmount * (2 if (didWin(colour, select)) else 1)}** coins{'!' if (didWin(colour, select)) else '.'}") + +def getConfig(): + global data + with open("users.json", "r") as f: + data = json.load(f) + +def setConfig(): + with open("users.json", "w") as f: + json.dump(data, f, indent=4) + +def didWin(colour, select): + return (colour == 'red' and select == 0) or (colour == 'black' and select == 1) + +bot.run(TOKEN) \ No newline at end of file diff --git a/users.json b/users.json new file mode 100644 index 0000000..e15a1fd --- /dev/null +++ b/users.json @@ -0,0 +1,5 @@ +{ + "534077333443969026": { + "coins": 200 + } +} \ No newline at end of file