#load_extension

1 messages · Page 1 of 1 (latest)

strange island
#

v2.4.1

load_extension problem

bot.py

import os
import discord
import configparser
from discord.ext import commands

conf = configparser.ConfigParser()
conf.read('config.ini')

bot = commands.Bot(
    command_prefix=commands.when_mentioned_or(conf.get('setting', 'Prefix')),
    intents=discord.Intents.all()
)

for filename in os.listdir('module'):
    if filename.endswith('.py'):
        await bot.load_extension(f'module.{filename[:-3]}')

if __name__ == "__main__":
    bot.run(conf.get('setting', 'TOKEN'))

commands.py

import asyncio
import discord
from discord.ext import commands
from discord import ApplicationContext
from discord.commands import slash_command


class util(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print(f'Logged in as {self.bot.user.name}')

    @slash_command(description="ping")
    async def ping(self, ctx: ApplicationContext):
        await ctx.respond(f'Pong! In {round(self.bot.latency * 1000)}ms')

def setup(bot):
    bot.add_cog(util(bot))

Error

RuntimeWarning: Enable tracemalloc to get the object allocation traceback
bot.py:28: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
  bot.load_extension(f'module.{filename[:-3]}')

If add await and async with add_cog

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/homebrew/Cellar/[email protected]/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/lily199iu/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/Users/lily199iu/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/Users/lily199iu/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/Users/lily199iu/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 320, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/Users/lily199iu/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 294, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "bot.py", line 26
    await bot.load_extension('dismusic')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function
echo agate
#

Can you show the pip list?

olive oyster
#

load_extension isn't a co-routine, so remove await

strange island
#
Package            Version
------------------ -----------
aiodns             3.0.0
aiohttp            3.8.5
aiosignal          1.3.1
annotated-types    0.5.0
anyio              3.6.2
async-timeout      4.0.3
attrs              22.2.0
beautifulsoup4     4.12.2
Brotli             1.0.9
certifi            2022.12.7
cffi               1.15.1
charset-normalizer 2.1.1
discord.py         2.3.2
frozenlist         1.3.3
h11                0.14.0
httpcore           0.16.3
httpx              0.24.1
idna               3.4
lxml               4.9.3
multidict          6.0.4
orjson             3.8.5
pip                22.3.1
py-cord            2.4.1
pycares            4.3.0
pycparser          2.21
pydantic           2.3.0
pydantic_core      2.6.3
PyNaCl             1.5.0
requests           2.31.0
rfc3986            1.5.0
setuptools         65.6.3
sniffio            1.3.0
soupsieve          2.3.2.post1
typing_extensions  4.7.1
urllib3            1.26.14
wavelink           2.6.4
wheel              0.38.4
yarl               1.8.2
ytmusicapi         1.2.0

[notice] A new release of pip available: 22.3.1 -> 23.2.1
[notice] To update, run: pip install --upgrade pip
olive oyster
echo agate
#

And py-cord and reinstall only py-cord

strange island
#

it work thanks ~

olive oyster
#

Great 😄

#

.tag close

dapper pastureBOT
#

Done with your help thread?

Please close your own help thread by using </close:1009144375709814897> with @twilit compass.

Backup bot: </solved:1109625445990793246> (or .solved) with @dapper pasture.