I am trying to set per server custom prefixes in Discord.py with PostgreSQL and asyncpg I watched a video that is 1 years old so maybe thats why that doesn't work any more so i would be appreciate any help
This is my code in main.py
import discord
import os
from discord.ext import commands
import datetime
import asyncio
import random
import json
import asyncpg
from discord.ext.commands import bot, Bot
TOKEN = ''
DEFAULT_PREFIX = "."
async def get_prefix(bot, message):
if not message.guild:
return commands.when_mentioned(DEFAULT_PREFIX)(bot, message)
prefix = await client.db.fetch('SELECT prefix FROM guilds WHERE guild.id = $1', message.guild.id)
if len(prefix) == 0:
await client.db.execute('INSERT INTO guilds("guild_id) VALUES ($1, $2)', message.guild.id, DEFAULT_PREFIX)
prefix = DEFAULT_PREFIX
else:
prefix = prefix[0].get("prefix")
return commands.when_mentioned_or(prefix)(bot,message)
intents = discord.Intents.all()
intents.message_content = True
client = commands.Bot(command_prefix=get_prefix, intents=intents)
async def create_dp_pool():
client.db = await asyncpg.create_pool(database='name', user='postgres', password='')
client.db = await asyncpg.create_pool(dsn='postgres://postgres:password@localhost:5432/name')
print('Connection Successful')
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.online)
print('We have logged in as {0.user}'.format(client))
await client.load_extension('cogs.cog')
await client.load_extension('cogs.commandsforbot')
await client.load_extension('cogs.moder')
@client.event
async def on_member_join(member):
guild = client.get_guild(1016358118982176772)
channel = guild.get_channel(1018637868752703588)
role = discord.utils.get(member.guild.roles, name="Community")
await channel.send(f'Welcome to the server {member.mention} !')
await member.add_roles(role)
@client.command(aliases=['setpre'])
@commands.has_permissions(administrator=True)
async def setprefix(ctx, new_prefix):
await client.db.execute('UPDATE guilds SET prefix = $1 WHERE "guild_id" = $2', new_prefix, ctx.guild.id)
await ctx.send("Prefix updated!")
async def run_until_complete():
async with client:
client.loop.run_until_complete(create_dp_pool())
asyncio.run(run_until_complete())
client.run(TOKEN)
And i get this error
Traceback (most recent call last): File "C:\Users\unfge\Desktop\DISCORD BOT\main.py", line 73, in asyncio.run(run_until_complete()) File "C:\Users\unfge\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\unfge\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete return future.result() File "C:\Users\unfge\Desktop\DISCORD BOT\main.py", line 70, in run_until_complete client.loop.run_until_complete(create_dp_pool()) File "C:\Users\unfge\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 622, in run_until_complete self._check_running() File "C:\Users\unfge\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 582, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running sys:1: RuntimeWarning: coroutine 'create_dp_pool' was never awaited