Python 当某人加入时创建规则协议

Python 当某人加入时创建规则协议,python,discord,discord.py,Python,Discord,Discord.py,我想要一个机器人,它将新成员设置为只能查看一个频道的角色,并且必须在规则上达成一致才能使用服务器。我写这篇文章就是为了做到这一点,但我不断地得到这个错误 Ignoring exception in on_member_join Traceback (most recent call last): File "C:\Users\ezter\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", l

我想要一个机器人,它将新成员设置为只能查看一个频道的角色,并且必须在规则上达成一致才能使用服务器。我写这篇文章就是为了做到这一点,但我不断地得到这个错误

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Users\ezter\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "D:\programing\Discord Bots\Osis Nation\Start.py", line 31, in on_member_join
    role = discord.utils.get(bot.roles, name="Default")
  File "C:\Users\ezter\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 296, in __getattr__
    raise AttributeError(msg.format(self.__class__, name))
AttributeError: '<class 'discord.ext.commands.bot.Bot'>' object has no attribute 'roles'
忽略on\u成员\u连接中的异常
回溯(最近一次呼叫最后一次):
文件“C:\Users\ezter\AppData\Local\Programs\Python\36\lib\site packages\discord\client.py”,第307行,在运行事件中
getattr(自身,事件)的收益率(*args,**kwargs)
文件“D:\Programming\Discord Bots\Osis Nation\Start.py”,第31行,在on_成员_join中
role=discord.utils.get(bot.roles,name=“Default”)
文件“C:\Users\ezter\AppData\Local\Programs\Python\36\lib\site packages\discord\client.py”,第296行,位于\uu getattr__
raise AttributeError(msg.format(self.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
AttributeError:“”对象没有“角色”属性
我用这个代码做的

#Welcomer
@bot.event
async def on_member_join(member):
    print("A member just joined and his name is" + member.name)
    mid = member.id
    role = discord.utils.get(bot.roles, name="Default")
    role3 = discord.utils.get(member.server.roles, id="<479670838648635392>")
    role2 = discord.utils.get(member.server.roles, id="<479692108953944081>")
    await bot.add_roles(member, bot.get_)

    Rules = "1. Do not DM the Owner or any other staff unless they have DMed you first! \n 2. Be kind, \n 3. Use common sense, \n 4. No swearing, \n 5. No racism or bullying, \n 6. No advertising, \n 7. Do not chat in #music Text or Voice channels, \n 8. Do not spam #applications if your application is accepted or denied! This is only to be used for submitting and staff members replying to the application. \n 9. Do not use or abuse bot commands you should not be using! \n 10. Do not @ any staff members unless it is an emergency.  "

    await bot.send_message(bot.get_channel('479685091749134346'), 'Hello <@%s>, Welcome to Osis Nation, please read all the rules carefully' % (mid))
    await bot.send_message(bot.get_channel('479685091749134346'), (Rules))
    await bot.send_message(bot.get_channel('479685091749134346'), 'If you agree write !Agree. If you do not agree write !Decline')

    if member.content.upper().startwith("!Agree"):
        await bot.send_message(bot.get_channel('479685091749134346'), 'You will be redirected to the server in 5 seconds')
        time.sleep(5)

        await bot.remove_roles(member, role)
欢迎光临 @机器人事件 成员加入时的异步定义(成员): 打印(“刚加入的成员,其名称为“+member.name”) mid=member.id role=discord.utils.get(bot.roles,name=“Default”) role3=discord.utils.get(member.server.roles,id=”“) role2=discord.utils.get(member.server.roles,id=”“) 等待bot.add_角色(成员,bot.get_) 规则=“1.除非业主或任何其他员工先告知您,否则请勿告知您\n2。善待他人。使用常识\n 4。禁止骂人,\n 5。没有种族主义或欺凌\n 6。没有广告,\n 7。请勿在音乐文本或语音频道中聊天,\n 8。如果您的申请被接受或拒绝,请不要发送垃圾邮件!这仅用于提交申请和工作人员回复申请\n9。不要使用或滥用您不应该使用的bot命令\n10。除非是紧急情况,否则不要对任何工作人员下毒。" 等待bot.send_消息(bot.get_频道('479685091749134346'),'您好,欢迎来到Osis国家,请仔细阅读所有规则'%(mid)) 等待bot.send_消息(bot.get_通道('479685091749134346'),(规则)) 等待bot.send_消息(bot.get_频道('479685091749134346'),“如果你同意写!同意。如果你不同意写!拒绝”) 如果member.content.upper().startwith(“!Agree”): 等待bot.send_消息(bot.get_通道('479685091749134346'),“您将在5秒钟内重定向到服务器” 时间。睡眠(5) 等待bot。删除_角色(成员、角色)
我认为最简单的方法是对
@everyone
角色进行严格限制。然后再扮演另一个更为宽容的角色,
@citizen
(或你想叫它什么都行):

@bot.event
async def on_member_join(member):
    print("A member just joined and his name is" + member.name)
    citizen = discord.utils.get(member.server.roles, name="citizen")

    # DM user with rules
    await bot.send_message("Hello {0.mention}, welcome to {0.server.name}".format(member))
    await bot.send_message(member, Rules)
    dm = await bot.send_message(member, "Type !agree to agree")

    # get response
    check = lambda s: s.lower().startswith("!agree")
    msg = await bot.wait_for_message(channel=dm.channel, author=member, check=check)

    await bot.add_roles(member, citizen)

我是用这个代码做的

    #Welcomer
@bot.event
async def on_member_join(member):
    print("A member just joined and his name is" + member.name)
    mid = member.id
    role = discord.utils.get(member.server.roles, name="Default")
    await bot.add_roles(member, role)

    Rules = "1. Do not DM the Owner or any other staff unless they have DMed you first! \n 2. Be kind, \n 3. Use common sense, \n 4. No swearing, \n 5. No racism or bullying, \n 6. No advertising, \n 7. Do not chat in #music Text or Voice channels, \n 8. Do not spam #applications if your application is accepted or denied! This is only to be used for submitting and staff members replying to the application. \n 9. Do not use or abuse bot commands you should not be using! \n 10. Do not @ any staff members unless it is an emergency.  "

    await bot.send_message(bot.get_channel('479685091749134346'), 'Hello <@%s>, Welcome to Osis Nation, please read all the rules carefully' % (mid))
    await bot.send_message(bot.get_channel('479685091749134346'), (Rules))
    await bot.send_message(bot.get_channel('479685091749134346'), 'If you agree write !Agree. If you do not agree write !Decline')
@bot.event
async def on_message(message):
    if message.content.upper().startswith("!AGREE"):
        await bot.send_message(bot.get_channel('479685091749134346'), 'You will be redirected to the server in 5 seconds')
        time.sleep(5)
        role = discord.utils.get(message.server.roles, name="Default")
        await bot.remove_roles(message.author, role)
欢迎光临 @机器人事件 成员加入时的异步定义(成员): 打印(“刚加入的成员,其名称为“+member.name”) mid=member.id role=discord.utils.get(member.server.roles,name=“Default”) 等待bot.add_角色(成员,角色) 规则=“1。除非业主或任何其他员工先对您进行了DM,否则不要对其进行DM\n2。善待他人。使用常识\n 4。禁止骂人,\n 5。没有种族主义或欺凌\n 6。没有广告,\n 7。请勿在音乐文本或语音频道中聊天,\n 8。如果您的申请被接受或拒绝,请不要发送垃圾邮件!这仅用于提交申请和工作人员回复申请\n9。不要使用或滥用您不应该使用的bot命令\n10。除非是紧急情况,否则不要对任何工作人员下毒。" 等待bot.send_消息(bot.get_频道('479685091749134346'),'您好,欢迎来到Osis国家,请仔细阅读所有规则'%(mid)) 等待bot.send_消息(bot.get_通道('479685091749134346'),(规则)) 等待bot.send_消息(bot.get_频道('479685091749134346'),“如果你同意写!同意。如果你不同意写!拒绝”) @机器人事件 异步def on_消息(消息): 如果message.content.upper().startswith(“!AGREE”): 等待bot.send_消息(bot.get_通道('479685091749134346'),“您将在5秒钟内重定向到服务器” 时间。睡眠(5) role=discord.utils.get(message.server.roles,name=“Default”) 等待bot.remove_角色(message.author,role)
我建议删除
time.sleep(5)
并将其替换为
wait asyncio.sleep(5)
,因为time.sleep是一个阻塞命令。这可能意味着bot在
time.sleep
时会错过其他用户发送的输入。