A Python
A Python

Reputation: 93

Unable to log a cog, gettin ExtensionNotFound

I'm not sure if I'm missing something here, but I have a .py file in my cogs folder that will not load. Keeps giving me error ```The above exception was the direct cause of the following exception:

discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.determineHit' could not be loaded.

The following is a picture of my file locations: https://i.sstatic.net/ru1K7.jpg

I will copy the code here as well for you to see, but will snip the bulk of the code as it is quite long. If you need further information, please let me know.

import discord
import json
import random
import os
import time

from pathlib import Path
from threading import Timer
from discord.ext import commands
# from Roll_not_strike import *
# from Roll_true_strike import *
# from playerone_zero_current_hp import *
# from playertwo_zero_current_hp import *


def evasionTimer(self):
    pass

class Hit(commands.Cog):

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

    # Bulk of Code

def setup(client):
    client.add_cog(Hit(client))

With the main script of the bot being:

import discord
import os
from discord.ext import commands

token = open("token.txt", "r").read()

client = commands.Bot(command_prefix = '!')

@client.command()
async def load(ctx, extension):
    client.load_extension("cogs." + extension)

@client.command()
async def unload(ctx, extension):
    client.unload_extension("cogs." + extension)

for filename in os.listdir("./cogs"):
    if filename.endswith('.py'):
        client.load_extension("cogs." + filename[:-3])

client.run(token)

Every other cog in the cogs folder loads up just fine, should I remove determineHit.py I am not sure what I'm missing here. Any help would be appreciated.

Edit:

Before the request for a trace back was asked for, I started thinking on what was going on, and came to the conclusion that the file determineHit.py doesn't really use any discord commands. What is happening is user types in command to discord !roll, bot see that !roll command points to a method in determineHit.py where all the calculations are done (The code in that file is pure python, nothing calling upon discord commands), and then returns that data back to the bot, with all data that needs to be sent to the chat stored in a dictonary called msgwhich then has a for loop to print out all necessary information.

Example, in determineHit.py, you might find:

msg = []
msg.append("Hi Peoples!")
msg.append("Wow! You hit someone!")
msg.append("Aww! You missed!")
msg.append("These are Examples!)
return msg

back in the bot itself, it would unpack msg and print out the statements stored within with a for loop:

for msg_item in msg:
     await ctx.send(msg_item)

Long story short, I assumed that because determineHit.py isn't using anything from the discord library, it doesn't need to be a cog, it can be in another folder in the same directory.

So I did this:

https://i.sstatic.net/4qTqk.jpg

Hoping that might solve my problem. (The other files under determineHit.py are the same, they are just python files containing calculations and the like, they call on no methods in any discord library.)

Evidently, I am wrong. As when I try to run the bot with from determineHit import * I get the extension error that started this all. Run it without the above, and bot starts, but obviously can't run the command, because the method it calls on doesn't exist, because it can't see determineHit.py

Probably more wordy than you needed, I'm sorry. But anyway...

I also did the traceback someone reqested, and I got the following:

"C:\Users\arrae\PycharmProjects\Python Learning\venv\Scripts\python.exe" C:/Users/arrae/PycharmProjects/DiscordDiceGame/BotCommands.py
'Bot' object has no attribute 'load_extensions'
'Bot' object has no attribute 'load_extensions'
'Bot' object has no attribute 'load_extensions'
'Bot' object has no attribute 'load_extensions'
'Bot' object has no attribute 'load_extensions'
Bot is Online

mind you that this was done after I moved determineHit.py out of cogs, and put it in 'gamelogic' folder.

However, if it does help, I put determineHit.py back in the cogs folder, and ran the same traceback code offered:

"C:\Users\arrae\PycharmProjects\Python Learning\venv\Scripts\python.exe" C:/Users/arrae/PycharmProjects/DiscordDiceGame/BotCommands.py
Traceback (most recent call last):
  File "C:\Users\arrae\PycharmProjects\Python Learning\venv\lib\site-packages\discord\ext\commands\bot.py", line 621, in load_extension
    lib = importlib.import_module(name)
  File "C:\Users\arrae\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\arrae\PycharmProjects\DiscordDiceGame\cogs\determineHit.py", line 10, in <module>
    from Roll_not_strike import *
ModuleNotFoundError: No module named 'Roll_not_strike'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/arrae/PycharmProjects/DiscordDiceGame/BotCommands.py", line 25, in <module>
    client.load_extension("cogs." + filename[:-3])
  File "C:\Users\arrae\PycharmProjects\Python Learning\venv\lib\site-packages\discord\ext\commands\bot.py", line 623, in load_extension
    raise errors.ExtensionNotFound(name, e) from e
discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.determineHit' could not be loaded.

I'm sorry if I just made things more complicated. Thought I had found my own answer.

Upvotes: 1

Views: 913

Answers (1)

Uchur
Uchur

Reputation: 81

I would like to see an actual traceback.

You can create a dictionary, list each cog and see if it helps.

async def on_ready():
    print('Bot is ready')

initial_extensions = (
   'cogs.charCreation',
   'cogs.charFeats',
   'cogs.charSheet',
   'cogs.gameCombat',
   'cogs.determineHit'
)

for extension in initial_extensions:
    try:
       client.load_extensions(extension)
    except Exception as e:
       print(e)    

Source: https://github.com/Rapptz/RoboDanny/blob/rewrite/bot.py

Edit: You need to place code above right to the main file after

client = commands.Bot(command_prefix = '!')

Put determineHit.py back to folder with cogs.

Right after

import discord
import os
from discord.ext import commands

paste the following lines to the main file

from gamelogic import onPRIUTil, playerone_zero_current_hp, playertwo_zero_current_hp, Roll_not_strike, Roll_true_strike

Upvotes: 3

Related Questions