Home > OS >  discord bot command runs, but gets stuck trying to multiply
discord bot command runs, but gets stuck trying to multiply

Time:04-16

everything works fine, it prints the multiplication but it gets stuck on adding more coins to the server, it worked ok until I added the multiplier

import discord 
import random
from discord.ext import commands

client = commands.Bot(command_prefix = 's!')
servermultiplier = 1
servercoins = 0

@client.event
async def on_ready():
    global servercoins, servermultiplier
    file1 = open("C:/Users/squid/Documents/Stuff/ok/save.txt", 'r')
    count = 0
    result = []
    for line in file1:
        count  = 1
        result.append( line.strip())
    servercoins = result[0]
    serermultiplier = result[1]
    print(servercoins)
    print('Bot is ready!')
    channel = client.get_channel(id=864049237679538189)

@client.command(aliases=['8ball', '8b', 'eb'])
async def eightball(ctx, *, question):
    responses = ['no', 'kinda', 'i guess?', 'yes', '100%', 'absolutely not', 'what are you dumb?', 'sure', 'totaly']
    await ctx.send(f'Question: {question}\nAnwser: {random.choice(responses)}')

@client.command(aliases=['b','money','cash','bal'])
async def balance(ctx):
    await ctx.send(f'Server has: **${servercoins}**')

@client.command(aliases=['give'])
async def givemoney(ctx):
    global servercoins, servermultiplier
    print('give', 1 * servermultiplier) #<- prints fine
    servercoins  = 1 * servermultiplier #<- hangs here
    #anything below here doesnt work
    #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    print('given')
    await ctx.send(f'Server now has: **${servercoins}**')

any help is appreciated (typing random stuff because my post is mostly code and i need to put these edits but stack overflow is being rude so yeah)

CodePudding user response:

Initially, you set

servermultiplier = 1
servercoins = 0

but then modified it into a string in on_ready:

    for line in file1:
        count  = 1
        result.append( line.strip())
    servercoins = result[0]
    serermultiplier = result[1]  # note that there's a typo here

Here, result is a list of strings, so servercoins is set to a string. (This is why globals are bad!)

You later try to add it servercoins = 1 * servermultiplier, which is not allowed.

>>> servercoins = '0'
>>> servermultiplier = 1
>>> servercoins  = 1 * servermultiplier
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

I still don't know why it causes the code to hang. I would assume that the exception stopped the rest of the command from executing, and then it was silently ignored later on.

  • Related