#πŸ”’ Discord bot integrated to django

41 messages Β· Page 1 of 1 (latest)

obsidian stirrup
#

So it is supposed to be clear and simple, the bot creates a channel that is named after the user ID when they login, i get this error:

Traceback (most recent call last):
  File "C:\Users\HP\Documents\personal projects\store\try 5\echodrive\discord_bot\services.py", line 58, in _create_channel
    print(f"Created channel {channel.name} for user {user_id}")
                             ^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'name'

C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\asyncio\tasks.py:314: RuntimeWarning: coroutine 'Guild.create_text_channel' was never awaited
  result = coro.send(None)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Error creating Discord channel: object NoneType can't be used in 'await' expression
Full traceback:
Traceback (most recent call last):
  File "C:\Users\HP\Documents\personal projects\store\try 5\echodrive\discord_bot\views.py", line 57, in register_view  
    channel_id = async_to_sync(bot.create_user_channel)(user_profile.profile_id)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\site-packages\asgiref\sync.py", line 254, in __call__   
    return call_result.result()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\_base.py", line 449, in result       
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\_base.py", line 401, in __get_result 
    raise self._exception
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\site-packages\asgiref\sync.py", line 331, in main_wrap  
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object NoneType can't be used in 'await' expression```
astral rampartBOT
#

@obsidian stirrup

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

obsidian stirrup
#

code:

    async def _create_channel(self, user_id):
        if not self._ready:
            print("Bot is not ready yet. Waiting...")
            while not self._ready:
                await asyncio.sleep(1)

        try:
            # Get the first guild (server) the bot is in
            guild = self.client.guilds[0]
            if not guild:
                print("Bot is not in any guilds")
                return None

            # Create channel name in lowercase
            channel_name = f"user-{user_id.lower()}"
            
            # Create the channel
            channel = await guild.create_text_channel(channel_name)
            print(f"Created channel {channel.name} for user {user_id}")
            return channel.id

        except Exception as e:
            print(f"Error creating channel: {str(e)}")
            print("Full traceback:")
            print(traceback.format_exc())
            return None

    def create_user_channel(self, user_id):
        future = asyncio.run_coroutine_threadsafe(self._create_channel(user_id), self._loop)
        return future.result(timeout=10)```
#

if there are other parts u need to see please do tell

#

thank you

#

before answer reply or ping or dm

calm socket
obsidian stirrup
#

this is the name the channel should be named

#

and that is a variable

#

@calm socket

calm socket
#

I know

#

If you wanted the channel name, you've already defined it in channel_name, no?

obsidian stirrup
#

wait a min

#

which line ?

calm socket
#
            channel_name = f"user-{user_id.lower()}"

obsidian stirrup
#

yes this is what i want the channel name to be when created

calm socket
#

Yeah. so pass that to your print statement

obsidian stirrup
#

what statement ?

calm socket
#
            print(f"Created channel {channel.name} for user {user_id}")
obsidian stirrup
#

ok

#

then ?

calm socket
#

replace channel.name

#

with channel_name

#

?

obsidian stirrup
#

done

calm socket
#

Did it work?

obsidian stirrup
#
Error creating Discord channel: object int can't be used in 'await' expression
Full traceback:
Traceback (most recent call last):
  File "C:\Users\HP\Documents\personal projects\store\try 5\echodrive\discord_bot\views.py", line 57, in register_view  
    channel_id = async_to_sync(bot.create_user_channel)(user_profile.profile_id)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\site-packages\asgiref\sync.py", line 254, in __call__   
    return call_result.result()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\_base.py", line 449, in result       
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\_base.py", line 401, in __get_result 
    raise self._exception
  File "C:\Users\HP\AppData\Local\Programs\Python\Python312\Lib\site-packages\asgiref\sync.py", line 331, in main_wrap  
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object int can't be used in 'await' expression```
#

@calm socket

calm socket
obsidian stirrup
#

ah yes tysm

calm socket
obsidian stirrup
#

now this please thank you very much

obsidian stirrup
astral rampartBOT
calm socket
#

Can you share your create_user_channel function please? @obsidian stirrup

#

Oh is this it?

    def create_user_channel(self, user_id):
        future = asyncio.run_coroutine_threadsafe(self._create_channel(user_id), self._loop)
        return future.result(timeout=10)
obsidian stirrup
#

ah yes

#

@calm socket

astral rampartBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.