Reputation: 95
I created a Python Discord Bot, I put all the functions into a class but when I did I started getting a Type Error, only thing is the error is telling me to do exactly what I did. Here is the affected code and the error.
client = commands.Bot(command_prefix='--', case_insensitive=True)
class Settings:
def __init__(self):
self.spitfirerole = 917086768736649258
with open("data.json") as f:
data = json.load(f)
f.close()
self.prev = data["serial"]
@client.event
async def on_ready(self):
print('ThatOneGuy is online.')
await client.change_presence(activity=discord.Game(name="chess with Mr.Z"))
channel = client.get_channel(918300328267497502)
em = discord.Embed(title=f"Status", color=0x22ff12)
em.add_field(name="Active", value=f"I am now searching for a Spitfire.")
await channel.send(f"<@&{self.spitfirerole}>", embed=em)
The error;
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Users\Libra\PycharmProjects\ThatOneGuy\venv\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
TypeError: on_ready() missing 1 required positional argument: 'self'
Upvotes: 2
Views: 780
Reputation: 71542
When an on_ready
handler is registered with @client.event
, it's expected to be a function that takes no arguments. If you want your on_ready
to be a method in a class, you'll need to make it a static method:
@client.event
@staticmethod
async def on_ready():
print('ThatOneGuy is online.')
await client.change_presence(activity=discord.Game(name="chess with Mr.Z"))
If it needs to be an instance method, you probably want to subclass discord.Client
(or commands.Bot
) rather than using @client.event
:
class Settings(commands.Bot):
async def on_ready(self):
print('ThatOneGuy is online.')
await self.change_presence(activity=discord.Game(name="chess with Mr.Z"))
client = Settings(command_prefix='--', case_insensitive=True)
Another possibility would be having a global on_ready
that takes no arguments and delegates to your Settings instance:
client = commands.Bot(command_prefix='--', case_insensitive=True)
settings = Settings()
@client.event
def on_ready():
settings.on_ready()
Upvotes: 2