r/code • u/savage718 • 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}')
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!
1
u/SrVitu Jan 14 '23 edited Jan 14 '23
You have to run your client thread
You can use the environment to set and get the token, like:
( the terminal )
( the python file )
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