gpt4 book ai didi

python - cog 中的 discord.py 状态

转载 作者:行者123 更新时间:2023-12-04 01:07:02 26 4
gpt4 key购买 nike

我到处搜索这个问题,但找不到答案。

我刚刚开始学习 discord.py,之前的 python 知识很少,如果这个问题看起来很简单,我很抱歉,我只使用 YouTube 教程就做了大约 12 个小时。

所以我的问题是如何在齿轮内部设置移动状态?我不断收到一条错误消息,提示“AttributeError:模块‘discord.ext.commands’没有属性‘event’”。

代码是

import discord
import os
from discord.ext import commands
from itertools import cycle

status = cycle(['First status', 'Second status', 'Third status'])

class status(commands.Cog):

def __init__(self, client):
self.client = client

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

@commands.event
async def on_ready():
change_status.start()
print('Bot ready')


def setup(client):
client.add_cog(status(client))

在此先感谢您的帮助!

最佳答案

经过相当多的研究,我已经能够修复您遇到的错误。但是,我一直无法真正改变状态;这可能是因为我没有正确使用 change_presence() 方法。尽管如此,我至少可以帮助您消除错误,您可以从那里开始。下面是我在您的代码中指出的先前错误的合并,以及将消除错误消息的代码。

import discord
import os
from discord.ext import commands, tasks
from itertools import cycle

class status(commands.Cog):

def __init__(self, client):
self.client = client
self.status = cycle(['First status', 'Second status', 'Third status'])

@tasks.loop(seconds=3.0)
async def change_status(self):
print('Changing status')
await self.client.change_presence(activity=discord.Game(next(self.status)))

@commands.Cog.listener()
async def on_ready(self):
self.change_status.start()

def setup(client):
client.add_cog(status(client))

导致错误的原因是 @commands.event 不存在。相反,您必须使用 @commands.Cog.listener(),这就是错误显示 'discord.ext.commands' has no attribute 'event' 的原因.我找到了一个 reddit forum谈论您遇到的相同问题,并且该人实际上能够解决它,所以我建议您仔细阅读并亲自尝试。我将删除我的旧答案,因为这个包含我指出的所有内容。另外,如果您接受这个答案,我将不胜感激,因为它确实消除了您询问的错误。

关于python - cog 中的 discord.py 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66095007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com