警告命令 discord.py postgres sql 错误
Warn command discord.py postgres sql error
所以我从 https://www.youtube.com/channel/UCBwVvFWzp01YMD9ibqhSkEg 的 discord 机器人的 youtube 教程中发出了警告命令
我收到了这个错误
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE
guild_id = AND member_id = ", guild_id, member_id)
AttributeError: 'Connection' object has no attribute 'fetchrow'
代码:
import datetime
import discord
import asyncpg
from discord.ext import commands
class Warn(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.loop.run_until_complete(self.create_db_pool())
@commands.Cog.listener()
async def on_ready(self):
await self.db.execute(
"CREATE TABLE IF NOT EXISTS warnlogs (guild_id BIGINT, member_id BIGINT, warns TEXT[], times DECIMAL[])")
print("warn system ready")
async def create_db_pool(self):
self.db = await asyncpg.create_pool(database="warn", user="postgres", password="")
#self.bot.db = await asyncpg.create_pool(database_url, ssl="require") when hosting the bot
async def warn_log(self, guild_id, member_id):
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
if not data:
return []
return data
async def warn_entry(self, guild_id, member_id, reason, times):
data = await self.warn_log(guild_id, member_id)
if data == []:
await self.bot.db.execute("INSERT INTO warnlogs (guild_id, member_id, warns, times) VALUES (, , , )", guild_id, member_id, [reason], [times])
return
warns = data[2]
times = data[3]
warns.append(reason)
times.append(times)
await self.bot.db.execute("UPDATE warnlogs SET times = , warns = WHERE guild_id = AND member_id = ", times, warns, guild_id, member_id)
@commands.command()
async def warn(self, ctx, member: discord.Member, *, reason="No reason provided"):
if member == ctx.author or member == self.bot.user:
return await ctx.send("You cant mute yourself or the bot.")
await self.warn_entry(ctx.guild.id, member.id, reason, ctx.message.created_at.timestamp())
await ctx.send(f"{ctx.author.name} has warned {member.name} \nReason: {reason}")
data = await self.warn_log(ctx.guild.id, member.id)
count = len(data[3])
embed = discord.Embed(title=f"Warned by {ctx.author.name}", colour=discord.Colour.red(), timestamp=datetime.datetime.utcnow())
embed.add_field(name="Reason", value=reason)
embed.add_field(name="Total warns", value=f"**{count}** warns\n More than 5 warns a week can have serious consequences.")
embed.set_thumbnail(url=ctx.author.avatar_url)
try:
await member.send(embed=embed)
except:
pass
def setup(bot):
bot.add_cog(Warn(bot))
那么有什么方法可以修复此错误吗?如果可以,请发送重写的代码,这将非常有帮助。感谢您的宝贵时间。
据我所知,您将连接池创建到 'self.db'。
self.db = await asyncpg.create_pool(database="warn", user="postgres", password="")
但是您正在 'self.bot.db' 上调用 fetchrow 函数,它不包含您的连接池。您必须调用 'self.db.fetchrow' 而不是 'self.bot.db.fetchrow'。
改变这个:
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
为此:
data = await self.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
您应该将 'self.bot.db.execute' 更改为 'self.db.execute'。
托管机器人后,您必须取消对这部分的注释:
#self.bot.db = await asyncpg.create_pool(database_url, ssl="require") when hosting the bot
并使用 'self.bot.db' 而不是 'self.db'。
所以我从 https://www.youtube.com/channel/UCBwVvFWzp01YMD9ibqhSkEg 的 discord 机器人的 youtube 教程中发出了警告命令
我收到了这个错误
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id) AttributeError: 'Connection' object has no attribute 'fetchrow'
代码:
import datetime
import discord
import asyncpg
from discord.ext import commands
class Warn(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.loop.run_until_complete(self.create_db_pool())
@commands.Cog.listener()
async def on_ready(self):
await self.db.execute(
"CREATE TABLE IF NOT EXISTS warnlogs (guild_id BIGINT, member_id BIGINT, warns TEXT[], times DECIMAL[])")
print("warn system ready")
async def create_db_pool(self):
self.db = await asyncpg.create_pool(database="warn", user="postgres", password="")
#self.bot.db = await asyncpg.create_pool(database_url, ssl="require") when hosting the bot
async def warn_log(self, guild_id, member_id):
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
if not data:
return []
return data
async def warn_entry(self, guild_id, member_id, reason, times):
data = await self.warn_log(guild_id, member_id)
if data == []:
await self.bot.db.execute("INSERT INTO warnlogs (guild_id, member_id, warns, times) VALUES (, , , )", guild_id, member_id, [reason], [times])
return
warns = data[2]
times = data[3]
warns.append(reason)
times.append(times)
await self.bot.db.execute("UPDATE warnlogs SET times = , warns = WHERE guild_id = AND member_id = ", times, warns, guild_id, member_id)
@commands.command()
async def warn(self, ctx, member: discord.Member, *, reason="No reason provided"):
if member == ctx.author or member == self.bot.user:
return await ctx.send("You cant mute yourself or the bot.")
await self.warn_entry(ctx.guild.id, member.id, reason, ctx.message.created_at.timestamp())
await ctx.send(f"{ctx.author.name} has warned {member.name} \nReason: {reason}")
data = await self.warn_log(ctx.guild.id, member.id)
count = len(data[3])
embed = discord.Embed(title=f"Warned by {ctx.author.name}", colour=discord.Colour.red(), timestamp=datetime.datetime.utcnow())
embed.add_field(name="Reason", value=reason)
embed.add_field(name="Total warns", value=f"**{count}** warns\n More than 5 warns a week can have serious consequences.")
embed.set_thumbnail(url=ctx.author.avatar_url)
try:
await member.send(embed=embed)
except:
pass
def setup(bot):
bot.add_cog(Warn(bot))
那么有什么方法可以修复此错误吗?如果可以,请发送重写的代码,这将非常有帮助。感谢您的宝贵时间。
据我所知,您将连接池创建到 'self.db'。
self.db = await asyncpg.create_pool(database="warn", user="postgres", password="")
但是您正在 'self.bot.db' 上调用 fetchrow 函数,它不包含您的连接池。您必须调用 'self.db.fetchrow' 而不是 'self.bot.db.fetchrow'。
改变这个:
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
为此:
data = await self.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = AND member_id = ", guild_id, member_id)
您应该将 'self.bot.db.execute' 更改为 'self.db.execute'。
托管机器人后,您必须取消对这部分的注释:
#self.bot.db = await asyncpg.create_pool(database_url, ssl="require") when hosting the bot
并使用 'self.bot.db' 而不是 'self.db'。