Python Discord bot在加入时提供角色

Python Discord bot在加入时提供角色,python,discord,bots,Python,Discord,Bots,我正在尝试制作一个简单的机器人,它将角色分配给刚加入服务器的人 守则: import discord import os from discord.utils import get bot_acces_token = os.environ['token'] intents = discord.Intents.default() intents.members = True client = discord.Client(intents=intents) @client.event asy

我正在尝试制作一个简单的机器人,它将角色分配给刚加入服务器的人

守则:

import discord
import os
from discord.utils import get

bot_acces_token = os.environ['token']

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

@client.event
async def on_member_join(member):
  role = discord.utils.get(member.server.roles, id="123456789")
  await client.add_roles(member, role)

@client.event
async def on_ready():
  print('Bot is ready')

client.run(bot_acces_token)
但不幸的是,我得到了这个错误:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 17, in on_member_join
    role = discord.utils.get(member.server.roles, id="123456789")
AttributeError: 'Member' object has no attribute 'server'

你试图获得结果的方式是旧的,并且已经改变了

您需要更改定义bot的方式。您需要更改这段代码:

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)
必须使用
命令API
才能使用
事件。将这些行更改为:

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
在脚本顶部添加导入语句:

from discord.ext import commands
同时删除以下内容:

from discord.utils import get
请尝试以下方法:

@client.event
async def on_member_join(member):
  role = discord.utils.get(member.guild.roles, id="123456789")
  await member.add_roles(role)
您需要使用
member.guild.roles
而不是您实际使用的角色。您还需要使用
wait member.add_roles(role)
而不是您使用的角色。这对你会有用的。如果您仍然有错误,请立即询问


谢谢D

您试图获得结果的方式已经过时,并且已经改变

您需要更改定义bot的方式。您需要更改这段代码:

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)
必须使用
命令API
才能使用
事件。将这些行更改为:

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
在脚本顶部添加导入语句:

from discord.ext import commands
同时删除以下内容:

from discord.utils import get
请尝试以下方法:

@client.event
async def on_member_join(member):
  role = discord.utils.get(member.guild.roles, id="123456789")
  await member.add_roles(role)
您需要使用
member.guild.roles
而不是您实际使用的角色。您还需要使用
wait member.add_roles(role)
而不是您使用的角色。这对你会有用的。如果您仍然有错误,请立即询问

谢谢这就是解决方案:

import discord
from discord.ext import commands
import os

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@client.event
async def on_member_join(member):
  channel = client.get_channel(1234567)
  role = discord.utils.get(member.guild.roles, id=1234567)
  await member.add_roles(role)
这就是解决方案:

import discord
from discord.ext import commands
import os

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@client.event
async def on_member_join(member):
  channel = client.get_channel(1234567)
  role = discord.utils.get(member.guild.roles, id=1234567)
  await member.add_roles(role)

您尝试添加角色的方式已过时。
server
不是discordpy中任何部分的有效属性。该属性实际上是
guild
您尝试添加角色的方式已经过时。
server
对于discordpy中的任何部分都不是有效的属性。该属性实际上是
guild
我尝试了这个,但这次又出现了一个错误。忽略on_member_join Traceback(最后一次调用)中的异常:文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/client.py”,第343行,在运行事件等待coro(*args,**kwargs)文件“main.py”,第18行,在on_member_join等待成员。添加_角色(角色)文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/member.py”,第777行,添加角色等待请求(公会id、用户id、角色id、原因=原因)AttributeError:'NoneType'对象没有属性'id'@OqOq你做了所有的事情吗?我编辑了答案。是的,我试着做了你告诉我做的所有事情,但它还是让我犯了同样的错误。@OqOq别担心!我会给你写一个代码并给出正确的解释。错误在这里:“123456789”“我试过了,但这次又犯了一个错误。忽略on_member_join Traceback(最后一次调用)中的异常:文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/client.py”,第343行,在运行事件等待coro(*args,**kwargs)文件“main.py”,第18行,在on_member_join等待成员。添加_角色(角色)文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/member.py”,第777行,添加角色等待请求(公会id、用户id、角色id、原因=原因)AttributeError:'NoneType'对象没有属性'id'@OqOq你做了所有的事情吗?我编辑了答案。是的,我试着做了你告诉我做的所有事情,但它还是让我犯了同样的错误。@OqOq别担心!我会给你写一个有正确解释的代码。错误在这里:“123456789”