4

How to load commands from multiple files Python Bot below is my main.py and other python files with commands. Is this correct method or do i need to change anything? do i need to add token, prefix, bot = commands.Bot, bot.run(token) etc in all files.

main.py

token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"

import discord
from discord.ext import commands
startup_extensions = ["second", "third"]

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command(pass_context=True)
async def hello1(ctx):
    msg = 'Hello {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)

second.py

import discord
from discord.ext import commands

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

@commands.command(pass_context=True)
async def hello2(ctx):
    msg = 'Hello{0.author.mention}'.format(ctx.message)
    await bot.say(msg)

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

third.py

import discord
from discord.ext import commands

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

@commands.command(pass_context=True)
async def hello3(ctx):
    msg = 'Hello{0.author.mention}'.format(ctx.message)
    await bot.say(msg)

def setup(bot):
bot.add_cog(Third(bot))
Demotry
  • 869
  • 4
  • 21
  • 40
  • `discord.py` has the concept of "cogs", which are groups of commands, events, etc that your bot can load. See [this example](https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be). What version od `discord.py` are you using? – Patrick Haugh Jun 04 '18 at 11:13
  • @PatrickHaugh I'm using 0.16.12 version. – Demotry Jun 04 '18 at 11:22
  • 1
    Then you should take a look at [this example](https://gist.github.com/leovoel/46cd89ed6a8f41fd09c5) instead. – Patrick Haugh Jun 04 '18 at 11:23
  • All of your remaining references to `bot` in the cog should be changed to `self.bot`. – Patrick Haugh Jun 04 '18 at 11:49
  • No, the `__main__` there refers to [the main script being run in the interpreter](https://stackoverflow.com/questions/419163/what-does-if-name-main-do), not the `main.py` file. I would use that code as-is. – Patrick Haugh Jun 04 '18 at 11:57
  • Ok got it... Thank you bro ... – Demotry Jun 04 '18 at 12:02
  • @PatrickHaugh in your link members.py sub command not working. When we try `?cool` or `?cool _bot` it gives always same reply first one not second answer. – Demotry Jun 05 '18 at 03:50
  • It would be `?cool bot`, because of the `name` parameter passed to command. – Patrick Haugh Jun 05 '18 at 03:53
  • @PatrickHaugh Yes working now. again 1 more problem raised it's working for commands and 2 sub commands. But not working for 3 sub commands giving same reply. `?Iam cool bot` I know something need to edit but I'm confused. – Demotry Jun 05 '18 at 04:57

1 Answers1

3

Would you could do is setup your file with cogs for example your main file:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix="!") # <- Choose your prefix


# Put all of your cog files in here like 'moderation_commands'
# If you have a folder called 'commands' for example you could do #'commands.moderation_commands'
cog_files = ['commands.moderation_commands']

for cog_file in cog_files: # Cycle through the files in array
    client.load_extension(cog_file) # Load the file
    print("%s has loaded." % cog_file) # Print a success message.

client.run(token) # Run the bot.

Say in your moderation_commands file it would look like:

import discord
from discord.ext import commands

class ModerationCommands(commands.Cog):
    def __init__(self, client):
        self.client = client
    @commands.command(name="kick") # Your command decorator.
    async def my_kick_command(self, ctx) # ctx is a representation of the 
        # command. Like await ctx.send("") Sends a message in the channel
        # Or like ctx.author.id <- The authors ID
        pass # <- Your command code here

def setup(client) # Must have a setup function
    client.add_cog(ModerationCommands(client)) # Add the class to the cog.

You can find more information on cogs here: https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html

Jaxon. B
  • 92
  • 6