Python 为什么这里不需要@client.event decorator?

Python 为什么这里不需要@client.event decorator?,python,class,discord,discord.py,decorator,Python,Class,Discord,Discord.py,Decorator,最近我一直在尝试学习Python,我一直在看一些机器人和自助机器人的源代码,试图了解发生了什么。我现在看到的自助机器人(当然是出于教育目的)没有@client.event decorator,我几乎在任何地方都见过。下面是两个相关文件的代码片段 main.py: # Importing import asyncio from collections import namedtuple import signal from replit import clear import client i

最近我一直在尝试学习Python,我一直在看一些机器人和自助机器人的源代码,试图了解发生了什么。我现在看到的自助机器人(当然是出于教育目的)没有@client.event decorator,我几乎在任何地方都见过。下面是两个相关文件的代码片段

main.py:

# Importing
import asyncio 
from collections import namedtuple
import signal
from replit import clear

import client
import config
import updater

# Create clients
entries = []
clear()
for login in config.clients:
  entry = namedtuple('entry', 'client, token')
  newClient = client.myClient(login.catchingChannels, login.prefix, login.autoDuration)
  entries.append(
    entry(client = newClient, token = login.token))

loop = asyncio.get_event_loop()
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)

# Shutdown
async def shutdown(signal, runningBots):
  [runningBot.cancel() for runningBot in runningBots]
  await asyncio.gather(*runningBots, return_exceptions=True)
  loop.stop()

async def wrapped_connect(entry):
  try:
    await entry.client.start(entry.token, bot = False)
    print('Logged in as {}'.format(entry))
  finally:
    await entry.client.close()

try:
  runningBots = []
  for entry in entries:
    runningBots.append(loop.create_task(wrapped_connect(entry)))

  for s in signals:
    loop.add_signal_handler(s, lambda s = s: asyncio.create_task(shutdown(s, runningBots)))

  loop.run_forever()
finally:
  client.utils.cprint("Exiting...", "yellow")
  client.utils.shutdown_event()
  exit()
client.py:

### Discord Clients

# Importing
import discord; from discord.ext import tasks
import random
from time import sleep
import re
import string
from replit import db
from asyncio import sleep as asleep
import io
import contextlib

import config
import utils

class myClient(discord.Client): 
  def __init__(self, allowedChannels, prefix, autoDuration):
    super().__init__()
    self.allowedChannels = allowedChannels
    self.prefix = prefix
    self.autoDuration = autoDuration

  async def on_ready(self):
    utils.cprint("{} logged in".format(self.user), "blue")
    await self.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Pokémon spawn"))
    self.running = False
    self.spamChannels = await self.get_channels()
    self.data = pokemon.dataManager()
    self.prefixes = [config.globalPrefix, self.prefix]
    length = len(str(self.user))
    self.name = str(self.user)[:length - 5]
    self.autoKey = "autoEnabled_" + self.name
    self.commandChannel = self.get_channel(random.choice(self.allowedChannels))
    self.init_auto()

  async def get_channels(self):
    channels = []
    for channel_ID in config.spamChannels:
      channels.append(self.get_channel(channel_ID))
    return channels

  # Spammer
  @tasks.loop(seconds=2.5)
  async def spammer(self):
    channel = random.choice(self.spamChannels)
    try:
      await channel.send(' '.join(random.sample(string.ascii_letters+string.digits,50)*20))
    except:
      pass

  async def on_message (self, message):
    # Command parser
    result = list(filter(message.content.startswith, self.prefixes)) != []
    if result == True and message.author.id in config.ownerIDs:
      content = utils.filterer(message.content, self.prefix)

      # Ping command
      if content == "ping":
        await message.channel.send(f'Pong! {round(self.latency * 1000)}ms')
.....
我删除了一些不相关的代码,但是还有很多事情发生。我认为所有东西都在一个类中并且有两个文件的原因是它登录到多个帐户


因此,对于我的问题,为什么on_message函数不使用@client.event decorator,没有它它它如何工作?

在我看来,这是可行的,因为客户端本身不是默认的discord客户端,而是从原始discord客户端继承的修改版本。由于这是一个子类,我相信它不需要装饰器。子类只是覆盖
discord.Client
。因此,如果被覆盖,bot将使用新的
事件。这也可以通过普通机器人来完成。看见