不和谐机器人编辑角色

discord bot edit role

我正在尝试扮演一个永久改变颜色的角色。我正在使用 discord.py 重写。

几个小时前它还能工作,现在我很困惑,因为它不再工作了。我什么也没改变。

这是我的代码:

import discord
import asyncio
from discord.ext import commands

bot = commands.Bot(command_prefix='?', description="description")


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')


@bot.command()
async def rgb(ctx, time: int):
    colours = [0xFF0000, 0x00FF00, 0x0000FF]
    i = 0
    selectedrole = ctx.guild.get_role(748128282859274251)

    while True:
        i = (i + 1) % 3
        print("This is printed")
        await selectedrole.edit(colour=discord.Colour(colours[i]))
        print("This will not be printed")
        await asyncio.sleep(time)


bot.run('xxxxx')

没有运行这行代码就停止了(程序还在运行ning,没有任何反应)

await selectedrole.edit(colour=discord.Colour(colours[i]))

这是因为您的机器人正在向 API 发送垃圾邮件,因此永远不会等待编辑代码。如果检测到超时错误,您可以尝试删除该角色并重新创建该角色,这可能会有所帮助,但无论哪种方式,您都应该避免向 API 发送垃圾邮件,因为它可能会删除您的机器人帐户。

是的,你必须建立一个定时器,我不会让这个通过命令改变。但请注意,Discord 不喜欢 Rainbow 角色,或者至少在 2018 年不喜欢他们。我没有任何其他信息。 [https://twitter.com/discord/status/1055182857709256704?s=20]

顺便说一句。您可能必须更改 Bot-Token,因为您在这里泄露了它,但无论如何这是代码。

import asyncio
from discord.ext import commands

bot = commands.Bot(command_prefix='?', description="description")
TOKEN = 'YOUR NEW TOKEN'


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')


@bot.command()
async def rgb(ctx):
    colours = [0xFF0000, 0x00FF00, 0x0000FF]
    i = 0
    selectedrole = ctx.guild.get_role(101010101010101)

    while True:
        await asyncio.sleep(10)
        i = (i + 1) % 3
        print("Color changed")
        await selectedrole.edit(colour=discord.Colour(colours[i]))


bot.run(TOKEN)