如何使用aioredis和discord.py
问题是我不知道如何将discord.ext.commands
与aioredis.create_redis_pool一起使用
我正在启动discord机器人
from discord.ext import commands
@bot.command("get_count")
async def get_count(ctx):
count = get_reactions_count()
# I need to somehow define async redis connection and use it here for example
await ctx.send("some text")
bot = commands.Bot()
bot.run(config.TOKEN)
但在这种情况下,我如何定义redis客户端
我知道我们可以这样做,但这是一个最佳的解决方案吗
@bot.command("get_count")
async def get_count(ctx):
redis = await aioredis.create_redis_pool(
'redis://localhost')
count = get_reactions_count()
# and use redis connection here
await ctx.send("some text")
要使连接保持活动状态,只需将其作为所谓的“bot-var”使用即可
您可以对池执行相同的操作,有两种方法:
一,
简单地使用它
bot.pool.some_method
第二种方法是首选方法,可以多次调用
on_ready
事件您还希望将redis事件循环与bot的循环“连接”,从文档中我可以看到
aioredis.create_redis_pool
将loop
作为可选参数相关问题 更多 >
编程相关推荐