This code changes the discord bot's status every 10 seconds. Code worked before I pip updated discord.py, but now it gives an error: Someone told me, to create a main
function and inside that function put, await ch_pr()
?
then you need to sue
asyncio.run
to run the funtion
Error:
Traceback (most recent call last):
File "I:\Frecker\testbot.py", line 160, in <module>
client.loop.create_task(ch_pr())
File "C:\Users\Londo\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 108, in __getattr__
raise AttributeError(msg)
AttributeError: loop attribute cannot be accessed in non-async contexts. Consider using either an asynchronous main function and passing it to asyncio.run or using asynchronous initialisation hooks such as Client.setup_hook
Code:
@client.event
async def on_ipc_error(self,endpoint, error):
print(endpoint, "raised", error)
async def ch_pr():
await client.wait_until_ready()
statuses = [
discord.Game(name=f"on {len(client.guilds)} servers! | >help"),
discord.Game(name="discord.py"),
discord.Activity(type=discord.ActivityType.streaming, name="ChezLondo on Youtube", url="https://www.youtube.com/channel/UCE8aHxOOXspTeC0Lm5vx2KA"),
discord.Activity(type=discord.ActivityType.streaming, name="ChezLondo_ on Twitch", url="https://www.twitch.tv/chezlondo_"),
discord.Activity(type=discord.ActivityType.listening, name="Freker on top.gg!"),
discord.Activity(type=discord.ActivityType.watching, name="over you fools")
]
while not client.is_closed():
status = random.choice(statuses)
await client.change_presence(activity=status)
await asyncio.sleep(10)
client.loop.create_task(ch_pr())