我試圖讓我的機器人每小時更改一次狀態,但它給了我標題中的錯誤,我嘗試放置self但它會給我這個錯誤:AttributeError: 'statusscript' object has no attribute 'ws'.
global list
list = ["1", "2", "3"]
@commands.command(aliases=['add'])
async def addstatus(self, ctx, *, arg):
global list
list.append(arg)
await ctx.send(f'`{arg}` added to status list')
@commands.command()
async def startstatus(self, ctx):
@tasks.loop(seconds=3600)
async def looper(self):
duration = 3600
print(f"LOOPER IS RUNNING EVERY {duration} SECONDS!")
selected = random.choice(list)
activity = selected
client = commands.Bot
await client.change_presence(activity=discord.Game(activity))
looper.start(self)
uj5u.com熱心網友回復:
- 不要使用內置資料型別的變數名稱。它有時效果不佳,因此避免它是一個好習慣。
- 我猜這是
Cog因為self. 然后,當您要參考時,client必須使用self.client.
我創建了Testcog 來讓你看看它應該是什么樣子:
class Test(commands.Cog):
def __init__(self, client):
self.client = client
global status_list
status_list = ["1", "2", "3"]
@commands.command(aliases=['add'])
async def addstatus(self, ctx, *, arg):
global status_list
status_list.append(arg)
await ctx.send(f'`{arg}` added to status list')
@commands.command()
async def startstatus(self, ctx):
self.looper.start()
@tasks.loop(seconds=3600) # you can also use "hours=1" instead of seconds
async def looper(self):
duration = 3600
print(f"LOOPER IS RUNNING EVERY {duration} SECONDS!")
activity = random.choice(status_list)
await self.client.change_presence(activity=discord.Game(activity)) # because you are in "Cog" you have to use self.client to refer to client
client.add_cog(Test(client))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/414115.html
標籤:
