'Discord.py Editing channel name edit doesn't execute

I have a simple a program that reads a state from a text file and renames a channel accordingly. Yet, I have tried every possible method I could find online and none of them worked. I have isolated the issue to .edit() which is very strange. Everything else seems to work perfectly until it reaches .edit(). Even weirder, it perfectly changes the name of the channel to "Online" but not to "Offline". I literally cannot figure out why this is happening and baffled by this.

Here is my code:

import discord
import asyncio
from discord.ext import tasks

client = discord.Client()

@client.event
async def on_ready():
    print("Bot is ready!")
    status.start()

async def setname(state):                                         # crated a function to change name as last resort
    ch: discord.TextChannel = client.get_channel(947128272758648873)
    states = ["Offline", "Online"]                                # two channel names I want to change to
    await ch.edit(name = states[int(state)])
    print(state)
    
@tasks.loop(seconds = 10)
async def status():
    file = open("status.txt", 'r')
    content = file.read()
    await setname(content)                                        # content contains either a 0 or 1 

client.run('TOKEN')

The code doesn't give any errors, but just stops working (printing the state) when the text file is changed

I have tried:

  • Checking if the state is being read from file (yes)
  • Checking if the channel can be found (yes)

I might be misunderstanding how loop tasks work as I am new to discord.py

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source