【问题标题】:How can I make the bot write on how many servers it is active in its status? (Discord.py Rewrite)如何让机器人在其状态下激活多少台服务器? (Discord.py 重写)
【发布时间】:2020-12-24 13:04:26
【问题描述】:

我想要做的实际上是机器人来写我的消息和有多少服务器在线。

我的代码:

status = cycle([f"I am online on {len(bot.guilds)} servers!","Second Status")

@bot.event
async def on_ready():
    print("Logged in as: " + bot.user.name + "\n")
    await change_status.start()

@tasks.loop(seconds = 30)
async def change_status():
    await bot.change_presence(activity = discord.Game(next(status)))

当我尝试时,Bot 状态如下:

我在 0 台服务器上在线!

每次循环重复时,机器人都会说:

我在 0 台服务器上在线!

但该机器人目前在 3 台服务器上在线。我该如何解决这个问题?

【问题讨论】:

    标签: python-3.x discord bots discord.py discord.py-rewrite


    【解决方案1】:

    在函数中定义状态,以便在机器人准备好后不断更新和加载。

    status = 0
    
    @bot.event
    async def on_ready():
        print("Logged in as: " + bot.user.name + "\n")
        await change_status.start()
    
    @tasks.loop(seconds = 30)
    async def change_status():
        statuses = [f"I am online on {len(bot.guilds)} servers!", "Second Status"]
        if status + 1 > len(statuses):
            status = 0
        await bot.change_presence(activity = discord.Game(statuses[status]))
        status += 1
    
    

    【讨论】:

    • 这对我不起作用。当我将状态放入函数时,它会在每个循环开始时重新启动列表,所以我只能看到第一个状态。
    • 现在试试,我从来没有使用过循环,所以我让我的代码使用纯python,它只是通过索引某个整数来遍历列表。
    猜你喜欢
    • 2021-06-02
    • 2021-01-06
    • 2021-09-09
    • 2021-04-13
    • 2021-04-15
    • 2019-05-26
    • 1970-01-01
    • 2019-06-15
    • 2016-03-02
    相关资源
    最近更新 更多