Async events in other file



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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)










share|improve this question




























    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)










    share|improve this question
























      0












      0








      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)










      share|improve this question














      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)







      python asynchronous command python-3.6 discord.py






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 16:05









      SiDzejSiDzej

      2716




      2716






















          1 Answer
          1






          active

          oldest

          votes


















          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))





          share|improve this answer

























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323416%2fasync-events-in-other-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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))





          share|improve this answer

























          • 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















          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))





          share|improve this answer

























          • 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













          0












          0








          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))





          share|improve this answer















          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))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 17:06

























          answered Nov 15 '18 at 16:26









          BenjinBenjin

          94329




          94329












          • 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

















          • 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
















          It worked although it doesn't do anything. Bot doesn't respond on command.

          – SiDzej
          Nov 15 '18 at 16:53






          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






          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





          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





          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





          Bot is definitely running, the file is loaded, although it doesn't execute a command.

          – SiDzej
          Nov 15 '18 at 17:45



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323416%2fasync-events-in-other-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Peter Parker: The Spectacular Spider-Man #308