0

I am making a discord bot using discord.py API. After doing some coding I've realized that I should keep the code clean and keep the commands and events in separate .py files. How could I do that event or command still listen for a trigger and are in separate files? I've tried doing it with import but it just imports classes. Example command:

@client.command(pass_context=True) async def kick(ctx, *, member: discord.Member = None): if ctx.message.channel.permissions_for(ctx.message.author).administrator is True: await client.send_message(member, settings.kick_direct) await client.kick(member) await client.say(settings.kick_message + member.mention + settings.kick_message2) else: await client.say(settings.permission_error)

SiDzej
  • 72
  • 2
  • 9

1 Answers1

0

You need to load the extension in the file where you create your discord.py client. See below example.

bot.py

from discord.ext import commands

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

client.load_extension('cog')

@client.event
async def on_ready():
    print('client ready')

@client.command()
async def ping():
    await client.say('Pong')

client.run('TOKEN')

cog.py

from discord.ext import commands

class TestCog:

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

    @commands.command()
    async def add(self):
        self.counter += 1
        await self.bot.say('Counter is now %d' % self.counter)


def setup(bot):
    bot.add_cog(TestCog(bot))
Benjin
  • 3,448
  • 1
  • 10
  • 20
  • It worked although it doesn't do anything. Bot doesn't respond on command. – SiDzej Nov 15 '18 at 16:53
  • I have tested the code on my side and it works fine. Are you placing both `bot.py` and `cog.py` in the same folder? – Benjin Nov 15 '18 at 17:05
  • Yeah, same folder. – SiDzej Nov 15 '18 at 17:31
  • Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load – Benjin Nov 15 '18 at 17:35
  • Bot is definitely running, the file is loaded, although it doesn't execute a command. – SiDzej Nov 15 '18 at 17:45
  • So using both `!ping` and `!add` do nothing? What about the default `!help`? – Benjin Nov 15 '18 at 17:48
  • I've implemented it with command I'd like to use if it's not a problem. – SiDzej Nov 15 '18 at 17:50
  • I can run commands in main file, but can't run this one command in standalone file. – SiDzej Nov 15 '18 at 17:53
  • Change `client.load_extension('kick')` to `client.load_extension('command')`. It has to be the file name, not the class name. – Benjin Nov 15 '18 at 18:07
  • It is named kick, I just called the link like that. kick.py is running because i get printed checkstate 1 but the second print isn't showing up after giving a command. This means that extension is running, but the command isn't. – SiDzej Nov 15 '18 at 18:14
  • Your kick command should have `self` at the start, not the end. Also, in future please include full code samples in your initial question. – Benjin Nov 15 '18 at 18:18
  • The example I gave works fine. See here: https://i.imgur.com/cgFtKgw.png – Benjin Nov 15 '18 at 18:58
  • It has worked now. I've found out that one of my events interfered with my command, and blocked this command from being registered. Thanks a lot for your help. – SiDzej Nov 15 '18 at 20:55
  • Glad to hear it's working. For future reference, if the interfering event was `on_message`, see here: https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working – Benjin Nov 16 '18 at 05:06
  • Thanks a lot. It was an `on_message` event. – SiDzej Nov 16 '18 at 13:53