Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

python - (discord py) I cant reffer to the server

When i use ctx.guild.roles it says that i have no required ctx attribute if message attribute is first and message attribute is missing if ctx is the first.

@client.event
async def on_message(message, ctx):
    if message.content == "!lighton":
        light = True
        role = discord.utils.get(ctx.guild.roles, name="Face")
        while light:
            await client.edit_role(server=server, role=role, colour=0xF7B0B0)
            asyncio.sleep(3)
            await client.edit_role(server=server, role=role, colour=0xD36D6D)
            asyncio.sleep(3)

    elif message.content == "!lightoff":
        light = False

I've tried to use server = client.get_server(#server id#) The console says " 'Bot' object has no attribute 'get_server' "


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to do it like this:

light = True

@client.event
async def on_message(message):
    global light
    if message.content == '@lighton':
        light = True
        role = discord.utils.get(message.guild.roles, name = "Face")
        while light:
            await role.edit(colour = 0xF7B0B0)
            await asyncio.sleep(3)
            await role.edit(colour = 0xD36D6D)
            await asyncio.sleep(3)
    elif message.content == '@lightoff':
        light = False

There were a few more errors in your code that I took the liberty of fixing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...