Async events in other file











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 at 16:05









      Phillip CJ

      74




      74
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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.
            – Phillip CJ
            Nov 15 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 at 17:05












          • Yeah, same folder.
            – Phillip CJ
            Nov 15 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 at 17:35










          • Bot is definitely running, the file is loaded, although it doesn't execute a command.
            – Phillip CJ
            Nov 15 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',
          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








          up vote
          0
          down vote



          accepted










          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.
            – Phillip CJ
            Nov 15 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 at 17:05












          • Yeah, same folder.
            – Phillip CJ
            Nov 15 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 at 17:35










          • Bot is definitely running, the file is loaded, although it doesn't execute a command.
            – Phillip CJ
            Nov 15 at 17:45















          up vote
          0
          down vote



          accepted










          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.
            – Phillip CJ
            Nov 15 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 at 17:05












          • Yeah, same folder.
            – Phillip CJ
            Nov 15 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 at 17:35










          • Bot is definitely running, the file is loaded, although it doesn't execute a command.
            – Phillip CJ
            Nov 15 at 17:45













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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 at 17:06

























          answered Nov 15 at 16:26









          Benjin

          74328




          74328












          • It worked although it doesn't do anything. Bot doesn't respond on command.
            – Phillip CJ
            Nov 15 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 at 17:05












          • Yeah, same folder.
            – Phillip CJ
            Nov 15 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 at 17:35










          • Bot is definitely running, the file is loaded, although it doesn't execute a command.
            – Phillip CJ
            Nov 15 at 17:45


















          • It worked although it doesn't do anything. Bot doesn't respond on command.
            – Phillip CJ
            Nov 15 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 at 17:05












          • Yeah, same folder.
            – Phillip CJ
            Nov 15 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 at 17:35










          • Bot is definitely running, the file is loaded, although it doesn't execute a command.
            – Phillip CJ
            Nov 15 at 17:45
















          It worked although it doesn't do anything. Bot doesn't respond on command.
          – Phillip CJ
          Nov 15 at 16:53






          It worked although it doesn't do anything. Bot doesn't respond on command.
          – Phillip CJ
          Nov 15 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 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 at 17:05














          Yeah, same folder.
          – Phillip CJ
          Nov 15 at 17:31




          Yeah, same folder.
          – Phillip CJ
          Nov 15 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 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 at 17:35












          Bot is definitely running, the file is loaded, although it doesn't execute a command.
          – Phillip CJ
          Nov 15 at 17:45




          Bot is definitely running, the file is loaded, although it doesn't execute a command.
          – Phillip CJ
          Nov 15 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?