I'm trying to setup MongoDB with my discord.py bot. I get it to connect, but I cannot retrieve guild prefixes when I define command_prefix.
My current code is:
import discord
from discord.ext import commands
import pymongo
from pymongo import MongoClient
cluster = MongoClient("url im not showing")
db = cluster["Cluster0"]
collection = db["Guild Prefixes"]
def get_prefix(bot, message):
for x in collection.find({"Guild ID": message.guild.id}):
prefix = x["Prefix"]
return prefix
client = commands.Bot(command_prefix=get_prefix, intents=discord.Intents.all(), case_insensitive=True)
I'm getting the following error:
File "c:\Users\colee\Documents\GitHub\Guard\bot.py", line 11, in get_prefix
return prefix
UnboundLocalError: local variable 'prefix' referenced before assignment
My current table looks like:
How can I fix this error?
