Home > Mobile >  How do i take user input from discord and store it as a variable to use in an equation? Discord.py
How do i take user input from discord and store it as a variable to use in an equation? Discord.py

Time:11-27

Ok so im not intierly fluent in discord.py but heres the code im trying to use:

from discord import channel
from discord.embeds import Embed
import discord
from discord.ext import *
import os
import random
import string
from discord.ext import commands
from dotenv import load_dotenv
from discord.ext import commands
import requests
import sys
import threading
from discord.utils import get
import discord
import asyncio
from threading import Thread
from keep_alive import keep_alive

load_dotenv()
bot = commands.Bot(command_prefix = '.')

def init():
    loop = asyncio.get_event_loop()
    loop.create_task(bot.run(token))
    Thread(target=loop.run_forever).start()
    
@bot.event
async def on_ready():
  print('Bot is up and running')


@bot.command()
async def p(ctx):
  # MESSAGE 1, should have user input stored in variable "place"
  embed=discord.Embed()
  embed.add_field(name="Where did you place?", value="** **")
  sent = await ctx.send(embed=embed)

  # MESSAGE 2, should have user input stored in variable "kills"
  embed=discord.Embed()
  embed.add_field(name="How many kills did you get?", value="** **")
  sent = await ctx.send(embed=embed)

  # MESSAGE 3, should have combined input of place   kills AFTER running PLACEMENT CALC in variable "total""
  embed=discord.Embed()
  embed.add_field(name="Total Points:", value=total)
  sent = await ctx.send(embed=embed)

bot.run("OTA0MDc5MzY5NzU1MDU4MjA2.YX2Thg.S4A2e9SqZUvYZpyTzBW44W4Vumk")
keep_alive()
init()

I want to take the user response from the first and second msgs from discord.py and input them into the first 2 questions of this code so i can claculate the total:

import sys

# Placement input, should be response to MESSAGE 1 In discord.py
print ("Where did you place? ")
place = int(input('| \n'))

#Checking if place is in range from 20-1, Should respond with the message in an embed in discord
if place >20 or place <1:
    place = 0

# Kill input, response to MESSAGE 2 In discord.py
print ("How many kills did you get?")
kills = int(input('| \n '))


# Assigning total / final variable
total = 0
tplace = 0

# ranges for placement variables
top20 = (18,19,20)      #Top 20
top17 = (16,17)         #Top 17
top15 = (13,14,15)      #Top 15
top12 = (11,12)         #Top 12
top10 = (6,7,8,9,10)    #Top 10

#Total kills calculations
kills = kills * 10
    
#Total placement calculations
if place in top20: #Top 20
    tplace = 10
    
if place in top17: #Top 17
    tplace = 20
    
if place in top15: #Top 15
    tplace = 35
    
if place in top12: #Top 12
    tplace = 50
    
if place in top10: #Top 10
    tplace = 60

if place == 5: #Top 5
    tplace = 80
    
if place == 4: #Top 4
    tplace = 90
    
if place == 3: #Top 3
    tplace = 100
    
if place == 2: #Top 2
    tplace = 125    
    
if place == 1: #Top 1 / vic roy
    tlace = 175

#Output, should be MESSAGE 3 In discord.py
total = kills   tplace
print ("Total Points: ")
print (total)

and after calculating the total I just send the variable "total" inside of an emebd, i just dont know hwo to store the response to 2 separate discord message inside of the variables i need. It should run as

Bot: "Where did you place?" 
you: 10 (Stored in variable "place")

Bot:"How many kills did you get?" 
you: 1 (Stored in variable "kills")

---------calculating the total points (should be 70)-------

Bot: "Total points: 70"   

I know im probably being stupid but nothing i find online rlly helps

CodePudding user response:

You can use results

Also, you might want to remove unnecessary code and imports:

import discord, asyncio
from discord.ext import commands
from dotenv import load_dotenv
from keep_alive import keep_alive

load_dotenv()
bot = commands.Bot(command_prefix = '.')

@bot.event
async def on_ready():
    print('Bot is up and running')

# OTHER CODE HERE

keep_alive()
bot.run(Token)
  • Related