jcezetah
jcezetah

Reputation: 3

Error (discord.ext.commands.bot Ignoring exception in command pay) When trying to use discord bot commands

I keep getting Errors when trying to use discord bot commands.

    #imports commands from discord.txt
    #imports json files
    import discord
    from discord.ext import commands 
    import json
    import os
    
    os.chdir("C:\\Users\\jceze\\OneDrive\\Desktop\\fletch points bot")
    
    intents = discord.Intents.all()
    
    #client variable
    client = commands.Bot(command_prefix=">",intents=intents)
    
    client = commands.ext import commands
    
    #On ready event
    @client.event
    async def on_ready():
        print("Ready")
    
    #checks balance, creates account in mainbank.json
    @client.command()
    async def balance(ctx):
        await open_account(ctx.author)
        user = ctx.author
        users = await load_bank_data()
    
        bank_amt = users[str(user.id)]["bank"]
    
        em = discord.Embed(titele = f"{ctx.author.name}'s balance", color = discord.Color.red())
        em.add_field(name = "Bank", value = bank_amt)
        await ctx.send(embed = em)
    
    @client.command()
    async def pay(ctx, amount = None):
        await open_account(ctx.author)
        user = ctx.author
        users = await load_bank_data()
    
        if amount == None:
            await ctx.send("Please enter the amount")
            return
        else:
            payment = amount
        
        await ctx.send(f"You have been paid {payment} coins!!")
    
        users[str(user.id)]["bank"] += payment
    
        with open("mainbank.json","w") as f:
            json.dump(users,f)
    
    #helper function to open user bank
    async def open_account(user):
        users = await load_bank_data()
        
        if str(user.id) in users:
            return False
        else:
            users[str(user.id)]["bank"] = 0
    
        with open("mainbank.json", "w") as f:
            json.dump(users,f)
            return True
    
    #helper function which loads bank data
    async def load_bank_data():
        with open("mainbank.json","r") as f:
            users = json.load(f)
    
    #so user can check bank detals
    async def update_bank(user , change = 0, mode = "wallet"):
        users = await load_bank_data()
    
        users[str(user.id)][mode] += change
    
        with open("mainbank.json","w") as f:
            json.dump(users, f)
    
        bal = [users[str(user.id)][mode], users[str(user.id)][mode]]
        return user
    
    client.run( "MTAyODAyMzU1NTM0NDA0ODEzOA.GA7kP5.13TjOpz7N7tz0RPaAGbSW4u8bx5B-8rKkGnmDE")

Error output:

ERROR    discord.ext.commands.bot Ignoring exception in command pay
Traceback (most recent call last):
  File "C:\Users\jceze\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ext\commands\core.py", line 190, 
in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\jceze\OneDrive\Desktop\fletch points bot\bot.py", line 35, in pay
    @client.command()
  File "C:\Users\jceze\OneDrive\Desktop\fletch points bot\bot.py", line 56, in open_account   
    users = await load_bank_data()
TypeError: argument of type 'NoneType' is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File 

"C:\Users\jceze\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ext\commands\bot.py", line 1347, in invoke await ctx.command.invoke(ctx) File "C:\Users\jceze\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ext\commands\core.py", line 986, in invoke await injected(*ctx.args, **ctx.kwargs) # type: ignore File "C:\Users\jceze\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ext\commands\core.py", line 199, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'NoneType' is not iterable

Upvotes: 0

Views: 528

Answers (1)

earningjoker430
earningjoker430

Reputation: 468

load_bank_data() returns None, since you didn't return anything.

In your open_account() function, you assign the variable users to load_bank_data (which we know returns None). Then, you check if str(user.id) in users. Since users is None, you're essentially checking if x in None. in can only be used on iterable objects, like a tuple, or a list. None is not iterable, so that's why it's giving you that error.

TL;DR: At the end of your mainbank.json file, add a return users statement.

Upvotes: 0

Related Questions