python - 在 python 类中使用装饰器,未定义的变量
问题描述
我有一个运行装饰器的函数,但我需要async def _test
从类内部运行。
class Slash(commands.Cog):
def __init__(self, bot):
self.bot = bot
def setup(bot):
bot.add_cog(Slash(bot))
bot.slash = SlashCommand(bot, override_type=True, sync_commands=True)
@bot.slash.slash(name="test")
async def _test(ctx: SlashContext, a=None, b=None):
embed = discord.Embed(title="embed test")
await ctx.send(content="test", embeds=[embed])
bot
如果我只是在类中移动代码,则没有定义,并且self.
不能在装饰器中使用。知道如何让这个函数出现在课堂上吗?谢谢
解决方案
您可以简单地使用cog_ext.cog_slash
装饰器
from discord_slash import cog_ext
class Slash(commands.Cog):
def __init__(self, bot):
self.bot = bot
@cog_ext.cog_slash(name="test")
async def _test(self, ctx: SlashContext, a=None, b=None):
embed = discord.Embed(title="embed test")
await ctx.send(content="test", embeds=[embed])
PS:在介绍中,我什至没有看文档,只是在 PyPi 页面上......我不知道你怎么会错过它
推荐阅读
- sas - 循环数组,保留值
- node.js - 执行 mongoose/mongodb 查询的副作用
- dataframe - SQL 和 withColumn 之间的性能
- java - Spring-boot 和 Keycloak 集成
- javascript - JS中的就地重定向
- c++ - 如何修复检测到堆损坏:在正常块之后
- javascript - 为什么我在 w3c 标准中找不到 Element.after() 方法?
- django - Django:使用 API 和 Pickle 发布 Pandas 数据帧或如何“hello world”序列化程序和查看 Django REST 框架中的二进制数据
- python-3.x - 如何修复这个 dict 和 postgreSQL
- javascript - 使用 styled-components 防止组件重新渲染