0

I am making a blacklist command for my discord bot that interacts with JSON to read and write a JSON file to add or remove blacklisted users. I have a file named _json.py that houses the read and write functions, and it looks like this.

import json
from pathlib import Path

def get_path():
    cwd = Path(__file__).parents[1]
    cwd = str(cwd)
    return cwd

def read_json(filename):
    cwd = get_path()
    with open(cwd+'/bot_config'+filename+'.json', 'r') as file:
        data = json.load(file)
    return data   

def write_json(data, filename):
    cwd = get_path()
    with open(cwd+'/bot_config'+filename+'.json', 'w') as file:
        json.dump(data, file, indent=4)

The file that houses my blacklist/unblacklist command, and all other moderation commands is called moderation.py. Here are the imports and the blacklist/unblacklist commands.

import discord
from discord.ext import commands
from datetime import datetime
import _json

class Moderation(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    @commands.has_permissions(ban_members=True)
    async def blacklist(self, context, member : discord.Member):
        if context.message.author.id == member.id:
            myEmbed = discord.Embed(title="ERROR: Unable to Self Punish", timestamp=datetime.utcnow(), color=0xFF0000)
            myEmbed.set_footer(icon_url=context.author.avatar_url, text=f"Invoked by {context.message.author}")

            await context.message.channel.send(embed=myEmbed)
            return
        
        self.client.blacklisted_users.append(member.id)
        data = _json.read_json("blacklist")
        data["blacklistedUsers"].append(member.id)
        _json.write_json(data, "blacklist")
        await context.send(f"{member.mention} has been blacklisted.")

    @commands.command()
    @commands.has_permissions(ban_members=True)
    async def unblacklist(self, context, member : discord.Member):
        self.client.blacklisted_users.remove(member.id)
        data = _json.read_json("blacklist")
        data["blacklistedUsers"].remove(member.id)
        _json.write_json(data, "blacklist")
        await context.send(f"{member.mention} has been unblacklisted.")

def setup(client):
    client.add_cog(Moderation(client))

When I try to use the blacklist command on a user in my test server, it raises this error in the terminal.

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: module '_json' has no attribute 'read_json'

1 Answer 1

1

_json is the name of a built-in module so your interpreter will take that one instead of your own file. Give it another name like json_utils and import that instead. I know you added the underscore to not confuse it with the existing json module (without underscore), but the module with the underscore also exists.

Sign up to request clarification or add additional context in comments.

10 Comments

when i rename the file and try to import it, i get an unresolved import error.
I'm gonna have to see your directory structure & the new import to help you with that.
I renamed the file to _blacklist and changed the import _json to import _blacklist. The _blacklist is underlined and is an unresolved import.
This is why I asked for your directory structure - are those two in the same directory, or is either of them in a subdirectory? What IDE are you using?
Both the _blacklist.py file and the moderation.py where I am trying to import blacklist file are in the same folder. I am using Visual Studio Code. My project is: Main Folder: bot | Inside Bot: pycache*| *bot_config - json files in here | cogs - houses my listeners.py, general.py, _blacklist.py and moderation.py | git.ignore & main.py (in bot folder)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.