admin管理员组文章数量:1390707
I created a custom wraps like this
def addLogger(fn):
from functools import wraps
@wraps(fn)
async def add_logger(*args, **kwargs):
print(f"addLogger: About to run {fn.__name__}")
log = logger
startTime = time.time()
log.info('About to run %s' % fn.__name__)
result = await fn(*args, **kwargs)
log.info('Done running %s Execution time: %s' % (fn.__name__, time.time() - startTime))
print(f"addLogger: Done running {fn.__name__}")
return result
return add_logger
and it's working great on this function
@bot.event
@addLogger
async def on_ready():
logger.info(f'{bot.user.name} has connected to Discord!')
try :
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command")
except Exception as e:
print(e)
but not on list_cogs
its just does nothing
@addLogger
@bot.treemand(name="list_cogs", description="list all cogs")
async def list_cogs(interaction):
await interaction.response.send_message(f"{cogList}")
I already tried to return commands.check(add_logger)
instead of add_logger
but it ends up to not work at all
I created a custom wraps like this
def addLogger(fn):
from functools import wraps
@wraps(fn)
async def add_logger(*args, **kwargs):
print(f"addLogger: About to run {fn.__name__}")
log = logger
startTime = time.time()
log.info('About to run %s' % fn.__name__)
result = await fn(*args, **kwargs)
log.info('Done running %s Execution time: %s' % (fn.__name__, time.time() - startTime))
print(f"addLogger: Done running {fn.__name__}")
return result
return add_logger
and it's working great on this function
@bot.event
@addLogger
async def on_ready():
logger.info(f'{bot.user.name} has connected to Discord!')
try :
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command")
except Exception as e:
print(e)
but not on list_cogs
its just does nothing
@addLogger
@bot.treemand(name="list_cogs", description="list all cogs")
async def list_cogs(interaction):
await interaction.response.send_message(f"{cogList}")
I already tried to return commands.check(add_logger)
instead of add_logger
but it ends up to not work at all
1 Answer
Reset to default 3You need to have @addLogger
after the @bot.treemand()
line. So the correct code would be
@bot.treemand(name="list_cogs", description="list all cogs")
@addLogger
async def list_cogs(interaction):
await interaction.response.send_message(f"{cogList}")
Think of it like @addLogger
changing the code of list_cogs()
so the new list_cogs()
function will do what it did before, but also time itself. Then, once you have that new function, that's what you add to the bot tree as a command.
本文标签: pythonCustom wraps on discord commandStack Overflow
版权声明:本文标题:python - Custom wraps on discord command - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744632091a2616602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论