Reputation: 11
so the thing is, i'm making a discord bot in python with python.py and i'm making a command to mute someone by put their user id in a json file.
@client.command()
async def mute(user):
with open("muted.json", 'r') as f:
data = json.load(f)
if not user.id in data:
data[user.id] = {}
else:
await client.send_message(message.channel, "The user is already muted")
It's saying at "if not user.id in data:" that "AttributeError: 'str' object has no attribute 'id'" How can I fix that ?
Upvotes: 1
Views: 10756
Reputation: 60954
By default, all arguments to commands are strings. If you want the library to convert them for you, you have to tell it what type you want it converted to by supplying a converter with a type annotation. If you want to reference the message
that invoked the command, you'll also have to tell the librtary to pass the invocation context into the command callback.
@client.command(pass_context=True)
async def mute(ctx, user: discord.User):
with open("muted.json", 'r') as f:
data = json.load(f)
if not user.id in data:
data[user.id] = {}
else:
await client.send_message(message.channel, "The user is already muted")
It's worth noting that this command doesn't really do anything. It creates a dictionary from a file, modifies it, then discards it when the function ends. You should instead have a module-level data
dictionary that is loaded once, then saved whenever you modify it.
Upvotes: 2