This is part of my script that handels getting a custom prefix (If needed I can send all of it)
settings = json.load(open("settings.json"))
# funtion that gets the prefix from the settings file depening on guild id
def get_prefix(bot, messages):
guild = messages.guild.id
guild = str(guild)
settings["guilds"][0][guild][0]["prefix"] = prefix
return prefix
client = commands.Bot(command_prefix=get_prefix, help_command=None)
@client.event
async def on_guild_join(guild):
print(f"Joined {guild.name}")
print(f"{guild.name} has {guild.member_count} members")
print(f"{guild.name}'s id is {guild.id}")
print(f"{guild.name}'s owner is {guild.owner}")
print(f"{guild.name}'s region is {guild.region}")
settings["guilds"][0][guild.id][0]["prefix"] = "?"
settings["guilds"][0][guild.id][0]["guild_name"] = guild.name
settings["guilds"][0][guild.id][0]["guild_id"] = guild.id
settings["guilds"][0][guild.id][0]["guild_owner"] = guild.owner
settings["guilds"][0][guild.id][0]["guild_region"] = guild.region
json.dump(settings, open("settings.json", "w"))
@client.event
async def on_ready():
print("------")
print("Logged in as")
print(client.user.name)
print(client.user.id)
print("------")
@client.command()
async def prefix(ctx, *, prefix):
guild = ctx.guild.id
settings["guilds"][0][guild][0]["prefix"] = prefix
json.dump(settings, open("settings.json", "w"))
That’s my current script for getting the prefix and setting it depending which server it called from.
It worked great on my test bot, but when I transferred it over it seemed not to anymore.
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 979, in on_message
await self.process_commands(message)
File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 975, in process_commands
ctx = await self.get_context(message)
File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 886, in get_context
prefix = await self.get_prefix(message)
File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 831, in get_prefix
ret = await discord.utils.maybe_coroutine(prefix, self, message)
File "C:\Python39\lib\site-packages\discord\utils.py", line 341, in maybe_coroutine
value = f(*args, **kwargs)
File "c:\Users\furrc\Code\Python\GIF Bot\bot.py", line 16, in get_prefix
settings["guilds"][0][guild][0]["prefix"] = prefix
TypeError: 'Command' object is not subscriptable
Thats my error code I get, it says TypeError: 'Command' object is not subscriptable but I have no idea where Command is, its not included in my .json file
{
"guilds": [
{
"771773406144233514":[
{
"id": 771773406144233514,
"name": "Makashi's Bot Testing Server",
"prefix": "!"
}
]
,
"810686563138142218":[
{
"id": 810686563138142218,
"name": "SPAM",
"prefix": "?"
}
]
}
]
}
That’s my json folder and it doesnt contain Command in it, so I’m not sure why it being called.
>Solution :
Probably, settings variable was shadowed with some other settings from some library you use. Try to make it guilds_settings and refer to the variable by using the global keyword.