简体   繁体   English

Discord Bot python 理解

[英]Discord Bot python understanding

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我不清楚@client.event的作用以及它是如何工作的,我知道它是某种侦听器和函数get_ready它是不是内置在 discord.py 包 bcz 中的函数,显然它在任何地方都同名

Yes, @client.event is a listener.是的,@client.event 是一个监听器。 When discord realises that event has occured it will callback this function.当 discord 意识到事件已经发生时,它会回调这个函数。

The name on_ready is the sam everywhere because it only makes sense to use it. on_ready这个名字无处不在,因为只有使用它才有意义。 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.不,你不需要使用 on_ready,你可以使用其他任何东西,但你只需要在装饰器中指定它。

By default, if you just do @client.event, it will take the name of your function.默认情况下,如果您只是执行@client.event,它将采用您的函数的名称。

@client.event(‘on_ready’)
def anotherfuncname():
   …

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM