Search code examples
pythonmongodbdiscorddiscord.pypymongo

How can I find different parts of the same mongodb entry in discord.py


I am currently working on a system that deletes a channel when the user leaves. This system works by, when a user asks for it, a command is used to create the channel and the member id and channel id are stored in mongodb, as shown in the picture below:

MongoDB Collection

My current code for this is:

  @commands.Cog.listener()
    async def on_member_remove(self, member):
        channelfind = cluster["Channels"]["info"]
        if member.guild.id == testid:
            joinedat = diskord.utils.utcnow() - member.joined_at
            time = humanize.precisedelta(joinedat, format="%0.0f")
            embed = diskord.Embed(title="\u200b", color=0xfc8eac)
            embed: Embed = diskord.Embed(
                description= f'**{member.mention} left the server**\n:timer: **Joined:**\n{time} ago',
                color=0xfc8eac
            )
            embed.set_author(name=member, icon_url=member.avatar.url)
            embed.set_thumbnail(url=member.avatar.url)
            embed.timestamp = datetime.datetime.utcnow()
            embed.set_footer(text=f'ID: {member.id} \u200b ')
            memberid = channelfind.find_one({"member_id": member.id})
            if memberid is not None:
                
                log = testlog
                await self.bot.get_channel(log).send(embed=embed)
            else:
                pass

However, I am not sure how I can find the channel_id that is with the member_id of the user who has left

Any help would be appreciated thank you!


Solution

  • .find_one() returns a dictionary representing the document. So, you can do

    memberid = channelfind.find_one({"member_id": member.id})
    print(memberid['channel_id'])
    

    to get the channel_id.