我有一个discord.py bot,它根据命令中给出的参数,尝试读取postgresql表的不同条目。例如,如果一个人要做$playtime penguin,那么它将以分钟为单位给出为帐户“penguin”播放的条目。这是我到目前为止所拥有的
import discord
from discord.ext import commands
import asyncio
import asyncpg
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.command()
async def playtime(ctx, arg):
arg = name()
conn = await asyncpg.connect('postgresql://{POSTGRES USERNAME/PASSWORD}@localhost/postgres')
playtime = await conn.fetchrow(
"SELECT minutes_played FROM public.penguin WHERE username = name();")
await ctx.send(playtime)
bot.run('{BOT TOKEN}')
但是,这不起作用,因为它没有定义name()。我希望name()被定义为discord用户在命令中给出的参数,例如penguin。如何将name()定义为discord命令中给定的参数
从我在文档中看到的
arg
应该包含作为命令的第一个参数提供的名称。还要确保使用参数化查询来避免sql injections。以下代码应该可以工作:如果向命令传递了错误的名称,可能还需要添加一些错误处理代码
参考资料:
[1]https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html
[2]https://magicstack.github.io/asyncpg/current/usage.html
相关问题 更多 >
编程相关推荐