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
JavaScript
x
42
42
1
import discord
2
import random
3
from discord.ext import commands
4
5
client = commands.Bot(command_prefix = 's!')
6
servermultiplier = 1
7
servercoins = 0
8
9
@client.event
10
async def on_ready():
11
global servercoins, servermultiplier
12
file1 = open("C:/Users/squid/Documents/Stuff/ok/save.txt", 'r')
13
count = 0
14
result = []
15
for line in file1:
16
count += 1
17
result.append( line.strip())
18
servercoins = result[0]
19
serermultiplier = result[1]
20
print(servercoins)
21
print('Bot is ready!')
22
channel = client.get_channel(id=864049237679538189)
23
24
@client.command(aliases=['8ball', '8b', 'eb'])
25
async def eightball(ctx, *, question):
26
responses = ['no', 'kinda', 'i guess?', 'yes', '100%', 'absolutely not', 'what are you dumb?', 'sure', 'totaly']
27
await ctx.send(f'Question: {question}nAnwser: {random.choice(responses)}')
28
29
@client.command(aliases=['b','money','cash','bal'])
30
async def balance(ctx):
31
await ctx.send(f'Server has: **${servercoins}**')
32
33
@client.command(aliases=['give'])
34
async def givemoney(ctx):
35
global servercoins, servermultiplier
36
print('give', 1 * servermultiplier) #<- prints fine
37
servercoins += 1 * servermultiplier #<- hangs here
38
#anything below here doesnt work
39
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
40
print('given')
41
await ctx.send(f'Server now has: **${servercoins}**')
42
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)
Advertisement
Answer
Initially, you set
JavaScript
1
3
1
servermultiplier = 1
2
servercoins = 0
3
but then modified it into a string in on_ready
:
JavaScript
1
6
1
for line in file1:
2
count += 1
3
result.append( line.strip())
4
servercoins = result[0]
5
serermultiplier = result[1] # note that there's a typo here
6
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.
JavaScript
1
7
1
>>> servercoins = '0'
2
>>> servermultiplier = 1
3
>>> servercoins += 1 * servermultiplier
4
Traceback (most recent call last):
5
File "<stdin>", line 1, in <module>
6
TypeError: can only concatenate str (not "int") to str
7
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.