Reputation: 51
So i made a warn command from a youtube tutorial for my discord bot from https://www.youtube.com/channel/UCBwVvFWzp01YMD9ibqhSkEg
and i got this error
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = $1 AND member_id = $2", guild_id, member_id) AttributeError: 'Connection' object has no attribute 'fetchrow'
code:
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 = $1 AND member_id = $2", 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 ($1, $2, $3, $4)", 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 = $1, warns = $2 WHERE guild_id = $3 AND member_id = $4", 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))
so is there any way to fix this error if so can you please send the rewritten code it would be very helpful.🙏 And thank you for your time.😊
Upvotes: 0
Views: 484
Reputation: 453
From what I can see you create connection pool into 'self.db'.
self.db = await asyncpg.create_pool(database="warn", user="postgres", password="")
But then you are calling fetchrow function on 'self.bot.db', which doesn't contain your connection pool. You have to call 'self.db.fetchrow' instead of 'self.bot.db.fetchrow'.
Change this:
data = await self.bot.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = $1 AND member_id = $2", guild_id, member_id)
For this:
data = await self.db.fetchrow("SELECT * FROM warnlogs WHERE guild_id = $1 AND member_id = $2", guild_id, member_id)
And you should change 'self.bot.db.execute' to 'self.db.execute'.
Once you are hosting bot you will have to uncomment this part:
#self.bot.db = await asyncpg.create_pool(database_url, ssl="require") when hosting the bot
And use 'self.bot.db' instead of 'self.db'.
Upvotes: 1