Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当命令位于DM中时,Python Discord.py bot将角色分配给服务器中的用户_Python_Discord.py_Discord.py Rewrite - Fatal编程技术网

当命令位于DM中时,Python Discord.py bot将角色分配给服务器中的用户

当命令位于DM中时,Python Discord.py bot将角色分配给服务器中的用户,python,discord.py,discord.py-rewrite,Python,Discord.py,Discord.py Rewrite,我正在尝试在服务器中获得一个bot,当它被dm'ed时,它会给用户一个特定的角色。现在我只在服务器中使用命令时才让它工作。有没有办法让它进入dm频道 @client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java", aliases=["lan

我正在尝试在服务器中获得一个bot,当它被dm'ed时,它会给用户一个特定的角色。现在我只在服务器中使用命令时才让它工作。有没有办法让它进入dm频道

@client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java",
                aliases=["lang"])
async def language(ctx, lang=""):
    if isinstance(ctx.channel, discord.DMChannel):
        if lang.lower() not in ["python", 'c++', 'java', 'c#']:
            await ctx.send("The available languages are Python, C++, C#, and Java.")
        else:
            await ctx.send("Your role is now " + lang.lower().capitalize() + ".")
            await ctx.message.add_reaction("You require to found the guild you want to give the rule to the user in that guild.

If your bot is a private bot for one server you can get the server id and use
client.get_guild(id)
and use the guild as reference or if your bot is a public bot which will be used in several servers you should add another argument as guild ID which should be provided by the user.

If you are going to make a private bot so here is your edited code:

@client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java",
                aliases=["lang"])
async def language(ctx, lang=""):
    guild_id = 123456789   # Replace it with yours
    if lang.lower() not in ["python", 'c++', 'java', 'c#']:
        await ctx.send("The available languages are Python, C++, C#, and Java.")
    else:
        await ctx.send("Your role is now "+lang.lower().capitalize()+".")
        await ctx.message.add_reaction("👌")
        role = get(client.get_guild(guild_id).roles, name=lang.lower().capitalize())
        roles = [role for role in ctx.message.author.roles if role.name in ['C++', "Java", 'Python', 'C#']]
        await ctx.message.author.remove_roles(*roles)
        await ctx.message.author.add_roles(role)
<代码> @ Client命令(Stase=)设置“最喜欢的编程语言”,描述=“设置Python、C++、Cype或java的角色”, 别名=[“lang”]) 异步定义语言(ctx,lang=”“): 如果isinstance(ctx.channel,discord.DMChannel): 如果lang.lower()不在[“python”、“c++”、“java”、“c#”中]: 等待CTX。(“可用语言是Python、C++、Cype和java”)。 其他: 等待ctx.send(“您的角色现在是“+lang.lower().capitalize()+”)
等待ctx.message.add_reaction(“您需要找到您想要将规则提供给该公会中的用户的公会)

如果您的bot是一台服务器的私有bot,您可以获取服务器id并使用
客户端。获取帮会(id)
并将帮会用作参考,或者如果您的bot是一个公共bot,将在多台服务器中使用,您应该添加另一个参数作为用户应提供的帮会id

如果您要制作一个私人机器人,那么以下是您编辑的代码:

<代码> @ Client命令(Stase=)设置“最喜欢的编程语言”,描述=“设置Python、C++、Cype或java的角色”, 别名=[“lang”]) 异步定义语言(ctx,lang=”“): 公会id=123456789#将其替换为您的公会id 如果lang.lower()不在[“python”、“c++”、“java”、“c#”中]: 等待CTX。(“可用语言是Python、C++、Cype和java”)。 其他: 等待ctx.send(“您的角色现在是“+lang.lower().capitalize()+”) 等待ctx.消息。添加反应(“🌔) role=get(client.get\u guild(guild\u id).roles,name=lang.lower().capitalize()) roles=[ctx.message.author.roles中角色的角色,如果['C++','Java','Python','C#']]中的role.name中的role 等待ctx.message.author.remove_角色(*角色) 等待ctx.message.author.add_角色(角色)
如果你是一个公共机器人

<代码> @ Client命令(Stase=)设置“最喜欢的编程语言”,描述=“设置Python、C++、Cype或java的角色”, 别名=[“lang”]) 异步定义语言(ctx,*args): lang=args[0] guild=client.get_guild(int(args[1]),如果是instance(ctx.channel,discord.DMChannel)或ctx.guild 如果lang.lower()不在[“python”、“c++”、“java”、“c#”中]: 等待CTX。(“可用语言是Python、C++、Cype和java”)。 其他: 等待ctx.send(“您的角色现在是“+lang.lower().capitalize()+”) 等待ctx.消息。添加反应(“🌔) role=get(ctx.message.guild.roles,name=lang.lower().capitalize()) roles=[guild.get_member(ctx.author.id)中角色的角色。role.name在['C++','Java','Python','C#']中的角色 等待ctx.message.author.remove_角色(*角色) 等待ctx.message.author.add_角色(角色)
在您的第一个解决方案中,我认为ctx.author.roles不起作用。我得到discord.ext.commands.errors.CommandInvokeError:Command引发了一个异常:AttributeError:“User”对象没有属性“roles”@JohnHenry5 my bad☺ 我不想改变我刚才所做的。这个解决方案被支持在dm频道中工作。如果你看到上面,我在isinstance(ctx.channel,discord.DMChannel)中加入一个If语句,以检查它是否在dm频道中。我在“私有”中使用你的解决方案“bot和它不工作。我需要以某种方式获得服务器id中相同用户的引用。