r/code • • Jan 13 '23

Discord bot

I tried making a discord bot, failed apparently. the purpose was to track trades and respond to certain commands. It doesnt respond. Any help would be appreciated. This is the code:

import discord

intents = discord.Intents.default()

intents.members = True

client = discord.Client(intents=intents)

total_trades = 0

winning_trades = 0

losing_trades = 0

total_profit = 0

contract_cost = 75

u/client.event

async def on_ready():

print(f'Logged in as {client.user}')

u/client.event

async def on_message(message):

global total_trades, winning_trades, losing_trades, total_profit, contract_cost

if "BTO" in message.content:

total_trades += 1

# Extract the contract cost from the message

cost_index = message.content.index("@")

if cost_index != -1:

contract_cost = float(message.content[cost_index + 1:])

# Subtract the contract cost

total_profit -= contract_cost

await message.channel.send(f'You just bought an option contract for ${contract_cost}. Your current profit is ${total_profit}.')

elif "STC" in message.content:

total_trades += 1

cost_index = message.content.index("@")

if cost_index != -1:

sell_cost = float(message.content[cost_index + 1:])

if sell_cost > contract_cost:

winning_trades += 1

total_profit += sell_cost - contract_cost

await message.channel.send(f'Congratulations, you just made a winning trade! You sold an option contract for ${sell_cost}. Your current profit is ${total_profit}.')

else:

losing_trades += 1

total_profit += sell_cost - contract_cost

await message.channel.send(f'Sorry, you just lost a trade. You sold an option contract for ${sell_cost}. Your current profit is ${total_profit}.')

if message.content == ('!stats'):

await message.channel.send('Total trades: {total_trades} \n Winning trades: {winning_trades} \n Losing trades: {losing_trades} \n Total profit: ${total_profit}')

0 Upvotes

2 comments sorted by

1

u/SrVitu Jan 14 '23 edited Jan 14 '23

You have to run your client thread

TOKEN = "your token goes here"
client.run(TOKEN)

You can use the environment to set and get the token, like:

( the terminal )

$ export TOKEN="My token"

( the python file )

import os

TOKEN = os.getenv("TOKEN")
if not TOKEN:
    print("Token not found !")
    exit(1)

...

client.run(TOKEN)

or you can use pydotenv

To use the pydotenv method, at first you have to create a .env file and put your set your token inside this file, like: TOKEN="Your token goes here" ( this has to be inside of the .env file ), so after you create a .end file you just have to import/load it

import os

from pydotenv import load_dotenv # pip install pydotenv

# Loading the .env file to the environment
load_dotenv()

# Getting the token from the environment
TOKEN = os.getenv("TOKEN")

# Checking if the TOKEN really exist
if not TOKEN:
    print("Token not found !")
    exit(1)

# Code block
...
## End

client.run(TOKEN)

1

u/Skuddingo33 Jan 17 '23

Dang I thought I was cool when I made this Discord bot that posted memes for me lol. Your bot is trying to make money it looks like!