简体   繁体   English

discord.py AttributeError: 'str' 对象没有属性 'id'

[英]discord.py AttributeError: 'str' object has no attribute 'id'

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.所以事情是,我正在用 python.py 在 python 中制作一个不和谐的机器人,我正在通过将他们的用户 ID 放在一个 json 文件中来使某人静音。

@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?它在“if not user.id in data:”中说“AttributeError: 'str' object has no attribute 'id'” 我该如何解决?

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.如果你想引用调用命令的message ,你还必须告诉库将调用上下文传递到命令回调中。

@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.相反,您应该有一个模块级data字典,它只加载一次,然后在您修改它时保存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “AttributeError: 'str' object has no attribute 'id'” discord.py 上的错误 - “AttributeError: 'str' object has no attribute 'id'” error on discord.py Discord.py“AttributeError:‘str’对象没有‘channel’属性” - Discord.py "AttributeError: 'str' object has no attribute 'channel'" Discord.py AttributeError: 'str' object 没有属性 'server' - Discord.py AttributeError: 'str' object has no attribute 'server' AttributeError: 'str' object 没有属性 'get' — Discord.py - AttributeError: 'str' object has no attribute 'get' — Discord.py 我收到 discord.py 错误,说 AttributeError: 'str' object has no attribute 'read' - I'm getting a discord.py error saying AttributeError: 'str' object has no attribute 'read' 为什么我得到 discord.py AttributeError: 'str' object has no attribute 'trigger_typing' - Why am I getting discord.py AttributeError: 'str' object has no attribute 'trigger_typing' discord.py 在使用 add_roles 时等待“AttributeError: 'list' object has no attribute 'id'” - discord.py await “AttributeError: 'list' object has no attribute 'id'” while using add_roles Discord.py AttributeError:类型对象“上下文”没有属性“消息” - Discord.py AttributeError: type object 'Context' has no attribute 'message' Discord.py AttributeError:“用户”对象没有“公会”属性 - Discord.py AttributeError: "User" object has no attribute "Guild" AttributeError: 'Command' 对象在 discord.py 中没有属性 'command' - AttributeError: 'Command' object has no attribute 'command' in discord.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM