I want to understand some basics in the making of a discord bot
# bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
client.run(TOKEN
I didn't understand clearly what the @client.event
does and how it works, I know its some kind of listener and the function get_ready
Is it a function built in the discord.py package bcz apparently its the same name everywhere
Yes, @client.event is a listener. When discord realises that event has occured it will callback this function.
The name on_ready
is the sam everywhere because it only makes sense to use it. No, you don't need to use on_ready, you can use anything else but you just need to specify it in the decorator instead.
By default, if you just do @client.event, it will take the name of your function.
@client.event(‘on_ready’)
def anotherfuncname():
…
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.